BookmarkSubscribeRSS Feed
ZRick
Obsidian | Level 7

%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?

7 REPLIES 7
shivas
Pyrite | Level 9

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

Ksharp
Super User

Here is process:

    &&  &&var&n

->   &    &var6

->        &var6

Ksharp

Astounding
PROC Star

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.

ZRick
Obsidian | Level 7

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?

manojinpec
Obsidian | Level 7

Hi,

. will tell sas that the macro resolution shoudl end at dot and after that any resolution would be different from the current.

Ksharp
Super User

Here is process:

    &&  &&var.&n

->   &    &var.6

->         &var.6

->           city6

Ksharp

Astounding
PROC Star

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.

sas-innovate-2024.png

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.

 

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
  • 7 replies
  • 1020 views
  • 0 likes
  • 5 in conversation