Understanding SAS Macro Symbol Tables

Posted: March 30, 2011 in SAS Tips & Tricks

Rules for Creating and Updating SAS MacroVariables:
When the macro processor receives a request to create or update a macro variable during macro execution, the macro processor follows certain rules. Let’s look at those rules.

Suppose the macro processor receives a %LET statement during a macro call, as follows:
%let macvar=value;
The macro processor will take 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.

Similarly, suppose the macro processor receives the following reference during a macro call:
&macvar
The macro processor will take 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.

Remember that if the macro processor receives either a %LET statement or a macro variable reference (&macvar) in open code, it will check 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.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s