If I have a macro variable which resolves to a variable name, how do I print the value of the variable?
For example, if my macro variable is &&name&i. and when i=2 then &name2 resolves to acct_num, then how do I print the value of acct_num.
put "&&name&i."; only prints the literal acct_num, not its value.
Is there any way to do this?
Assuming I've understood correctly, you should drop the quotes and add an extra ampersand. The quotes will force it to show the variable name instead of decoding it.
%let name=var;
%let num=2;
%let var1=account;
%let var2=acct_num;
data z;
acct_num = "asdf";
put &&&name.&num.;
run;
My log then shows:
68 data z;
69 acct_num = "asdf";
70
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable NAME resolves to var
SYMBOLGEN: Macro variable NUM resolves to 2
SYMBOLGEN: Macro variable VAR2 resolves to acct_num
71 put &&&name.&num.;
72 run;
NOTE: The data set WORK.Z has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
Hi Steve,
I think you can do this way...
%let name=var;
%let num=2;
%let var1=account;
%let var2=acct_num;
%put &&&name#
Thanks,
Kuthubudeen
Hi Steve
I think you're getting bogged down with ampersands - it's just
PUT &name&i;
Chris
Hi again
sorry (forgot one myself)
PUT &&name&i;
Chris
Hmm. It's strange - this only works for me when I use the triple ampersand as per my example. I wonder - is there differences in macro resolution between versions (I'm using 9.1.3)? That or I'm missing something!!
I'm using 9.1.3 I just used
%let i=2;
%let name2=acct_num;
data x;
acct_num=12345;
put &&name&i;
run;
%let i=2;
%let name2=acct_num;
data x;
acct_num=12345;
put &&name&i;
run;
resolves correctly on 9.3 as well. As woud two or more ampersands in this case.
I just tried your code Chris and it works as you said.
The difference to mine is that I included a . after the macro names. When I add the same to your example I get an error, whereas it works with the extra ampersand.
Curious... I didn't expect that.
I wouldn't think that the period has any adverse effect. It doesn't on 9.3. Each of the following produces the same (desired) result:
%let i=2;
%let name2=acct_num;
data x;
acct_num=12345;
put &&name&i.;
run;
data x;
acct_num=12345;
put &&&name&i.;
run;
data x;
acct_num=12345;
put &&&&name&i.;
run;
Ah, I see it now. My example decodes a further macro variable. I suppose we've understood the ops question slightly differently :).
No, actually, you were right! I was always taught that it NEVER hurts to ALWAYS end one's macro variables with a period. Obviously, there are some exceptions to that rule:
NONE of the following will work:
%let i=2;
%let name2=acct_num;
data x;
acct_num=12345;
put &&name.&i.;
run;
data x;
acct_num=12345;
put &&&name.&i.;
run;
data x;
acct_num=12345;
put &&&&name.&i.;
run;
You did not define a macro variable named NAME. So &name. will generate an error.
You can check how the &&'s are being combined by turning on SYMBOLGEN.
64 %let i=2;
65 %let name2=acct_num;
66 %let name=name;
72 %put 4-> &&&&name&i;
SYMBOLGEN: && resolves to &.
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 2
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable NAME2 resolves to acct_num
4-> acct_num
82 %put 4-> &&&&name.&i;
SYMBOLGEN: && resolves to &.
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 2
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable NAME resolves to name
4-> name2
Tom, You've ALMOST regained my faith in what I was taught, but there is still a discrepany I'd like someone to explain:
%let i=2;
%let name2=acct_num;
%let name=name;
/* All three of the following work */
data x;
acct_num=12345;
put &&name&i.;
run;
data x;
acct_num=12345;
put &&&name&i.;
run;
data x;
acct_num=12345;
put &&&&name&i.;
run;
/* However, below, only the 3 ampersand macro variable resolveds correctly*/
data x;
acct_num=12345;
put &&name&i.;
run;
data x;
acct_num=12345;
put &&&name&i.;
run;
data x;
acct_num=12345;
put &&&&name&i.;
run;
Art -
The period changes which pass will actually attempt to evaluate the macro variable value.
So in examples below using 2,3 and 4 leading &'s you see the effect in the symbolgen logic.
For 2 we have &&name.&i first tranlating into &name.2 which then translate into <value of &name>2.
But for 3 we have &&&name.&i first translating in &<value of &name>2. So when we set name=name we get &name2 and so the value of name2.
-Tom
80 %put 2-> &&name.&i;
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 2
SYMBOLGEN: Macro variable NAME resolves to name
2-> name2
81 %put 3-> &&&name.&i;
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable NAME resolves to name
SYMBOLGEN: Macro variable I resolves to 2
SYMBOLGEN: Macro variable NAME2 resolves to acct_num
3-> acct_num
82 %put 4-> &&&&name.&i;
SYMBOLGEN: && resolves to &.
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 2
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable NAME resolves to name
4-> name2
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.