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