简体   繁体   中英

sas passing a variable to a conditional macro

I can't figure out how to pass to a conditional macro the values of a dataset variable. Let's say we HAVE:

data HAVE;
    input id  name $ value ;
    datalines;
    1 pluto 111
    2 paperino 222
    3 trump 333
    4 topo 444
    ;
run;

I would like to use dataset variable name inside a sas conditinal macro to make other IF conditions.

What i mean is to use this code with another IF step before the conditional macro (or inside the conditional macro)

options minoperator mlogic;
  %macro test(var) / mindelimiter=',';

    %if &var in(pippo,pluto) %then %do; "if &var"n = name; end;
    %else %do;"mod &var"n = name;end;

  %mend test;

data want;
    set have;
    %test(pippo);
    %test(arj);
    %test(frank);
    %test(pluto);
    %test(george);
run;

For example:

options minoperator mlogic;
%macro test(var) / mindelimiter=',';

 if name = &var then do;

   %if &var in(pippo,pluto) %then %do "if &var"n = name; %end;
   %else %do; "mod &var"n = name; end;

 end;

%mend test;

but the IF name = &var is always true... there's some problem with using the name dataset variable inside the macro.

EDIT AFTER first answer

example code of conditioning inside the conditional macro:

%macro test(var) / mindelimiter=',';

 %if &var in(pippo pluto) %then %do; 
  if name = 'pluto' then ifif_&var. = name;
  if_&var. = name; 
  %end;
 %else %do; 
  mod_&var. = name; 
 %end;

%mend test;

it' just an example off course it's almost useless.

There's nothing inherently wrong with using it that way, though you have some errors in your code.

%macro test(var) / mindelimiter=',';

 if name = "&var" then do;

   %if &var in(pippo pluto) %then %do; if_&Var. = name; %end;
   %else %do; mod_&var. = name; %end;

 end;

%mend test;

That works, or at least as far as I can tell works for what you want.

Looks like you want to generate code from your data.

data _null_;
  set have end=eof;
  if _n_=1 then call execute('data want;set have;');
  call execute(cats('%nrstr(%test)(',name,')'));
  if eof then call execute('run;');
run;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM