1 / 13

Global and Local Symbol Tables

4. Global and Local Symbol Tables. %macro simp proc sql ; select count(*) into : numobs from fram.frex4 ; quit; %mend ; % simp %put & numobs ;. %macro simp ; %global numobs ; proc sql ; select count(*) into : numobs from fram.frex4 ; quit; %mend ; % simp %put & numobs ;.

chamness
Download Presentation

Global and Local Symbol Tables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 4 Global and Local Symbol Tables

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

  3. %macrosimp; %globalnumobs; procsql; 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.

  6. The %GLOBAL Statement %GLOBAL macro-variable1 macro-variable2 . . .; 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

  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.

  9. The %LOCAL Statement %LOCAL macro-variable1macro-variable2 . . . ; 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.

  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 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? 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>);

  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;

More Related