Search code examples
macrossasdatastep

SAS: macro procedure error in data step


I cannot find the solution to this error. I tried usign %eval, %sysfunc, and %sysevalf, with no success. What is needed to correctly evaluate the "&set" in the macro?

%macro set_istituzionale(set=, varout=);
  %if &set/100 = 1 %then &varout = 'AP';
%mend set_istituzionale;

data soff2; set soff; %set_istituzionale(set=setcon,varout=set); run;

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:
       &set/100 = 1
ERROR: The macro SET_ISTITUZIONALE will stop executing.

Solution

  • The logic behind you're macro is wrong.

    You are calling that macro inside a datastep, so that macro should contain something (a statement) that could be resolved inside the datastep.

    If-else won't be written in macro language but in normal datastep language.

    %macro set_istituzionale(set=, varout=);
      if &set/100 = 1 then &varout = 'AP';
    %mend set_istituzionale;
    

    now if you use your call you will have your datastep resolved this way:

    data soff2; 
    set soff;
    %set_istituzionale(set=setcon,varout=set);
    run;
    

    will became:

    data soff2; 
    set soff;
    if setcon/100 = 1 then set = 'AP';
    run;
    

    On your code you were using macro code so your step were resolved internally to the macro boolean statement %if &set/100 resolved were %if setcon/100 where setcon was a string (here we are conditionally speaking with macro language, writing the name of the variable won't catch the value of the variable 'cause it is completelly independent from the datastep).

    You should think to macro language only to something that could write down code for you, for example something like your first try could be used to insert a statement conditionally to the value of a macro variable like:

    data try;
    set want;
    
    %if &macrovar=A %then %do;
    if var1=1 and var2='B' then var3='C';
    %end;
    
    %if &macrovar=B %then %do 
    if var1=2 and var2='A' then var3='D';
    %end;
    
    run;
    
    when the macro variable macrovar will be = A the step will be:
    
    data try;
    set want;
    if var1=1 and var2='B' then var3='C';
    run;
    
    if the macro variable macrovar will be = B the step will be:
    
    data try;
    set want;
    if var1=2 and var2='A' then var3='D';
    run;
    

    But you can use macro code outside from the datastep too, for example to execute a datastep or something else conditionally to the value of a macro variable:

    %if &macrovar=A %then %do;
    
    data try; set tr;
    some code;
    run;
    
    %end;
    
    %else %if &macrovar=B %then %do;
    
    proc sort data=tr out=try nodupkey; by var1; run;
    
    %end;