简体   繁体   中英

Referring to a macro variable already defined

I was going through a SAS code and found the following ways of referencing a macro variable.

  1. & variable_name .
  2. & variable_name

I know that the first way of referencing is the normal way of how we refer to a defined macro variable in sas.

But, could you please explain how does the second way(without period after the macro variable name) is different from the first way

When you reference a macro variable, the & sign signals a macro variable reference is beginning. The macro processor also needs to determine the end of the macro variable reference.

In most cases, the macro reference is ended by either white space or a semicolon.

38   %let x=Hello;
39
40   %put &x;   *semicolon ends the reference;
Hello
41
42   %put &x World; *white space ends the reference;
Hello World

Macro variable names can consist of letters, numbers, and underscores. Any symbol which cannot be part of a macro variable will end a reference. For example a question mark:

44   %put &x?; *Question mark ends the reference;
Hello?

Now, suppose you want to generate the string HelloWorld, with no space between Hello and World:

46   %put &xWorld;
WARNING: Apparent symbolic reference XWORLD not resolved.
&xWorld

That didn't work. The macro processor saw a reference to a macro variable named xWorld, and there is no such macro variable. So we need a way to tell the macro processor that the macro variable reference ends after the x. We can add a period after the x, to say that "macro variable reference ends here."

47   %put &x.World;
HelloWorld

Note that importantly, the period we typed is not shown in the text written to the log. The macro processor "eats" the period when it uses it as the end of the macro variable reference.

This means that in cases where you want to have a period after a macro variable reference, you must type two periods. The first is eaten by the macro processor. The usual example of this is with a libref:

48   %let mylib=sashelp;
49
50   proc print data=&mylib.class;run;
ERROR: File WORK.SASHELPCLASS.DATA does not exist.

NOTE: The SAS System stopped processing this step because of errors.
51
52   proc print data=&mylib..class;run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.

Some people like to always use periods after macro variable references, as a coding style. Personally, I only uses the period when it is necessary to specify the end of a macro variables reference.

It is the short way to reference a variable. However it makes a difference when concatenating output:

%let var = My name ;

%put &var.is Paul;
// Output: My name is Paul.

%put &varis Paul;
// Output: WARNING: Apparent symbolic reference VARIS not resolved.
// &varis Paul

Another example where you definitely need a . :

%let lib = sashelp;
data temp;
  set &lib..class;
run;

I would recommend to always use the variant &variable. . Because it is clear where the variable ends.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM