Search code examples
sas-macro

SAS - Macro error within %if expression


I wrote this code:

%macro puntos_evol;

%let hoy = day(today());

%if &hoy = 14 %then %do;

data prueba;

mes_1 = intnx("month",today(),-1, 'E');
format mes_1 date9.;

run;
%end;
%else %do;
data salida;
dato='no valido';
run;
%end;

%mend puntos_evol;

%puntos_evol;

and the error was:

ERROR: Required operator not found in expression: &hoy = 14

but if i put %let hoy = 14;, it works perfect!.


Solution

  • You must enclose each of your attempted function calls by %sysfunc().

    %let works somehow like copy-pasting so it was not "smart" enough to automatically recognize that day() and today() are functions. Try this:

    /* just some useful debug options */
    options SymbolGen MPrint MLogic MExecNote MCompileNote=all;
    dm output 'clear';
    dm log 'clear';
    
    /* the code */
    %macro puntos_evol;
    %let hoy = %sysfunc(day(%sysfunc(today())));
    %PUT hoy=&hoy; /* put the date */
    %if &hoy = 14 %then %do;
        data prueba;
            mes_1 = intnx("month",today(),-1, 'E');
            format mes_1 date9.;
        run;
    %end;
    %else %do;
        data salida;
            dato='no valido';
        run;
    %end;
    %mend puntos_evol;
    
    %puntos_evol;
    

    and the log:

    5299  %macro puntos_evol;
    5300      %let hoy = %sysfunc(day(%sysfunc(today())));
    5301      %PUT hoy=&hoy;
    5302      %if &hoy = 14 %then %do;
    5303          data prueba;
    5304              mes_1 = intnx("month",today(),-1, 'E');
    5305              format mes_1 date9.;
    5306          run;
    5307      %end;
    5308      %else %do;
    5309          data salida;
    5310              dato='no valido';
    5311          run;
    5312      %end;
    5313  %mend puntos_evol;
    NOTE: The macro PUNTOS_EVOL completed compilation without errors.
          22 instructions 560 bytes.
    5314
    5315  %puntos_evol;
    MLOGIC(PUNTOS_EVOL):  Beginning execution.
    NOTE: The macro PUNTOS_EVOL is executing from memory.
          22 instructions 560 bytes.
    MLOGIC(PUNTOS_EVOL):  %LET (variable name is HOY)
    MLOGIC(PUNTOS_EVOL):  %PUT hoy=&hoy
    SYMBOLGEN:  Macro variable HOY resolves to 23
    hoy=23
    SYMBOLGEN:  Macro variable HOY resolves to 23
    MLOGIC(PUNTOS_EVOL):  %IF condition &hoy = 14 is FALSE
    MPRINT(PUNTOS_EVOL):   data salida;
    MPRINT(PUNTOS_EVOL):   dato='no valido';
    MPRINT(PUNTOS_EVOL):   run;
    
    NOTE: The data set WORK.SALIDA has 1 observations and 1 variables.
    NOTE: DATA statement used (Total process time):
          real time           0.00 seconds
          cpu time            0.00 seconds
    
    MLOGIC(PUNTOS_EVOL):  Ending execution.