DATA Step, Macro, Functions and more

why multiple ampersand resolve to this?

Reply
Frequent Contributor
Posts: 133

why multiple ampersand resolve to this?

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

Super Contributor
Posts: 349

Re: why multiple ampersand resolve to this?

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

Super User
Posts: 10,023

Re: why multiple ampersand resolve to this?

Here is process:

    &&  &&var&n

->   &    &var6

->        &var6

Ksharp

Super User
Posts: 5,503

Re: why multiple ampersand resolve to this?

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.

Frequent Contributor
Posts: 133

Re: why multiple ampersand resolve to this?

Posted in reply to Astounding

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?

Frequent Contributor
Posts: 139

Re: why multiple ampersand resolve to this?

Hi,

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

Super User
Posts: 10,023

Re: why multiple ampersand resolve to this?

Here is process:

    &&  &&var.&n

->   &    &var.6

->         &var.6

->           city6

Ksharp

Super User
Posts: 5,503

Re: why multiple ampersand resolve to this?

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.

Ask a Question
Discussion stats
  • 7 replies
  • 338 views
  • 0 likes
  • 5 in conversation