DATA Step, Macro, Functions and more

help request in understanding indirect macro variable references?

Reply
Frequent Contributor
Posts: 139

help request in understanding indirect macro variable references?

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

Super User
Super User
Posts: 9,193

Re: help request in understanding indirect macro variable references?

Posted in reply to CharlotteCain

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.

Super User
Posts: 6,526

Re: help request in understanding indirect macro variable references?

[ Edited ]
Posted in reply to CharlotteCain

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

 

Super User
Posts: 10,609

Re: help request in understanding indirect macro variable references?

Posted in reply to CharlotteCain

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

Ask a Question
Discussion stats
  • 3 replies
  • 103 views
  • 0 likes
  • 4 in conversation