0
votes

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.

2
Looks like you want to use the data in dataset HAVE to generate the datastep WANT. If so then you need to add another data step (data null step usually) and either generate the code using CALL EXECUTE() or write the code to a file and then using %INCLUDE to run the generated code.Tom

2 Answers

1
votes

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.

0
votes

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;