Search code examples
sassas-macro

Simple calculations in Macro Variables - any way to use the MOD(,) function?


Is there a clever way to use the MOD(,) function while defining Macro Variables?

For example consider this:

%LET year=2015;
%LET dec = %EVAL(mod(&year.,100));

Where dec will just contain the last two digits of the year. This will not work (the same with SYSEVAL), since the %EVAL function cannot access the MOD function properly.

I don't want to include a DATA STEP, but the result should actually be something like this:

data _null_;
input_year=2015;
input_dec =mod(input_year,100);
%LET year=input_year;
%LET dec = input_dec;
put &dec. 'and ' &year.;
run;

The problem here is that the Macro Variables cannot be called outside of the DATA STEP (maybe I did something wrong? Even the global option doesn't do the trick).

Some context:

In an existing autoexec file the user has to change some input, depending on the year, all together 3 Macro Variables have to be adapted (year, dec, and the follow up year). My idea was to infer the other changes from just one input, the year.


Solution

  • %eval and %sysevalf are used to do calculations on numbers, outside of a data step. When using functions then you need %sysfunc

    %LET year=2015;
    %LET dec = %sysfunc(mod(&year.,100));
    
    %put &=dec. and &=year.;