BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8

Hi I use a init.sas to run a series of program. My question is as follows:

1. For any global macro variables in each individual sas program that runs when init.sas is run, are they gonna be global macro variables for init.sas or it is not any more available once the individual sas program has finished running?

 

2. For a macro variable exist in init.sas but was not defined there, does it mean it was defined in one of the individual program that runs when init,sas is run

 

thanks

4 REPLIES 4
AMSAS
SAS Super FREQ

Here's the documentation on Scopes of Macro Variables

There are several unknowns in your questions, it's not as simple as a macro variable being created in init.sas

 

The short answer is a macro variable exists until you end the SAS session it was created in, assuming it was defined as global. If it was defined as local then it will only exist within the macro it was created in.

 

 

AMSAS
SAS Super FREQ

Here's a simple example:

%let Global_1 = I am Global 1 ;
%macro example ;
	%global global_2 ;
	%let local_1 = I am Local 1 ;
	%let global_2 = I am global 2 ;
	%put local_1 : &local_1 ;
    %put global_1 : &global_1 ;
    %put global_2 : &global_2 ;

%mend ;

%put Running Macro example ;
%example ;

 

Which generates this log (not I have the option SYMBOLGEN turned on)

62  %let Global_1 = I am Global 1 ;
263  %macro example ;
264      %global global_2 ;
265      %let local_1 = I am Local 1 ;
266      %let global_2 = I am global 2 ;
267      %put local_1 : &local_1 ;
268      %put global_1 : &global_1 ;
269      %put global_2 : &global_2 ;
270
271  %mend ;
272
273  %put Running Macro example ;
Running Macro example
274  %example ;
SYMBOLGEN:  Macro variable LOCAL_1 resolves to I am Local 1
local_1 : I am Local 1
SYMBOLGEN:  Macro variable GLOBAL_1 resolves to I am Global 1
global_1 : I am Global 1
SYMBOLGEN:  Macro variable GLOBAL_2 resolves to I am global 2
global_2 : I am global 2
275
276  %put Printing all macro variables ;
Printing all macro variables
277  %put local_1 : &local_1 ;
WARNING: Apparent symbolic reference LOCAL_1 not resolved.
local_1 : &local_1
278  %put global_1 : &global_1 ;
SYMBOLGEN:  Macro variable GLOBAL_1 resolves to I am Global 1
global_1 : I am Global 1
279  %put global_2 : &global_2 ;
SYMBOLGEN:  Macro variable GLOBAL_2 resolves to I am global 2
global_2 : I am global 2

Notice how local_1 prints a value when the %put is inside the example macro, but fails to resolve where outside.

 

Tom
Super User Tom
Super User

You probably need to clarify what you are asking about.

 

GLOBAL macro variables exist until you remove them with %SYMDEL macro statement.

 

Note that if you have defined any LOCAL macro variable with the same name then an existing GLOBAL macro variable then it will be the most local version of the macro whose value is used when you reference the variable.

 

What is INIT.SAS?  Is it just some SAS code?  Or does it define or use any macros?

 

If your code is referencing macro variables that it is not clearly defining then you might have trouble with the code.  At least the humans looking at the code will have trouble understanding what it is going to do.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 692 views
  • 3 likes
  • 4 in conversation