Chapter Contents


SAS Macro Language: Reference

Macro Variables Defined by Users

You can create your own macro variables, change their values, and define their scope. You can define a macro variable within a macro, and you can also explicitly define it as a global variable, by defining it with the %GLOBAL statement. Macro variable names must start with a letter or an underscore and can be followed by letters or digits. You can assign any name to a macro variable as long as the name is not a reserved word. The prefixes AF, DMS, SQL, and SYS are not recommended because they are frequently used in SAS software for automatic macro variables. Thus, using one of these prefixes can cause a name conflict with an automatic macro variable. For a complete list of reserved words in the macro language, see Appendix 1, "Reserved Words in the Macro Facility." If you assign a macro variable name that is not valid, an error message is printed in the SAS log.

You can use %PUT _ALL_ to view all user-created macro variables. See %PUT in Chapter 13.

Creating Macro Variables and Assigning Values

The simplest way to create and assign a value to a macro variable is to use the macro program statement %LET, as in

%let dsname=Newdata;

DSNAME is the name of the macro variable. Newdata is the value of the macro variable DSNAME. The value of a macro variable is simply a string of characters. The characters can include any letters, numbers, or printable symbols found on your keyboard, and blanks between characters. The case of letters is preserved in a macro variable value. Some characters, such as unmatched quotation marks, require special treatment, which is described later.

If a macro variable already exists, a value assigned to it replaces its current value. If a macro variable or its value contains macro triggers (% or &), the trigger is evaluated before the value is assigned. In the following example, &name is resolved to Cary and then it is assigned as the value of city in the following statements:

%let name=Cary;
%let city=&name;

Generally, the macro processor treats alphabetic characters, digits, and symbols (except & and %) as characters. It can also treat & and % as characters using a special treatment, which is described later. It does not make a distinction between character and numeric values as the rest of the SAS System does. (However, the %EVAL and %SYSEVALF functions can evaluate macro variables as integers or floating point numbers. See "Evaluation Functions" in Chapter 12, "Macro Language Elements.")

Macro variable values can represent text to be generated by the macro processor or text to be used by the macro processor. Values can range in length from 0 bytes to 32K. If you omit the value argument, the value is null (0 characters). By default, leading and trailing blanks are not stored with the value.

In addition to the %LET statement, other features of the macro language that create macro variables are

Types of Assignments for Macro Variable Values describes how to assign a variety of types of values to macro variables.

Types of Assignments for Macro Variable Values
To assign ... Use...
Constant text a character string. The following statements show several ways that the value maple can be assigned to macro variable STREET. In each case, the macro processor stores the five-character value maple as the value of STREET. The leading and trailing blanks are not stored.
%let street=maple;

%let street= maple;
%let street=
Note: Quotation marks are not required. If quotation marks are used, they become part of the value.
Digits the appropriate digits. This example creates the macro variables NUM and TOTALSTR:
%let num=123'
%let totalstr=100+200;
The macro processor does not treat 123 as a number or evaluate the expression 100+200. Instead, the macro processor treats all the digits as characters.
Arithmetic expressions the %EVAL function, for example,
%let num=%eval(100+200); / * produces 300 * /
use the %SYSEVALF function, for example,
%let num=%sysevalf(100+1.597) / * produces 101.597 * /
For more information, see "Evaluation Functions" in Chapter 12 and details on the functions in Chapter 13.
A null value no assignment for the value argument. For example,
%let country=;
A macro variable reference a macro variable reference, &macro-variable. For example,
%let street=Maple;
%let num=123;
%let address=&num &street Avenue;
This example shows multiple macro references that are part of a text expression. The macro processor attempts to resolve text expressions before it makes the assignment. Thus, the macro processor stores the value of macro variable ADDRESS as 123 Maple Avenue.

You can treat ampersands and percent signs as literals by using the %NRSTR function to mask the character so that the macro processor treats it as text instead of trying to interpret it as a macro call. See "Quoting Functions" in Chapter 12 and Chapter 7, "Macro Quoting," for information.

A macro invocation a macro call, %macro-name. For example,
%let status=%wait;
When the %LET statement executes, the macro processor also invokes the macro WAIT. The macro processor stores the text produced by the macro WAIT as the value of STATUS.

To prevent the macro from being invoked when the %LET statement executes, use the %NRSTR function to mask the percent sign:

%let status=%nrstr(%wait);
The macro processor stores %wait as the value of STATUS.
Blanks and special characters macro quoting function %STR or %NRSTR around the value. This action masks the blanks or special characters so that the macro processor interprets them as text. See "Quoting Functions" in Chapter 12 and Chapter 7, "Macro Quoting." For example,
%let state=%str( North Carolina);
%let town=%str(Taylor%'s Pond);
%let store=%nrstr(Smith&Jones);
%let plotit=%str(
       proc plot;
         plot income*age;
The definition of macro variable TOWN demonstrates using %STR to mask a value containing an unmatched quotation mark. "Macro Quoting Functions" in Chapter 12 and Chapter 7 discuss macro quoting functions that require unmatched quotation marks and other symbols to be marked.

The definition of macro variable PLOTIT demonstrates using %STR to mask blanks and special characters (semicolons) in macro variable values. When a macro variable contains complete SAS statements, the statements are easier to read if you enter them on separate lines with indentions for statements within a DATA or PROC step. Using a macro quoting function retains the significant blanks in the macro variable value.

Value from a DATA step the SYMPUT routine. This example puts the number of observations in a data set into a FOOTNOTE statement where AGE is greater than 20:
data _null_;
   set in.permdata end=final;
   if age>20 then n+1;
   if final then call symput('number',trim(left(n)));
footnote "&number Observations have AGE>20";
During the last iteration of the DATA step, the SYMPUT routine creates a macro variable named NUMBER whose value is the value of N. (The SAS System also issues a numeric-to-character conversion message.) The TRIM and the LEFT functions remove the extra space characters from the DATA step variable N before its value is assigned to the macro variable NUMBER.

The program generates this FOOTNOTE statement:

FOOTNOTE "Observations have AGE>20";
For a discussion of SYMPUT, including information on preventing the numeric-character message, see Chapter 13.

Chapter Contents



Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.