Hi
I have code like this:
%let year=2019;
proc sql;
select price as price_&year
from my_data;
quit;
With this the name of variable is price_2019 like I wanted. However, is there a way to use that specific macro variable same way to get other years too? I tried to use price_&year-1 but it didn't work. It feels stupid to make own macro for every year.
Since the macro language has only datatype text (which means that "2019 - 1" is not a formula, but just a string of characters), you need to force it to perform calculations by using %EVAL:
price_%eval(&year - 1)
Example:
%let year = 2019;
data test;
price_&year = 1;
price_%eval(&year - 1) = 2;
run;
proc print data=test noobs;
run;
Result:
price_2019 price_2018 1 2
Since the macro language has only datatype text (which means that "2019 - 1" is not a formula, but just a string of characters), you need to force it to perform calculations by using %EVAL:
price_%eval(&year - 1)
Example:
%let year = 2019;
data test;
price_&year = 1;
price_%eval(&year - 1) = 2;
run;
proc print data=test noobs;
run;
Result:
price_2019 price_2018 1 2
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.