all automatic macro variables except SYSPBUFF. For more information about
SYSPBUFF and other automatic macro variables, see “Automatic Macro Variables”
on page 167.
macro variables created outside of any macro.
macro variables created in %GLOBAL statements. For more information about the
%GLOBAL statement, see “Creating Global Macro Variables” on page 63.
most macro variables created by the CALL SYMPUT routine. For more information
about the CALL SYMPUT routine, see “Special Cases of Scope with the CALL
SYMPUT Routine” on page 65.
You can create global macro variables anytime during a SAS session or job. Except for
some automatic macro variables, you can change the values of global macro variables
anytime during a SAS session or job.
In most cases, once you define a global macro variable, its value is available to you
anywhere in the SAS session or job and can be changed anywhere. So, a macro variable
referenced inside a macro definition is global if a global macro variable already exists by
the same name. This action assumes that the variable is not specifically defined as local
with the %LOCAL statement or in a parameter list. The new macro variable definition
simply updates the existing global one. The following are exceptions that prevent you
from referencing the value of a global macro variable:
when a macro variable exists both in the global symbol table and in the local symbol
table, you cannot reference the global value from within the macro that contains the
local macro variable. In this case, the macro processor finds the local value first and
uses it instead of the global value.
if you create a macro variable in the DATA step with the SYMPUT routine, you
cannot reference the value with an ampersand until the program reaches a step
boundary. For more information about macro processing and step boundaries, see
Chapter 4, “Macro Processing,” on page 39.
You can create a read-only global macro variable and assign a specified value to it using
the READONLY option in a %GLOBAL statement. Existing macro variables cannot be
made read-only. The value of the variable cannot be changed, and the variable cannot be
deleted. All read-only macro variables persist until the scope in which they exist is
deleted. For more information, see “%GLOBAL Statement” on page 314.
You can use the %SYMGLOBL function to indicate whether an existing macro variable
resides in the global symbol table. For more information, see “%SYMGLOBL Function”
on page 279.
Local Macro Variables
Local macro variables are defined within an individual macro. Each macro that you
invoke creates its own local symbol table. Local macro variables exist only as long as a
particular macro executes. When the macro stops executing, all local macro variables for
that macro cease to exist.
The following code illustrates the local symbol table during the execution of the
following program.
%macro holinfo(day,date);
%let holiday=Christmas;
%put *** Inside macro: ***;
Local Macro Variables 51
%put *** &holiday occurs on &day, &date, 2012. ***;
%mend holinfo;
%holinfo(Tuesday,12/25)
%put *** Outside macro: ***;
%put *** &holiday occurs on &day, &date, 2012. ***;
The %PUT statements write the following to the SAS log:
*** Inside macro: ***
*** Christmas occurs on Tuesday, 12/25, 2012. ***
66
67 %put *** Outside macro: ***;
*** Outside macro: ***
68 %put *** &holiday occurs on &day, &date, 2012. ***;
WARNING: Apparent symbolic reference HOLIDAY not resolved.
WARNING: Apparent symbolic reference DAY not resolved.
WARNING: Apparent symbolic reference DATE not resolved.
*** &holiday occurs on &day, &date, 2012. ***
As you can see from the log, the local macro variables DAY, DATE, and HOLIDAY
resolve inside the macro. But outside the macro, they do not exist and therefore do not
resolve.
Figure 5.2 Local Symbol Table
A macro's local symbol table is empty until the macro creates at least one macro
variable. A local symbol table can be created by any of the following:
the presence of one or more macro parameters
a %LOCAL statement
macro statements that define macro variables, such as %LET and the iterative %DO
statement (if the variable does not already exist globally or a %GLOBAL statement
is not used)
Note: Macro parameters are always local to the macro that defines them. You cannot
make macro parameters global. (Although, you can assign the value of the parameter
to a global variable. For more information, see “Creating Global Variables Based on
the Value of Local Variables” on page 65.)
When you invoke one macro inside another, you create nested scopes. Because you can
have any number of levels of nested macros, your programs can contain any number of
levels of nested scopes.
52 Chapter 5 Scopes of Macro Variables
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset