## Quick topic to understand macro variable

Regular Contributor
Posts: 222

# Quick topic to understand macro variable

Reference to Resolving and Using &&var and Referencing Macro Variables Indirectly

Hi mates,

I've beeing looking for an analogy to understand the enviroment of macro call's.

The analogy to understand is tricky, because you need to know what is the first value that the macro will solve to. Although, there are some tips that might help us to understand and solve then quickly.

As we all know, the best way to understand is practicing and testing, but if you are going to take a SAS Advance Programming Certification you may know how the macro works.

The code below represent the exactly what i've beeing looking for:

``````%let one=two;
%let two=three;
%let three=Check;

%put &one;
%put &&one;
%put &&&one;
%put &&&&one;
%put &&&&&one;
%put &&&&&&one;
%put &&&&&&&one;
%put &&&&&&&&one;
%put &&&&&&&&&one;
%put &&&&&&&&&&one;``````

The values printed to the SAS Log:

```21         %put &one;
two
22         %put &&one;
two
23         %put &&&one;
three
24         %put &&&&one;
two
25         %put &&&&&one;
three
26         %put &&&&&&one;
three
27         %put &&&&&&&one;
Check
28         %put &&&&&&&&one;
two
29         %put &&&&&&&&&one;
three
30         %put &&&&&&&&&&one;```

But how SAS interprets this and reaches the certain values ?

1.  When the quantity of "&" is 1 or 2 or 4 or 8 or 16 or 32 or 64 or ...,

and so on, the result will always be the value of the first macro

2.  The second macro, the analogy is: take off the last exponential

quantity, it will left  1 or 2 "&", it solves to "&one > &two > three"

3.  The third macro , the analogy is: Same analogy of the second,

but if rests 3 "&" then the value will always be the value of third

Table for the analogy

All these content were tested in a SAS enviroment

If you have any other idea please show me, or if you have another analogy please explain.

The table for the analogy is attached

Hope this helps,

Rodrigo Dartibali Elias

Super User
Posts: 6,785

## Re: Quick topic to understand macro variable

Rodrigo,

There are only a small number of rules you need to know.

Macro language resolves strings from left to right.

Two ampersands resolve into one.

If any ampersands remain, macro language repeats the resolution process.

So to take just one of your examples:

&&&&&&one gets treated as if it were four pieces:  && && && one

Since two ampersands resolve into one, this becomes &&&one

Ampersands remain, so resolution continues, and &&&one gets treated as if it were two pieces:  && &one

Two ampersands resolve into one, so this becomes &two

Ampersands remain, so this gets resolved and becomes three

The same rules will explain all of your results.

Regular Contributor
Posts: 222

## Re: Quick topic to understand macro variable

[ Edited ]

But if you use the quantity of ampersands in (1,2,4,8,16,32,64,...) it will always be the first value, right ?

Please make a test. And you will see it. Always beginning of number 1 and multiply by 2 => 1*2=2*2=4*2=8*2=16=...

Thats what i mean for the first macro.

The other rules works like you said.

But try to make the same analogy i've made for all other variables.

You said &&&&&&one gets treated as if it were four pieces:  && && && one = Ok, but the last result number multiplied by 2 was 4 before the quantity of 6 ampersands. You take out 4 ampersands, left 2 ampersands put these in the first case >> &one = && &one

The result of &&&one is three, same as &&&&&&one.

But if only left 1 ampersands or &&&&&one, the result of &one = &two then solves to three again.

I know the analogy is this like you said, but i tried to find another one for a better understanding, could you please check if this is right ?

Thanks

Super User
Posts: 6,785

## Re: Quick topic to understand macro variable

While it's true that 1, 2, 4, 8, 16 ... ampersands are equivalent, that doesn't mean you can just remove 4 ampersands any time you would like.  All the existing pieces of &&&&&&one must be evaluated from left to right.

Resolving each piece generates &&&one

Since ampersands remain, macro language must now re-resolve &&&one

Resolving each piece generates &two

Again, ampersands remain, so macro language now re-resolves &two and comes up with three.

Regular Contributor
Posts: 222

## Re: Quick topic to understand macro variable

Not remove four ampersands, but remove the quantity of the last exponential.

Just like:  If the last exponential was 2 then in the next remove 2 =>  &&one - &&&one => &two > three

If the last exponential is 4 ampersands: remove 4 &&&&&one => &one => &two => three

Your analogy is the right one, but i tried to understand by another way.

Please use my analogy and check out.

That's what i mean

Super User
Posts: 8,127

## Re: Quick topic to understand macro variable

Powers of two will reduce to the same as just one &.

For the intervening numbers that are not powers of 2 it is probably harder to see a pattern.

Try this version:

``````%let level1=level2;
%let level2=level3;
%let level3=level4;
%let level4=level5;
%put 1=&level1 2=&&level1 4=&&&&level1 8=&&&&&&&&level1;
%put 3=&&&level1 5=&&&&&level1 7=&&&&&&level1 9=&&&&&&&&&level1 10=&&&&&&&&&&level1 12=&&&&&&&&&&&&level1 ;
%put 11=&&&&&&&&&&&level1 13=&&&&&&&&&&&&&level1 14=&&&&&&&&&&&&&&level1;
%put 15=&&&&&&&&&&&&&&&level1;``````

Results:

``````1=level2 2=level2 4=level2 8=level2
3=level3 5=level3 7=level3 9=level3 10=level3 12=level3
11=level4 13=level4 14=level4
15=level5
``````

Regular Contributor
Posts: 222

## Re: Quick topic to understand macro variable

Oh thanks Tom, That's easier.

Tks

Discussion stats
• 6 replies
• 473 views
• 2 likes
• 3 in conversation