%let var=city;
%let n=6;
%put &&&&var&n;
after 4 ampersand, and resolve to &var6, why &var?
I mean it should be &city6.right?
Hi,
Two &s (&&) resolve to one &
Four or more &s are resolved from left to right.
So if you want &city6 then you need to use &&&&&var&n.
Thanks,
Shiva
Here is process:
&& &&var&n
-> & &var6
-> &var6
Ksharp
ZRick,
If you are looking to get &city6, while 4 ampersands work, 3 would have been enough.
In &&&var&n:
&& becomes &
&var becomes city
&n becomes 6
So the whole thing becomes &city6
Many times, extra ampersands don't hurt but they merely require additional resolution. For example, 6 ampersands would also have worked if you add a delimiter (see below).
In &&&&&&var.&n
&& becomes &
&& becomes &
&& becomes &
var. remains var.
&n becomes 6
So after one set of resolutions, this becomes &&&var.6
In the next set of resolutions,
&& becomes &
&var. becomes city
6 remains 6
So after the second set of resolutions, this becomes &city6
As other posters mentioned, move from left to right, and resolve each text string. Then re-resolve if necessary.
Good luck.
Astounding,
Can you explain why adding period then whole result changed? if I put %put &&&&var.&n; now it turns to city6, why is that?
Hi,
. will tell sas that the macro resolution shoudl end at dot and after that any resolution would be different from the current.
Here is process:
&& &&var.&n
-> & &var.6
-> &var.6
-> city6
Ksharp
That's absolutely correct. To put it in words ...
With a dot, the expression resolves as Ksharp indicated. Within &var.6, the dot delimits the name of the macro variable. It tells macro language that VAR is the name of the macro variable, and 6 is just text to be appended to &VAR.
Without a dot, the expression would resolve to &var6. How would macro language know to look for &VAR, rather than &VAR6? It doesn't. This would trigger a search for &VAR6. Since there is no such macro variable, it would generate an error.
In general, keep asking! Speaking for myself only, I'm more than willing to help when I see that you are putting in the work and trying hard to learn.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.