Understanding Symbol Tables

The Global Symbol Table

You are already somewhat familiar with the global symbol table. Remember that automatic macro variables are stored in the global symbol table. User-defined macro variables that you create with a %LET statement in open code (code that is outside of a macro definition) are also stored in the 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. Macro variables in the global symbol table
  • are available anytime during the session
  • can be created by a user
  • have values that can be changed during the session (except for some automatic macro variables).
You can create a global macro variable with the following:
  • 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.
You should already be familiar with the %LET statement, the SYMPUT routine, and the INTO clause. Let’s examine the %GLOBAL statement.

The %GLOBAL Statement

The %GLOBAL statement
  • creates one or more macro variables in the global symbol table and assigns null values to them
  • can be used either inside or outside a macro definition
  • has no effect on variables that are already in the global symbol table.
    General form, %GLOBAL statement:
    %GLOBAL macro-variable-1 <...macro-variable-n>;
    Here is an explanation of the syntax:
    macro-variable
    is either the name of a macro variable or a text expression that generates a macro variable name.

Example

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=sasuser.courses;
   %let vars=course_title course_code days;
   proc print data=&dsn;
      var &vars;
   title "Listing of &dsn data set";
   run;
%mend;

%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 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.
local symbol table
The local symbol table contains macro variables that can be
  • created and initialized at macro invocation (that is, by parameters)
  • created or updated during macro execution
  • referenced anywhere within the macro.
You can create local macro variables with the following:
  • 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 if a local symbol table already exists. If no local symbol table exists when the SYMPUT routine executes, it creates a global macro variable.
You have already learned about using parameters in macro definitions. You should also already be familiar with the %LET statement, the SYMPUT routine, and the INTO clause. Let’s examine the %LOCAL statement.

The %LOCAL Statement

The %LOCAL statement
  • can appear only inside a macro definition
  • creates one or more macro variables in the local symbol table and assigns null values to them
  • has no effect on variables that are already in the local symbol table.
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.

Example

In this example, the first %LET statement creates a global macro variable named dsn and assigns a value of sasuser.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 sasuser.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=sasuser.courses;

%macro printdsn;
   %local dsn;
   %let dsn=sasuser.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.
Table 11.9 SAS Log
199  %let dsn=sasuser.courses;
200
201  %macro printdsn;
202     %local dsn;
203     %let dsn=sasuser.register;
204     %put The value of DSN inside Printdsn is &dsn;
205  %mend;
206
207  %printdsn
The value of DSN inside Printdsn is sasuser.register
208  %put The value of DSN outside Printdsn is &dsn;
The value of DSN outside Printdsn is sasuser.courses

Rules for Creating and Updating Variables

When the macro processor receives a request to create or update a macro variable during macro execution, the macro processor follows certain rules.
Suppose the macro processor receives a %LET statement during a macro call, as follows:
%let macvar=value;
The macro processor processes the following steps:
  1. The macro processor checks to see whether the macro variable macvar already exists in the local symbol table. If so, the macro processor updates macvar in the local symbol table with the value value. If macvar does not exist in the local table, the macro processor goes on to step 2.
  2. The macro processor checks to see whether the macro variable macvar already exists in the global symbol table. If so, the macro processor updates macvar in the global symbol table with the value value. If macvar does not exist in the global symbol table, the macro processor goes on to step 3.
  3. The macro processor creates a macro variable named macvar in the local symbol table and assigns a value of value to it.
macro variable macvar
Similarly, suppose the macro processor receives the following reference during a macro call:
&macvar
The macro processor takes the following steps:
  1. The macro processor checks to see whether the macro variable macvar exists in the local symbol table. If so, the macro processor retrieves the value of macvar from the local symbol table. If macvar does not exist in the local table, the macro processor goes on to step 2.
  2. The macro processor checks to see whether the macro variable macvar exists in the global symbol table. If so, the macro processor retrieves the value of macvar from the global symbol table. If macvar does not exist in the global symbol table, the macro processor goes on to step 3.
  3. The macro processor returns the tokens to the word scanner. A warning message is written to the SAS log to indicate that the reference was not resolved.
Warning message
Note: Remember that if the macro processor receives either a %LET statement or a macro variable reference (&macvar) in open code, it checks only the global symbol table for existence of the macro variable. If a macro program is not currently executing, a local symbol table does not currently exist.

Multiple Local Symbol Tables

Multiple local symbol tables can exist concurrently during macro execution if you have nested macros. That is, if you define a macro program that calls another macro program, and if both macros create local symbol tables, then two local symbol tables exists while the second macro executes.

Example

Suppose the following two macros, Outer and Inner, have been compiled. The macro named Outer creates a local macro variable named variX and assigns a value of one to it. Then Outer calls another macro program named Inner. The macro named Inner creates a local macro variable named variY and assigns the value of variX to it.
%macro outer;
   %local variX;
   %let variX=one;
   %inner
%mend outer;

%macro inner;
   %local variY;
   %let variY=&variX;
