Controlling Variable Scope

Scope of Macro Variables

Every macro variable has a scope.
Macro variable scope can either be global or local. Global macro variables can be created at any time during a SAS session, can persist for the duration of the SAS session, and can be referenced anywhere in the SAS session except for the DATALINES statement. To remove a global macro variable from memory within a SAS session, you must explicitly delete it using a %SYMDEL statement.
Local macro variables are created only during execution of a macro program, persist only as long as the macro is executing, and therefore can be referenced only while the macro is executing. When a macro terminates execution, its local symbol table is automatically deleted.

The %GLOBAL Statement

Global Symbol Table

Macro variables are stored in symbol tables, which list the macro variable name and its value. There is a global symbol table, which stores all global macro variables. Local macro variables are stored in a local symbol table that is created at the beginning of the execution of a macro.
Figure 9.2 Global Symbol Table
Global Symbol Table
The global symbol table is created during the initialization of a SAS session and is deleted at the end of the session. The following statements describe macro variables in the global symbol table:
  • They are available anytime during the session.
  • They can be created by a user.
  • They have values that can be changed during the session (except for some automatic macro variables).

A Brief Overview of %GLOBAL Statement

Here is what the %GLOBAL statement does:
  • It creates one or more macro variables in the global symbol table and assigns null values to them.
  • It can be used either inside or outside a macro definition.
  • It can create a READONLY macro variable with an initial value that cannot be changed.
  • It has no effect on variables that are already in the global symbol table.
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 (assuming 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.
You can create a global macro variable with any of these elements:
  • a %LET statement (used outside a macro definition)
  • a DATA step that contains a SYMPUT routine
  • a DATA step that contains a SYMPUTX routine
  • a SELECT statement that contains an INTO clause in PROC SQL
  • a %GLOBAL statement

%GLOBAL Statement Syntax

Syntax, %GLOBAL statement:
%GLOBAL macro-variable-1 <...macro-variable-n>;
macro-variable
is either the name of a macro variable or a text expression that generates a macro variable name.

Example: Using %GLOBAL Statement

To create a global macro variable inside a macro definition, you can use the %GLOBAL statement. The %GLOBAL statement in the following example creates two global macro variables, Dsn and Vars. The %LET statements assign values to the new global macro variables, as follows:
%macro printdsn;
   %global dsn vars;
   %let dsn=certadv.courses;
   %let vars=course_title course_code days;
   proc print data=&dsn;
      var &vars;
   title "Listing of &dsn data set";
   run;
%mend printdsn;

%printdsn
Note: You use the %SYMDEL statement to delete a macro variable from the global symbol table during a SAS session. To remove the macro variable dsn from the global symbol table, you submit the following statement:
%symdel dsn;

The %LOCAL Statement

Local Symbol Table

A local symbol table is created when a macro that includes a parameter list is called or when a request is made to create a local variable during macro execution. The local symbol table is deleted when the macro finishes execution. That is, the local symbol table exists only while the macro executes.
Figure 9.3 Local Symbol Table
Local Symbol Table
The local symbol table contains macro variables.
  • These variables can be created and initialized at macro invocation (that is, by parameters).
  • They can be created or updated during macro execution.
  • They can be referenced anywhere within the macro.
A local symbol table is not created until a request is made to create a local variable. Macros that do not create local variables do not have a local table. Remember, the SYMPUT routine can create local variables only if the local table already exists.
Since local symbol tables exist separately from the global symbol table, it is possible to have a local macro variable and a global macro variable that have the same name and different values.

A Brief Overview

The following is true of the %LOCAL statement:
  • It can appear only inside a macro definition.
  • It creates one or more macro variables in the local symbol table and assigns null values to them.
  • It has no effect on variables that are already in the local symbol table.
You can create local macro variables with any of these elements:
  • parameters in a macro definition
  • a %LET statement within a macro definition
  • a DATA step that contains a SYMPUT routine within a macro definition
  • a DATA step that contains a SYMPUTX routine within a macro definition
  • a SELECT statement that contains an INTO clause in PROC SQL within a macro definition
  • a %LOCAL statement
Note: The SYMPUT routine can create a local macro variable only if a local symbol table already exists and no variable of the same name exists in the global symbol table. If no local symbol table exists when SYMPUT executes, the value is assigned to a global macro variable. The SYMPUTX call routine provides a third argument (symbol-table) that allows you to explicitly create a variable in a local symbol table while the macro that generated the DATA step is executing. If no local symbol table exists when a call to SYMPUTX with a local specification executes, a local symbol table is created, and then the macro variable is created in the local symbol table.

%LOCAL Statement Syntax

Syntax, %LOCAL statement:
%LOCAL macro-variable-1 <...macro-variable-n>;
macro-variable
is either the name of a macro variable or a text expression that generates a macro variable name.

Example: Using the %LOCAL Statement

In this example, the first %LET statement creates a global macro variable named Dsn and assigns a value of CertAdv.Courses to it.
The %LOCAL statement within the macro definition creates a local macro variable named Dsn, and the %LET statement within the macro definition assigns a value of Certadv.Register to the local variable Dsn.
The %PUT statement within the macro definition writes the value of the local variable Dsn to the SAS log, whereas the %PUT statement that follows the macro definition writes the value of the global variable Dsn to the SAS log:
%let dsn=certadv.courses;

%macro printdsn;
   %local dsn;
   %let dsn=certadv.register;
   %put The value of DSN inside Printdsn is &dsn;
%mend;

%printdsn
%put The value of DSN outside Printdsn is &dsn;
When you submit this code, the following statements are written to the SAS log.
Log 9.3 SAS Log
199  %let dsn=certadv.courses;
200
201  %macro printdsn;
202     %local dsn;
203     %let dsn=certadv.register;
204     %put The value of DSN inside Printdsn is &dsn;
205  %mend;
206
207  %printdsn
The value of DSN inside Printdsn is certadv.register
208  %put The value of DSN outside Printdsn is &dsn;
The value of DSN outside Printdsn is certadv.courses

Nested Scope

The scope of a macro variable can be nested, like boxes within boxes. The following example illustrates nesting variable scope.
%macro test;
   %local x;         /*1*/
   %let x=FALSE;     /*2*/
%macro test;
%test;
1 The macro variable, X, is defined with a %LOCAL statement. When the %LOCAL statement executes, a local symbol table is created for the test macro, and the macro variable, X, is created in this local table.
2 When the macro processor executes the %LET statement, it searches the test macro's local table for a macro variable named X. It finds X in this local table and sets its value to False.
The local table is deleted when the test macro ends.
Last updated: October 16, 2019
..................Content has been hidden....................

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