- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%let y=2;
%let z=3;
%let Asia=Harish;
%let Asia1=bulli;
%let bulli2=India;
%let india3=Bharat;
%put &&Asia&I;------- it gives bulli
%put &&Asia&I&y;----- it doesn't give India
How resolve above macro variable &&Asia&I&y
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%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
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug
"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings
SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I'm sure you know the rule of two ampersands i.e. whenever macro processor see && it "glues" it to & and do rescan. In our case we have 3 levels of "inception" so we need to ensure proper number ampersands to trigger 3 rescanning.
Let no brackets means resolution in the first scan, then let () marks first "glue-ing", [] marks the second, and {} third. The following "bracketing" is evaluated:
/*
%put {[(&&)(&&)] [(&&)(&&)]} [(&&) (&&)] (&&)Asia &X (&&)y [(&&)(&&)]z;
*/
All the best
Bart
P.S. if you need me to add some extra clarifications you will have to wait few hours until I get to my laptop, since I'm using mobile to write this replay and it's very hard ;-D ;-D
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug
"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings
SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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:
- && resolves to &
- A dot can be used to delimit the name of a macro variable. So &x and &x. both refer to the macro variable X.
- SAS resolves each piece of the expression from left to right
- If the resolution generates more macro language (such && generating &), macro language re-resolves the entire 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.