BookmarkSubscribeRSS Feed
CharlotteCain
Quartz | Level 8

help request in indirect macro variable references?

 

%let city1=london;

%let city2=frankfurt

%let city3=paris;

%let n=1;

Please correct my understnding:

 &&city&n would resolves to &city1--> this resolves to london. Ok.

Looping the n one by one will get the above city names. Fine

so I suppose && resolves to &

 

What baffles me is use of more than && ampersands and resolving many other vars with multiple ampersands(&&&&) I vaguely remember reading an example on SAS documentation something dog resolves to cat and cat resolves to monkey or something like that. Should any experts lend their time in helping me understand this concept, I will most appreciate it. 

 

Regards,

Charlotte

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

The simple answer to that is this: Anymore than one ampersand in a line of code = badly planned/implemented coding = delete button.  

 

What you have to do with such code, if you can't just delete it, is to go through a loop yourself, take your example:
&&city&n

 

First loop, replace &n with the variable text and demote double & to single:

&city1

 

The same can be said about more:
&&&city&&n&n

 

First loop:

&&city&n1

 

Next:

&city11

 

Still, never a need to go there.

Astounding
PROC Star

You have the first piece, right ... && resolves into &

 

Next, you have to realize that macro language resolves strings from left to right, resolving each piece that it finds.  So:

 

&&city&n  gets resolved as three pieces:  && resolves to &, then city requires no resolution, then &n resolves to 1 (or 2 or 3).

 

So as you noted, &&city&n resolves to &city1 which then gets re-resolved into london.

 

For &&&, the best use I can think of occurs when calling a macro:

 

%let my_list = some list of a thousand variable names;

 

If such a macro variable exists with a thousand names, the macro could be called in a few ways:

 

%macro_call (list=some list of a thousand variable names)

%macro_call (list=&my_list)

%macro_call (list=my_list)

 

The first two macro calls need to compile a long list of values ... slightly longer than the third macro call which only compiles a single word.  Now within %MACRO_CALL, the first two macro calls can refer to &LIST to get the full list of variable names.  But the third macro call, while it compiles faster, has to refer to a more complex expression to get the full list of values:  &&&list

 

This resolves in two pieces:  && becomes &, and &list becomes my_list.  So the expression resolves into &my_list, which then gets re-resolved into the longer list of values.

 

We have even seen a recent question posted on the board here that required &&&&&&, but more than && is infrequent.

 

https://communities.sas.com/t5/Base-SAS-Programming/Macro-Variables-with-Multiple-Ampersands/m-p/409...

 

Ksharp
Super User

It is a little complicated problem. Just as @Reeza said put on as many & as you could , you will get right answer.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 3 replies
  • 1704 views
  • 0 likes
  • 4 in conversation