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

 

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
  • 962 views
  • 2 likes
  • 2 in conversation