O'Reilly logo

SAS 9.4 Macro Language, 5th Edition by

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. The variable is held in the global symbol table, which the
macro processor creates at the beginning of a SAS session when the following occurs:
a macro variable is defined in a statement that is outside a macro definition (called
open code)
the variable is created automatically by the macro processor (except SYSPBUFF)
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. SAS creates the
local symbol table when the macro starts executing. For more information about symbol
tables, see Chapter 2, “SAS Programs and Macro Processing,” on page 15 and Chapter
5, “Scopes of Macro Variables,” on page 49.
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. Chapter 2, “SAS Programs and Macro Processing,” on page 15 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. For more information, see “%PUT Statement” on page 332 and also Chapter 10,
“Macro Facility Error Messages and Debugging,” on page 121.
Macro Variables Defined by the Macro Processor
When you invoke SAS, the macro processor creates automatic macro variables that
supply information related to the SAS session. Automatic variables are global except
SYSPBUFF, which is local.
To use an automatic macro variable, reference it with an ampersand followed by the
macro variable name (for example, &SYSJOBID). This FOOTNOTE statement contains
references to the automatic macro variables SYSDAY and SYSDATE9:
footnote "Report for &sysday, &sysdate9";
If the current SAS session is invoked on December 16, 2011, macro variable resolution
causes SAS to receive this statement:
FOOTNOTE "Report for Friday, 16DEC2011";
Automatic macro variables are often useful in conditional logic such as a %IF statement
with actions determined by the value that is returned. For more information, see “%IF-
%THEN/%ELSE Statement” on page 317.
You can assign values to automatic macro variables that have read and write status.
However, you cannot assign a value to an automatic macro variable that has read-only
24 Chapter 3 Macro Variables

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required