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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.