BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I want to ask please what is the difference between the following two codes.

Are they equivalent?

code2 is using global statement.

Code1 is not using global statement.

Are both lastDEC SAS macro varaibles defined as global?

 

 

%let last=1808;
/*last DEC*/
data _null_;
kl=mdy(12,1,(substr("&last.",1,2)*1-1)); 
call symput("kl",kl);
run;

/*code1*/
%let lastDEC=%sysfunc(putn(&kl.,yymmn4.));
%put &lastDEC;

/*code2*/
%macro mmacro;
	%global lastDEC; 
    %let lastDEC=%sysfunc(putn(&kl.,yymmn4.));
%mend;
%mmacro;
%put &lastDEC;

 

2 REPLIES 2
Tom
Super User Tom
Super User

The difference is that the second block of code is defining and calling a macro.  The first one is just normal open code.

 

There is no need for a %GLOBAL statement in open code since there are no other symbol tables active. 

 

But when you are running in a macro then there is at least the symbol table for the current macro (and possibly others if the macro has been called by other macros).  Then if the macro variable you are trying to set does not already exist it will be created in the macro's local symbol table.  So to insure that it lives after the macro has ended you can use the %GLOBAL statement to create it in the global symbol table. 

 

Here is another variation:

/*code3*/
%macro mmacro;
    %let lastDEC=%sysfunc(putn(&kl.,yymmn4.));
%mend;

%let lastDEC=; 
%mmacro;
%put &lastDEC;

Notice that you don't NEED the %GLOBAL statement if you first insure that the macro variable has already been defined BEFORE you call the macro.

 

Also watch out for use %GLOBAL statement inside a macro with testing that the macro variable has not already been created. If it is already defined as LOCAL (to either the current macro or some other macro that is still running) then the %GLOBAL statement will generate an error.  

 

 

Here is another variation:

 

/*code4*/
%macro mmacro;
  %if not %symexist(lastDEC) %then %global lastDEC;
  %let lastDEC=%sysfunc(putn(&kl.,yymmn4.));
%mend mmacro;

%macro Bmacro;
%local lastDEC;
%mmacro;
%put &lastDEC;
%mend Bmacro;

%Bmacro;

 

PJB
Fluorite | Level 6 PJB
Fluorite | Level 6

It depends... If you run this in open code, then both macro variables will be in the global symbol table. If you run this inside another macro, the variable created by code1 will be local to the symbol table of that macro, whereas code2 will always put the variable in the global symbol table. If you would remove the global statement from code2, the variable would become local to %mmacro, like the variable created by code1 would be local to any macro it's ran from.

 

Also keep in mind that you're assigning the same variable (lastDEC) twice, the second time specifically global, which apparently results in an error if this variable already exists in the local but not global symbol table. Moreover, since lastDEC would already exist in at least whatever symbol table code1 would run from, code2 will reassign this macro variable and not create a new one in the %mmacro local symbol table.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 1357 views
  • 0 likes
  • 3 in conversation