%put &&&&&&&&&&&&&&Asia&X&&y&&&&z;
Here are "middle" steps, each time number of "& group" is doubled:
/*
%put &&&& &&&& && && &&Asia &X &&y &&&&z;
*/
%put 1) &&Asia&X;
%put 2) &&&&&&Asia&X&&y;
%put 3) &&&&&&&&&&&&&&Asia&X&&y&&&&z;
All the best
Bart
When every there are two &'s next to each other they resolve to one & and tell the macro processor to rescan the resulting string.
You can see a lot about what is happening by turn on the SYMBOLGEN option (only while debugging, if you leave it on for production runs you can get extremely large and unreadable SAS logs).
So for
%put &&Asia&I;
The first pass will resolve the && and &I and end up with:
&Asia1
But for
%put &&Asia&I&y;
The first pass will resolve &y also so you end up with:
&Asia12
But you did not define a macro variable with that name.
To get India as the result you could write :
%put &&bulli&y;
So to generate India you could replace BULLI in that with &&ASIA&I, but you need this newly inserted value to be calculated before the && and the &y. So you need to double up the existing &'s so that on the first pass they will get replaced with single &'s.
%put &&&&&&Asia&I&&y;
Results:
29 %put &&&&&&Asia&I&&y; SYMBOLGEN: && resolves to &. SYMBOLGEN: && resolves to &. SYMBOLGEN: && resolves to &. SYMBOLGEN: Macro variable I resolves to 1 SYMBOLGEN: && resolves to &. SYMBOLGEN: && resolves to &. SYMBOLGEN: Macro variable ASIA1 resolves to bulli SYMBOLGEN: Macro variable Y resolves to 2 SYMBOLGEN: Macro variable BULLI2 resolves to India India
Personally I find it much easier to build up to the answer in multiple steps instead of trying to figure out how many &'s are needed.
%let step1=&&ASIA&i;
%let step2=&step1.&y;
%put Country=&&&step2;
Result:
34 %let step1=&&ASIA&i; SYMBOLGEN: && resolves to &. SYMBOLGEN: Macro variable I resolves to 1 SYMBOLGEN: Macro variable ASIA1 resolves to bulli 35 %put &=step1; SYMBOLGEN: Macro variable STEP1 resolves to bulli STEP1=bulli 36 %let step2=&step1.&y; SYMBOLGEN: Macro variable STEP1 resolves to bulli SYMBOLGEN: Macro variable Y resolves to 2 37 %put &=step2; SYMBOLGEN: Macro variable STEP2 resolves to bulli2 STEP2=bulli2 38 %put Country=&&&step2; SYMBOLGEN: && resolves to &. SYMBOLGEN: Macro variable STEP2 resolves to bulli2 SYMBOLGEN: Macro variable BULLI2 resolves to India Country=India
In general if you are doing this many levels of redirection in macro logic you are probably approaching the problem the wrong way. Use SAS code to work with data and macro code to just help generate repetitive code.
I'm assuming you don't want to refer to &i at all, since you have no such macro variable.
Other than that, I'm assuming that if you were to use &x instead of &i, you are asking the right question that you would like answered.
To get &&Asia&x&y to resolve to India, some modification is necessary. But it could be with a little less effort:
%let X=1;
%let y=2;
%let z=3;
%let Asia=Harish;
%let Asia1=bulli;
%let bulli2=India;
%let india3=Bharat;
%put &&&&&&Asia&x..&y;
To understand why, you need to know four rules for resolving a complex macro expression:
From left to right, the pieces within &&&&&&Asia&x..&y are:
&& ==> &
&& ==> &
&& ==> &
Asia ==> Asia
&x. ==> 1
. ==> .
&y ==> 2
That means the entire expression resolves to &&&Asia1.2
Since that expression contains three ampersands, it gets resolved again. The pieces are:
&& ==> &
&Asia1. ==> billi
2 ==> 2
That means the expression now becomes &billi2
That gets resolved to India.
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.
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.