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 😞
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
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.)
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
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.