Tuesday, December 28, 2010

Multiple Local Symbol Tables

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;
Let's 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
    variX zero


  2. The macro processor receives %outer. The macro processor retrieves the macro Outer from Work.Sasmacr, then 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
    variX zero
    Outer Local Symbol Table
    variX      

  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
    variX zero
    Outer Local Symbol Table
    variX one

  5. The macro processor receives %inner. It retrieves the macro Inner from Work.Sasmacr, then 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
    variX zero
    Outer Local Symbol Table
    variX one
    Inner Local Symbol Table
    variY      

  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
    variX zero
    Outer Local Symbol Table
    variX one
    Inner Local Symbol Table
    variY one

  8. The Inner 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
    variX zero
    Outer Local Symbol Table
    variX one

  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
    variX zero

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.

1 comment: