Examples of Macro Variable Scopes
Changing the Values of Existing Macro Variables
When the macro processor executes a macro program statement that can create a macro
variable (such as a %LET statement), the macro processor attempts to change the value
of an existing macro variable rather than create a new macro variable. The %GLOBAL
and %LOCAL statements are exceptions.
To illustrate, consider the following %LET statements. Both statements assign values to
the macro variable NEW:
%let new=inventry;
%macro name1;
%let new=report;
%mend name1;
Suppose you submit the following statements:
%name1
data &new;
data report;
Because NEW exists as a global variable, the macro processor changes the value of the
variable rather than creating a new one. The macro NAME1's local symbol table remains
empty.
The following figure illustrates the contents of the global and local symbol tables before,
during, and after NAME1's execution.
Examples of Macro Variable Scopes 57
Figure 5.5 Snapshots of Symbol Tables
Creating Local Variables
When the macro processor executes a macro program statement that can create a macro
variable, the macro processor creates the variable in the local symbol table if no macro
variable with the same name is available to it. Consider the following example:
%let new=inventry;
%macro name2;
%let new=report;
%let old=warehse;
%mend name2;
%name2
data &new;
set &old;
58 Chapter 5 Scopes of Macro Variables
run;
After NAME2 executes, the SAS compiler sees the following statements:
data report;
set &old;
run;
The macro processor encounters the reference &OLD after macro NAME2 has finished
executing. Thus, the macro variable OLD no longer exists. The macro processor is not
able to resolve the reference and issues a warning message.
The following figure illustrates the contents of the global and local symbol tables at
various stages.
Figure 5.6 Symbol Tables at Various Stages
But suppose you place the SAS statements inside the macro NAME2, as in the following
program:
%let new=inventry;
Examples of Macro Variable Scopes 59
%macro name2;
%let new=report;
%let old=warehse;
data &new;
set &old;
run;
%mend name2;
%name2
In this case, the macro processor generates the SET statement during the execution of
NAME2, and it locates OLD in NAME2's local symbol table. Therefore, executing the
macro produces the following statements:
data report;
set warehse;
run;
The same rule applies regardless of how many levels of nesting exist. Consider the
following example:
%let new=inventry;
%macro conditn;
%let old=sales;
%let cond=cases>0;
%mend conditn;
%macro name3;
%let new=report;
%let old=warehse;
%conditn
data &new;
set &old;
if &cond;
run;
%mend name3;
%name3
The macro processor generates these statements:
data report;
set sales;
if &cond;
run;
CONDITN finishes executing before the macro processor reaches the reference
&COND, so no variable named COND exists when the macro processor attempts to
resolve the reference. Thus, the macro processor issues a warning message and generates
the unresolved reference as part of the constant text and issues a warning message. The
following figure shows the symbol tables at each step.
60 Chapter 5 Scopes of Macro Variables
Figure 5.7 Symbol Tables Showing Two Levels of Nesting
Notice that the placement of a macro invocation is what creates a nested scope, not the
placement of the macro definition. For example, invoking CONDITN from within
NAME3 creates the nested scope. It is not necessary to define CONDITN within
NAME3.
Forcing a Macro Variable to Be Local
At times that you need to ensure that the macro processor creates a local macro variable
rather than changing the value of an existing macro variable. In this case, use the
%LOCAL statement to create the macro variable.
Always make all macro variables created within macros local when you do not need
their values after the macro stops executing. Debugging the large macro programs is
easier if you minimize the possibility of inadvertently changing a macro variable's value.
Also, local macro variables do not exist after their defining macro finishes executing, but
global variables exist for the duration of the SAS session. Therefore, local variables use
less overall storage.
Examples of Macro Variable Scopes 61
..................Content has been hidden....................

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