BookmarkSubscribeRSS Feed
steve_citi
Calcite | Level 5

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?

21 REPLIES 21
DF
Fluorite | Level 6 DF
Fluorite | Level 6

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

sas_new
Calcite | Level 5

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

ChrisSelley
Calcite | Level 5

Hi Steve

I think you're getting bogged down with ampersands - it's just

PUT &name&i;

Chris

ChrisSelley
Calcite | Level 5

Hi again

sorry (forgot one myself)

PUT &&name&i;

Chris

DF
Fluorite | Level 6 DF
Fluorite | Level 6

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

ChrisSelley
Calcite | Level 5

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;

art297
Opal | Level 21

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

DF
Fluorite | Level 6 DF
Fluorite | Level 6

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.

art297
Opal | Level 21

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;

DF
Fluorite | Level 6 DF
Fluorite | Level 6

Ah, I see it now. My example decodes a further macro variable. I suppose we've understood the ops question slightly differently :).

art297
Opal | Level 21

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;

Tom
Super User Tom
Super User

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

art297
Opal | Level 21

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;

Tom
Super User Tom
Super User

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-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 21 replies
  • 2945 views
  • 1 like
  • 10 in conversation