Presentation is loading. Please wait.

Presentation is loading. Please wait.

Global and Local Symbol Tables

Similar presentations


Presentation on theme: "Global and Local Symbol Tables"— Presentation transcript:

1 Global and Local Symbol Tables
4 Global and Local Symbol Tables

2 %macro simp proc sql; select count(*) into :numobs from fram.frex4 ; quit; %mend; %simp %put &numobs;

3 %macro simp; %global numobs; proc sql; select count(*) into :numobs from fram.frex4 ; quit; %mend; %simp %put &numobs;

4 The difference between global and local symbol tables.

5 The Global Symbol Table (Review)
The global symbol table is Created during SAS initialization Initialized with automatic macro variables Deleted at the end of the session. This material was covered in chapter 2.

6 The %GLOBAL Statement %GLOBAL macro-variable1 macro-variable ; The %GLOBAL statement adds one or more macro variables to the global symbol table with null values. It has no effect on variables already in the global table. It can be used anywhere in a SAS program.

7 The Local Symbol Table Local macro variables can be created within a macro definition: %LET statement DATA step SYMPUTX routine PROC SQL SELECT statement INTO clause %LOCAL statement Only the last bullet item is truly new. The first three items were implied, but never explicitly stated.

8 The Local Symbol Table A local symbol table is
Created when a macro with a parameter list is called or a local macro variable is created during macro execution Deleted when the macro finishes execution. Macros that do not create local variables do not have a local table. This material was covered in chapter 3.

9 The %LOCAL Statement %LOCAL macro-variable1 macro-variable ; The %LOCAL statement adds one or more macro variables to the local symbol table with null values. It has no effect on variables already in the local table. It can appear only inside a macro definition. The last bullet should be emphasized.

10 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 these rules: %let macvar=value; Does MACVAR already exist in the local table? Update MACVAR in the local table. Yes No In other words, the macro variable will be created in the executing macro's local table UNLESS IT ALREADY EXISTS in another table. The %local statement on the previous slide guarantees that the macro variable will be created in the executing macro's local table and NOT over-write a like-named macro variable in another table. Does MACVAR already exist in the global table? Update MACVAR in the global table. Yes No Create MACVAR in the local table.

11 Rules for Resolving Variables
To resolve a macro variable reference during macro execution, the macro processor follows these rules: &macvar Does MACVAR exist in the local table? Yes Retrieve from local table. No Does MACVAR exist in the global table? In other words, check local first. Yes Retrieve from global table. No Return tokens to word scanner. Issue a warning to SAS log: Apparent symbolic reference MACVAR not resolved.

12 The SYMPUTX Routine The optional scope argument of the SYMPUTX routine specifies where to store the macro variable: G specifies the global symbol table. L specifies the current macro's local symbol table. If no local symbol table exists for the current macro, a local symbol table is created. CALL SYMPUTX(macro-variable, text <,scope>); The SCOPE argument is recommended ANY TIME the symputx routine is used inside a macro definition.

13 The %LOCAL Statement Declare the index variable of a macro loop as a local variable to prevent accidental contamination of a like-named macro variable in the global table or another local table. %macro putloop; %local i; %do i=1 %to &numrows; %put Country&i is &&country&i; %end; %mend putloop; Recommended practice.


Download ppt "Global and Local Symbol Tables"

Similar presentations


Ads by Google