Previous Page | Next Page

Macro Variables

Introduction to Macro Variables

Macro variables are tools that enable you to dynamically modify the text in a SAS program through symbolic substitution. You can assign large or small amounts of text to macro variables, and after that, you can use that text by simply referencing the variable that contains it.

Macro variable values have a maximum length of 65,534 characters. The length of a macro variable is determined by the text assigned to it instead of a specific length declaration. So its length varies with each value it contains. Macro variables contain only character data. However, the macro facility has features that enable a variable to be evaluated as a number when it contains character data that can be interpreted as a number. The value of a macro variable remains constant until it is specifically changed. Macro variables are independent of SAS data set variables.

Note:   Only printable characters should be assigned to macro variables. Non-printable values that are assigned to macro variables might cause unpredictable results.  [cautionend]

Macro variables defined by macro programmers are called user-defined macro variables. Those defined by the macro processor are called automatic macro variables. You can define and use macro variables anywhere in SAS programs, except within data lines.

When a macro variable is defined, the macro processor adds it to one of the program's macro variable symbol tables. When a macro variable is defined in a statement that is outside a macro definition (called open code) or when the variable is created automatically by the macro processor (except SYSPBUFF), the variable is held in the global symbol table, which the macro processor creates at the beginning of a SAS session. When a macro variable is defined within a macro and is not specifically defined as global, the variable is typically held in the macro's local symbol table, which SAS creates when the macro starts executing. For more information about symbol tables, see SAS Programs and Macro Processing and Scopes of Macro Variables.

When it is in the global symbol table, a macro variable exists for the remainder of the current SAS session. A variable in the global symbol table is called a global macro variable. This variable has global scope because its value is available to any part of the SAS session (except in CARDS or DATALINES statements). Other components of SAS might create global macro variables, but only those components created by the macro processor are considered automatic macro variables.

When it is in a local symbol table, a macro variable exists only during execution of the macro in which it is defined. A variable in a local symbol table is called a local macro variable. It has local scope because its value is available only while the macro is executing. SAS Programs and Macro Processing contains figures that illustrate a program with a global and a local symbol table.

You can use the %PUT statement to view all macro variables available in a current SAS session. See %PUT Statement and also in Macro Facility Error Messages and Debugging.

Previous Page | Next Page | Top of Page