Previous Page | Next Page

Macro Statements

%LOCAL Statement



Creates macro variables that are available only during the execution of the macro where they are defined.
Type: Macro statement
Restriction: Allowed in macro definitions only
See also: %GLOBAL Statement

Syntax
Details
Comparisons
Example
Using a Local Variable with the Same Name as a Global Variable

Syntax

%LOCAL macro-variable-1 <...macro-variable-n>;

macro-variable-1 <...macro-variable-n>

is the name of one or more macro variables or a text expression that generates one or more macro variable names. You cannot use a SAS variable list or a macro expression that generates a SAS variable list in a %LOCAL statement.


Details

The %LOCAL statement creates one or more local macro variables. A macro variable created with %LOCAL has a null value until you assign it some other value. Local macro variables are variables that are available only during the execution of the macro in which they are defined.

Use the %LOCAL statement to ensure that macro variables created earlier in a program are not inadvertently changed by values assigned to variables with the same name in the current macro. If a local macro variable already exists and you specify that variable in a %LOCAL statement, the existing value remains unchanged.


Comparisons


Example


Example 1: Using a Local Variable with the Same Name as a Global Variable

%let variable=1;

%macro routine;
   %put ***** Beginning ROUTINE *****;
   %local variable;
   %let variable=2;
   %put The value of variable inside ROUTINE is &variable;
   %put ***** Ending ROUTINE *****;
%mend routine;

%routine
%put The value of variable outside ROUTINE is &variable;

Submitting these statements writes these lines to the SAS log:

***** Beginning ROUTINE *****
The value of variable inside ROUTINE is 2
***** Ending ROUTINE *****
The value of variable outside ROUTINE is 1

Previous Page | Next Page | Top of Page