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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 887 views
  • 0 likes
  • 4 in conversation