Previous Page | Next Page

Scopes of Macro Variables

Local Macro Variables

Local macro variables are defined within an individual macro. Each macro 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 figure illustrates the local symbol table during the execution of the following program.

%macro holinfo(day,date);
   %let holiday=Christmas;
   %put *** Inside macro: ***;
   %put *** &holiday occurs on &day, &date, 2002. ***;
%mend holinfo;

%holinfo(Wednesday,12/25)

%put *** Outside macro: ***;
%put *** &holiday occurs on &day, &date, 2002. ***;

The %PUT statements write the following to the SAS log:

*** Inside macro: ***
*** Christmas occurs on Wednesday, 12/25, 2002. ***

*** Outside macro: ***
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, 2002. ***

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.

Local Symbol Table

[Local Macro Variables]

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:

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. See Creating Global Variables Based on the Value of Local Variables.)  [cautionend]

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.

You can use the %SYMLOCAL function to indicate whether an existing macro variable resides in an enclosing local symbol table. See the %SYMLOCAL Function for more detailed information.

Previous Page | Next Page | Top of Page