BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
splt
Calcite | Level 5

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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

 

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

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

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 994 views
  • 2 likes
  • 2 in conversation