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

Hi!

 

I need some help to create a dynamic variable, is something like that:

 

%let var1 = 5;

%let var2 = 3;

 

So, i need to call the var1, but i want to make it more dynamic to help in my program i tried this:

 

%let version = 1

%put &'var'&version

-> ERROR

%put &var&version

-> ERROR 'VAR' doesn't exist 

to call var1, but i didn't get success, i tried to use eval too, bu no success 😞

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

To reference a macro variable prefix the name with the & character.  In the %PUT statement you can also &= prefix to have it print the name and the value.

%let var1 = 5;
%let var2 = 3;
%put Value of VAR1 is &var1;
%put &=var1;

A macro variable name cannot include quotes so this cannot work.

%put &'var'&version;

If you want the macro processer to reevaluate the text it generates for more macro triggers use a double &.

%let var1=WANT;
%let version=1;
%put &&ver&version;

It will replace the double & with a single & and then make another pass over the resulting string.

&&var&version -->  &var1 --> WANT

View solution in original post

3 REPLIES 3
ballardw
Super User

Please show what you expect for output.

 

& references a macro variable. &var1 for example. Quotes are not valid as part of a macro variable name which is the complaint from &'var

 

%put &var1.&version;

 

There is a complicated concept called indirect referencing which may be another option

%let var1 = 5;

%let var2 = 3;
%let version = 1;

%put &&var&version.;

When multiple && are encountered the SAS, in effect "holds" on of the & and resolves part of the rest so var&version becomes Var1 and then the &var1 is implemented.

 

But if you don't know enough of the macro language to understand why &'var' was a bad idea you are very likely not ready for this as as resolving such takes a lot of practice. (and often locked up SAS sessions from bad results. Save code often.)

Tom
Super User Tom
Super User

To reference a macro variable prefix the name with the & character.  In the %PUT statement you can also &= prefix to have it print the name and the value.

%let var1 = 5;
%let var2 = 3;
%put Value of VAR1 is &var1;
%put &=var1;

A macro variable name cannot include quotes so this cannot work.

%put &'var'&version;

If you want the macro processer to reevaluate the text it generates for more macro triggers use a double &.

%let var1=WANT;
%let version=1;
%put &&ver&version;

It will replace the double & with a single & and then make another pass over the resulting string.

&&var&version -->  &var1 --> WANT

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 626 views
  • 1 like
  • 4 in conversation