Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sas MACRO ampersand

%let test = one;
%let one = two;

%put &test;
%put &&test;
%put &&&test;
%put &&&&test;
%put &&&&&test;

Well. I'm TOTALLY BEATEN by these ampersands. I don't understand why they need SO MANY ampersands before a macro variable. Is there any trick to master the usage of ampersand? BTW, what are the five results, correspondingly?

like image 947
zhuoer Avatar asked Apr 07 '14 04:04

zhuoer


People also ask

How do you use ampersand in SAS?

SAS | How to read character using Ampersand(&) We can use ampersand (&) to notify SAS to read the variable until there are two or more spaces encounter as a delimiter. This technique is always useful when the variable contains two or more words. Example 1: There are 2 spaces before 25, 32 and 30 in example code below.

How do you resolve a macro variable in SAS?

You can use these references anywhere in a SAS program. To resolve a macro variable reference that occurs within a literal string, enclose the string in double quotation marks. Macro variable references that are enclosed in single quotation marks are not resolved.

What is %macro in SAS?

Macro is a group of SAS statements that is referred by a name and to use it in program anywhere, using that name. It starts with a %MACRO statement and ends with %MEND statement.

What does %Mend do in SAS?

%MEND macro-name; The macro-name specified in the %MEND statement must match the macro-name specified in the %MACRO statement. Note: While specifying the macro-name in the %MEND statement is not required, it is recommended. It makes matching %MACRO and %MEND statements while debugging easier.


1 Answers

With a single set of ampersands, what you get is pretty boring; after one, odd number of ampersands leads to resolving twice, even number of ampersands resolves once. So you use 1 ampersand to resolve once and 3 ampersands to resolve twice, unless you have stock in the company that owns rights to the ampersand.

More interesting is the following test, which shows why even numbers of ampersands have value:

%let test = one;
%let testtwo = one;
%let one = two;
%let two=three;

%put &test&one;
%put &&test&one;
%put &&&test&one;
%put &&&&test&one;
%put &&&&&test&one;
%put &&&&&&test&one;

Basically, each pass through, SAS does the following:

  • Resolve any single ampersand plus text to a macro variable reference.
  • Resolve any pairs of ampersands to one ampersand.

Those are done simultaneously and iteratively until all ampersands are gone, and each result is kept for the next iteration and does not affect the current iteration. So, &test&one becomes onetwo because &test-> one and &one -> two. Steps for the remaining:

  • &&test&one -> &testtwo -> one. &&|test|&one. The double && before test becomes &, test remains, and &one resolves to two. That leaves &testtwo for the second pass which resolves to one.
  • &&&test&one -> &onetwo -> does not resolve. &&|&test|&one -> &|one|two -> DNR.
  • &&&&test&one -> &&testtwo -> &testtwo -> one. &&|&&|test|&one -> &&|testtwo -> &testtwo -> one. Two pairs each resolve down to one, making one pair, which then resolves to one, which leaves &testtwo to resolve.
  • &&&&&test&one is similar to three ampersand case, but with one extra pair.
  • &&&&&&test&one resolves to &&&testtwo resolves to &one resolves to two. &&|&&|&&|test|&one -> &&|&testtwo -> &one -> two. The odd number of pairs means we get one more set of resolves.

At the end of the day, what you need to remember:

  • 1 ampersand resolves the macro variable once and that's it.
  • 2 ampersands is useful for composite macro variables, ie, a prefix plus a macro-driven suffix (&&prefix&suffix).
  • 3 ampersands is useful for going two deep in resolving a single macro variable (&&&var -> &var2 -> var3).
  • 6 ampersands is useful for resolving a two-deep composite macro variable (ie, combining 2 and 3) ([&prefix=var, &suffix=2] &&&&&&prefix&suffix -> &&&var2 -> &var3 -> 4).

Beyond that, 4 or more (other than 6) are useful only for particularly complex combinations; the extra levels would be used to delay resolution until particular times.

like image 192
Joe Avatar answered Sep 24 '22 18:09

Joe