%mend inner;
We examine what happens to the symbol tables when you submit the following code:
%let variX=zero;
%outer
  1. The macro processor receives %let variX=zero;. It checks the global symbol table for a macro variable named variX. There is none, so the macro processor creates variX and assigns a value of zero to it.
    global symbol table
  2. The macro processor receives %outer. The macro processor retrieves the macro Outer from Work.Sasmacr, and begins executing it.
  3. The macro processor encounters %local variX;. It creates a local symbol table. The macro processor creates the macro variable variX in this local table and assigns a null value to it. This does not affect the macro variable variX that is stored in the global symbol table.
    global symbol table
  4. The macro processor encounters %let variX=one;. The macro processor checks the local symbol table for variX and assigns a value of one to it.
    global symbol table
  5. The macro processor receives %inner. It retrieves the macro Inner from Work.Sasmacr, and begins executing it.
  6. The macro processor encounters %local variY;. It creates a local symbol table. The macro processor creates a macro variable variY in this table and assigns a null value to it. There are now two local symbol tables in existence.
    global symbol table
  7. The macro processor encounters %let variY=&variX;. It checks the most recently created local table for variX. There is no such macro variable in that symbol table, so the macro processor then checks the other local symbol table. It retrieves the value one from that symbol table and substitutes the value into the %LET statement. Then the macro processor checks the most recently created local symbol table for a macro variable named variY. When it finds this macro variable, it assigns the value one to it.
    global symbol table
  8. The 4Inner macro finishes executing, and the local symbol table that was created within this macro is deleted. There is now only one local symbol table in existence.
    global symbol table
  9. The Outer macro finishes executing, and the local symbol table that was created within this macro is deleted. There are now no local symbol tables in existence. The global symbol table has not been changed since variX was created and was assigned a value of zero.
    global symbol table
As you can see, each macro program in the example above has its own local symbol table that exists as long as the macro executes. When a macro finishes executing, its local symbol table and all of the local macro variables that are contained in that table are erased. The global symbol table and all of the global macro variables that are contained in it remain.

The MPRINTNEST Option

The MPRINTNEST option allows the macro nesting information to be written to the SAS log in the MPRINT output. This has no effect on the MPRINT output that is sent to an external file.
General form, MPRINTNEST option:
OPTIONS MPRINTNEST | NOMPRINTNEST;
Here is an explanation of the syntax:
MPRINTNEST
specifies that macro nesting information is written in the MPRINT output in the SAS log.
NOMPRINTNEST
specifies that macro nesting information is not written in the MPRINT output in the SAS log.
The setting of the MPRINTNEST option does not imply the setting of MPRINT. You must set both MPRINT and MPRINTNEST for output with the nesting information to be written to the SAS log.

Example

Suppose that you have defined three nested macros, as follows:
%macro outer;
   data _null_;
      %inner
   run;
%mend outer;

%macro inner;
   put %inrmost;
%mend inner;

%macro inrmost;
   'This is the text of the PUT statement'
%mend inrmost;
The SAS log below shows the messages that are written when you set both the MPRINT and MPRINTNEST options and submit a call to the Outer macro, as follows:
options mprint mprintnest;
%outer
Table 11.10 SAS Log
MPRINT(OUTER):   data _null_;
MPRINT(OUTER.INNER): put
MPRINT(OUTER.INNER.INRMOST): 'This is the text of the PUT statement'
MPRINT(OUTER.INNER): ;
MPRINT(OUTER):   run;

This is the text of the PUT statement
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

The MLOGICNEST Option

The MLOGICNEST option allows the macro nesting information to be displayed in the MLOGIC output in the SAS log. The setting of MLOGICNEST does not affect the output of any currently executing macro.
General form, MLOGICNEST option:
OPTIONS MLOGICNEST | NOMLOGICNEST;
Here is an explanation of the syntax:
MLOGICNEST
specifies that macro nesting information is written in the MLOGIC output in the SAS log.
NOMLOGICNEST
specifies that macro nesting information is not written in the MLOGIC output in the SAS log.
The setting of MLOGICNEST does not imply the setting of MLOGIC. You must set both MLOGIC and MLOGICNEST for output with nesting information to be written to the SAS log.

Example

Suppose that you have defined three nested macros, as follows:
%macro outer;
   %put THIS IS OUTER;
   %inner
%mend outer;

%macro inner;
   %put THIS IS INNER;
   %inrmost
%mend inner;

%macro inrmost;
   %put THIS IS INRMOST;
%mend inrmost;
The SAS log below shows the messages that are written when you set both the MLOGIC and MLOGICNEST options and submit a call to the Outer macro, as follows:
options mlogic mlogicnest;
%outer
Table 11.11 SAS Log
MLOGIC(OUTER):  Beginning execution.
MLOGIC(OUTER):  %PUT THIS IS OUTER
THIS IS OUTER
MLOGIC(OUTER.INNER):  Beginning execution.
MLOGIC(OUTER.INNER):  %PUT THIS IS INNER
THIS IS INNER
MLOGIC(OUTER.INNER.INRMOST):  Beginning execution.
MLOGIC(OUTER.INNER.INRMOST):  %PUT THIS IS INRMOST
THIS IS INRMOST
MLOGIC(OUTER.INNER.INRMOST):  Ending execution.
MLOGIC(OUTER.INNER): Ending execution.
MLOGIC(OUTER): Ending execution.
..................Content has been hidden....................

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