Previous Page | Next Page

Macro Variables

Using Macro Variables


Macro Variable Reference

After a macro variable is created, you typically use the variable by referencing it with an ampersand preceding its name (&variable-name), which is called a macro variable reference. These references perform symbolic substitutions when they resolve to their value. 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. Compare the following statements that assign a value to macro variable DSN and use it in a TITLE statement:

%let dsn=Newdata;
title1 "Contents of Data Set &dsn";
title2 'Contents of Data Set &dsn';

In the first TITLE statement, the macro processor resolves the reference by replacing &DSN with the value of macro variable DSN. In the second TITLE statement, the value for DSN does not replace &DSN. SAS sees the following statements:

TITLE1 "Contents of Data Set Newdata";
TITLE2 'Contents of Data Set &dsn';

You can refer to a macro variable as many times as you need to in a SAS program. The value remains constant until you change it. For example, this program refers to macro variable DSN twice:

%let dsn=Newdata;
data temp;
   set &dsn;
   if age>=20;
run;

proc print;
   title "Subset of Data Set &dsn";
run;

Each time the reference &DSN appears, the macro processor replaces it with Newdata . SAS sees the following statements:

DATA TEMP;
      SET NEWDATA;
      IF AGE>=20;
   RUN;

   PROC PRINT;
      TITLE "Subset of Data Set NewData";
   RUN;

Note:   If you reference a macro variable that does not exist, a warning message is printed in the SAS log. For example, if macro variable JERRY is misspelled as JERY, the following produces an unexpected result:

%let jerry=student;
data temp;
  x="produced by &jery";
run;

This code produces the following message:

WARNING:  Apparent symbolic reference JERY not resolved.

  [cautionend]


Combining Macro Variable References with Text

It is often useful to place a macro variable reference next to leading or trailing text (for example, DATA=PERSNL&YR.EMPLOYES, where &YR contains two characters for a year), or to reference adjacent variables (for example, &MONTH&YR). You can reuse the same text in several places or to reuse a program because you can change values for each use.

To reuse the same text in several places, you can write a program with macro variable references representing the common elements. You can change all the locations with a single %LET statement, as shown:

%let name=sales;
   data new&name;
      set save.&name;
      more SAS statements
      if units>100;
   run;

After macro variable resolution, SAS sees these statements:

DATA NEWSALES;
      SET SAVE.SALES;
      more SAS statements
      IF UNITS>100;
   RUN;

Notice that macro variable references do not require the concatenation operator as the DATA step does. SAS forms the resulting words automatically.


Delimiting Macro Variable Names within Text

Sometimes when you use a macro variable reference as a prefix, the reference does not resolve as you expect if you simply concatenate it. Instead, you might need to delimit the reference by adding a period to the end of it.

A period immediately following a macro variable reference acts as a delimiter. That is, a period at the end of a reference forces the macro processor to recognize the end of the reference. The period does not appear in the resulting text.

Continuing with the example above, suppose that you need another DATA step that uses the names SALES1, SALES2, and INSALES.TEMP. You might add the following step to the program:

/*  first attempt to add suffixes--incorrect  */
data &name1 &name2;
   set in&name.temp;
run;

After macro variable resolution, SAS sees these statements:

DATA &NAME1 &NAME2;
   SET INSALESTEMP;
RUN;

None of the macro variable references have resolved as you intended. The macro processor issues warning messages, and SAS issues syntax error messages. Why?

Because NAME1 and NAME2 are valid SAS names, the macro processor searches for those macro variables rather than for NAME, and the references pass into the DATA statement without resolution.

In a macro variable reference, the word scanner recognizes that a macro variable name has ended when it encounters a character that is not used in a SAS name. However, you can use a period ( . ) as a delimiter for a macro variable reference. For example, to cause the macro processor to recognize the end of the word NAME in this example, use a period as a delimiter between &NAME and the suffix:

/*  correct version  */
data &name.1 &name.2;

SAS now sees this statement:

DATA SALES1 SALES2;


Creating a Period to Follow Resolved Text

Sometimes you need a period to follow the text resolved by the macro processor. For example, a two-level data set name needs to include a period between the libref and data set name.

When the character following a macro variable reference is a period, use two periods. The first is the delimiter for the macro reference, and the second is part of the text.

set in&name..temp;

After macro variable resolution, SAS sees this statement:

SET INSALES.TEMP;

You can end any macro variable reference with a delimiter, but the delimiter is necessary only if the characters that follow can be part of a SAS name. For example, both of these TITLE statements are correct:

title "&name.--a report";
   title "&name--a report";

They produce the following:

TITLE "sales--a report";

Previous Page | Next Page | Top of Page