%GLOBAL Statement

Creates macro variables that are available during the execution of an entire SAS session.
Type: Macro statement
Restriction: Allowed in macro definitions or open code
See: %LOCAL Statement

Syntax

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

Required Argument

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 %GLOBAL statement.

Details

The %GLOBAL statement creates one or more global macro variables and assigns null values to the variables. Global macro variables are variables that are available during the entire execution of the SAS session or job.
A macro variable created with a %GLOBAL statement has a null value until you assign it some other value. If a global macro variable already exists and you specify that variable in a %GLOBAL statement, the existing value remains unchanged.

Comparisons

  • Both the %GLOBAL statement and the %LOCAL statement create macro variables with a specific scope. However, the %GLOBAL statement creates global macro variables that exist for the duration of the session or job. The %LOCAL statement creates local macro variables that exist only during the execution of the macro that defines the variable.
  • If you define both a global macro variable and a local macro variable with the same name, the macro processor uses the value of the local variable during the execution of the macro that contains the local variable. When the macro that contains the local variable is not executing, the macro processor uses the value of the global variable.

Example: Creating Global Variables in a Macro Definition

%macro vars(first=1,last=);
   %global gfirst glast;
   %let gfirst=&first;
   %let glast=&last;
   var test&first-test&last;
%mend vars;
When you submit the following program, the macro VARS generates the VAR statement and the values for the macro variables used in the title statement.
proc print;
   %vars(last=50)
   title "Analysis of Tests &gfirst-&glast";
run;
SAS sees the following:
PROC PRINT;
   VAR TEST1-TEST50;
   TITLE "Analysis of Tests 1-50";
RUN;