1
votes

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.

3

3 Answers

4
votes

%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.;
2
votes

You need to use SYSFUNC() to use functions in macro syntax. SUBSTR will work as well.

%let year=2015;
%let index = %sysfunc(substr(&year, 3, 2));

%put &year;
%put &index.;

%EVAL() works for mathematical calculations, such as:

%let next_year = %eval(&year + 1);
%let next_year_index = %eval(&index + 1);

%put Next Year: &next_year.;
%put Next Year Index: &next_year_index;
0
votes

Reeza provided a nice solution which is absolutely correct. I'd expand it by two comments:
1. You can find functions like %SUBSTR in SAS, but those operate on macrovariables only; they do not replace %sysfunc(substr()), on the contrary.
2. It is useful to take a minute and go through the list of macrofunctions, i.e. those that begin with percent sign: you will see that apart from %EVAL(), there is also %SYSEVALF(). The list is here.