Friday, September 10, 2010

How to create Global or Local macro variables: (%Global / %Local)

Global Macro Variables:The global macro variable gets created during the initialization of a SAS session and its get deleted at the end of the session.

Global macro variable cane be created with

􀀀 a %LET statement (used outside a macro definition)
􀀀 a DATA step that contains a SYMPUT routine
􀀀 a DATA step that contains a SYMPUTX routine (New with SAS 9)
􀀀 a SELECT statement that contains an INTO clause in PROC SQL
􀀀 a %GLOBAL statement.

Global macro variables include the following:

*all automatic macro variables except SYSPBUFF. See Automatic Macro Variables for more information about SYSPBUFF and other automatic macro variables.
*macro variables created outside of any macro.
*macro variables created in %GLOBAL statements. See Creating Global Macro Variables for more  information about the %GLOBAL statement.

*most macro variables created by the CALL SYMPUT routine. See Special Cases of Scope with the CALL SYMPUT Routine for more information about the CALL SYMPUT routine.

You can create global macro variables any time during a SAS session or job. Except for some automatic macro variables, you can change the values of global macro variables any time during a SAS session or job.

In most cases, once you define a global macro variable, its value is available to you anywhere in the SAS session or job and can be changed anywhere. So, a macro variable referenced inside a macro definition is global if a global macro variable already exists by the same name (assuming that the variable is not specifically defined as local with the %LOCAL statement or in a parameter list). The new macro variable definition simply updates the existing global one. The following are exceptions that prevent you from referencing the value of a global macro variable:

When a macro variable exists both in the global symbol table and in the local symbol table, you cannot reference the global value from within the macro that contains the local macro variable. In this case, the macro processor finds the local value first and uses it instead of the global value.

If you create a macro variable in the DATA step with the SYMPUT routine, you cannot reference the value with an ampersand until the program reaches a step boundary.
Source: SAS Documentation, SAS(R) 9.2 Macro Language: Reference

Local Macro Variables:
A local symbol table is created when a macro that includes a parameter list is called or when a request is made to create a local variable during macro execution. The local macro variable gets deleted when the macro finishes its execution.

Local macro variables cane be created with

􀀀 the parameters in a macro definition
􀀀 a %LET statement within a macro definition
􀀀 a DATA step that contains a SYMPUT routine within a macro definition
􀀀 a DATA step that contains a SYMPUTX routine within a macro definition  (New with SAS 9)
􀀀 a SELECT statement that contains an INTO clause in PROC SQL within a macro definition
􀀀 a %LOCAL statement.

Note: The SYMPUT routine and the SYMPUTX routine can only create a local macro variable if a local symbol table already exists. If no local symbol table exists when the SYMPUT routine or SYMPUTX routine executes, it will create a global macro variable.

The SYMPUTX routine can also create the local table by setting the third argument to 'L' as shown here:

%macro test;
data _null_;
set sashelp.class(where=(sex='M'));
call symputx('gender',sex,'l');
run;
%put _user_;
%mend test;
%test

Macro Basics:




('DiggThis’)
ShareThis