Discover More Tips and Techniques on This Blog

Resolving and Using &&var&i Macro Variables

Here are the 2 important questions always comes up in our minds,(& vs && vs &&& and single dot and double dots) when we are dealing with macros for the first time and here are the answers for them.

I did find a very good regarding the above topics in the one of the SAS forums and IAN WHITLOCK explained it very clear.


or
when to use &,&&,and &&&,how do we distinguish?
&MACVAR references a macro variable. The rule is that the scanner reads from left to right. Two ampersands resolve to one and scanner continues. If the resulting expression has ampersands then that expression is scanned again. So &&x resolves to &x resolves to value of X
&&&x resolves to & followed by the value of X which then resolves to the value of the variable indicated.

If you are familiar with TSO CLISTS (even if you are not), they are very similar to SAS macro. SAS was originally based on PL1, so both SAS syntax and SAS macro are similar in some ways to
PL1 and PL1 macro.

what is the diff between Single dot and double dot(eg. &chech.> and &check..)

&CHECK and &CHECK. are the same thing. If the scanner finds a . that ends the macro variable reference, then the scanner eats the. and ends the reference. If there is no current macro variable then a . is a . So if &X is MYFMT then &X.. is MYFMT.

Test example: what is &&&X..5 when X has the value V and V has the value TEST?

&&&X..5 -> &V.5 -> TEST5

Test: Consider &&&...&X where the dots indicate there are a total of 15 ampersands preceding the X. Write a sequence of %LET statements followed by

%put &&&...&X ;

so that this causes text to be written on the log without any notes, warnings. or errors. One, three, and 15 are interesting sequences of amprsands. Find one number in between 3 and 15 and next oone after 15 that are interesting for the same reason. How many dots would be needed to make the letter S immeadiately follow the value of &&&...&X?

You may also need to check the SIGI paper to understand the multiple ampersands concept in detail:

Example: ....

%let dsn=study;
%let n=05;
%let dsn05=Client;
%put &dsn&n; *Resolves to study05;
%put &dsn.&n; *Resolves to study05;
%put &dsn..&n; *Resolves to study.05;

*Multi Ampersands Concept; %put &&dsn&n;
* First Scan Resolves to &dsn05;
* Second Scan Resolves to Client;

%put &&&dsn&n;
* First Scan Resolves to &client05;
* Second Scan Resolves to &Client05; *with an error message in the log file....;


*Log file; 57 %put &&&dsn&n;

WARNING: Apparent symbolic reference STUDY05 not resolved.
&study05

/*Because.. the most common mistake is that.. */
/*We assume that macro variable resolution process proceeds from right to left...*/
/*Infact it isn't. */

The bottom line is ...
/**For some reason.... the &&&dsn&n is taken as && &dsn&n, which resolves to &client05.*/
/*This macro variable will not get resolved, because &client5. macro variable isn't there in the symbol table.; */
Macrovariables and its resolution:
Example:
%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;

Answer:


%put &one; two
%put &&one; two
%put &&&one; three
%put &&&&one; two
%put &&&&&one; three
%put &&&&&&one; three
%put &&&&&&&one; Check
%put &&&&&&&&one; two

%put &&&&&&&&&one; three
%put &&&&&&&&&&one; three


Resolving and Using &&var&i Macro Variables

4 comments:

  1. thank u so much sarath, now i got clear of the multiple &..... Thank U so much

    ReplyDelete
  2. Sometimes SAS is just crazy, at least it's never boring though...

    %let six = seven;
    %let five = six;
    %let four = five;
    %let three = four;
    %let two = three;
    %let one = two;

    %macro test;
    %put &one; /* 1 '&': -> two */
    %put &&&one; /* 3 '&'s: -> &two -> three */
    %put &&&&&&&one; /* 7 '&'s: -> &&&two -> &three -> four */
    %put &&&&&&&&&&&&&&&one; /* 15 '&'s: &&&&&&&two -> &&&three -> &four -> five */
    %put &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&one; /* 31 '&'s: -> six */
    %put &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&one; /* 63 '&'s: -> seven */
    %mend;

    %test;

    ReplyDelete
  3. Hello sarath,
    After reading your post for SAS Macro variables,I have got cleared better relate to & vs && vs &&& and single dot and double dots and difference between them,Thanks for posting such informative post

    ReplyDelete
  4. &&&dsn&n is broken like this [or any thing multiple ampersands] : starting from left, taking ampersands in pairs. also breaking at where another variable is encountered. Applying the same again and again.
    |&&||&dsn||&n|

    now each pure pair of ampersands resolves to a single ampersand. a single variable is kept as it is, &var resolves to assigned value or throws up an error.

    so. &&&dsn&n = |&&||&dsn||&n| = |&| |study| |05| =&study05= error, because there is no study05 macro var. It does not resolve to &client05 as you claim in your example.

    ReplyDelete

Disclosure:

In the spirit of transparency and innovation, I want to share that some of the content on this blog is generated with the assistance of ChatGPT, an AI language model developed by OpenAI. While I use this tool to help brainstorm ideas and draft content, every post is carefully reviewed, edited, and personalized by me to ensure it aligns with my voice, values, and the needs of my readers. My goal is to provide you with accurate, valuable, and engaging content, and I believe that using AI as a creative aid helps achieve that. If you have any questions or feedback about this approach, feel free to reach out. Your trust and satisfaction are my top priorities.