BookmarkSubscribeRSS Feed
braam
Quartz | Level 8

I used %let to define local macro in %macro statement, but it was gone way outside of the macro. Because I would like to use macro variables generated in %macro outside of it, I want to use global macro. But, replacing %global with %let doesn't work for me. Do they work differently? 

 

Relatedly, can I simply re-define local macro variables to global macro ones?

2 REPLIES 2
Quentin
Super User

Hi,

 

Please show the code you have tried.  This will make it easier to explain why it's not working.

 

It's a good idea to always use the %LOCAL or %GLOBAL statement to explicitly declare the scope of macro variables (there are rare exceptions to this rule).

 

Yes you can use the %GLOBAL statement in a macro to create a global macro variable, for example:

 

%macro try() ;
  %global x ;
  %let x=1 ;
%mend try ;

%try()

%put &=x ;

No, if you have a local macro variable, you cannot use the %GLOBAL statement to redefine it as global. The below log shows the error that will be generated if you try:

 

615  %macro try() ;
616    %local x ;
617    %let x=1 ;
618    %global x ;
619    %let x=1 ;
620  %mend try ;
NOTE: The macro TRY completed compilation without errors.
      15 instructions 236 bytes.
621
622  %try()
ERROR: Attempt to %GLOBAL a name (X) which exists in a local environment.

HTH,

-Q.

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
Tom
Super User Tom
Super User

Note that you use %LET to assign values to macro variables, not to create LOCAL or GLOBAL macro variables.

I suspect you are just confused because if the macro variable you are assigning a value to does not already exist then it will be created as LOCAL.

Try this 

%macro mymacro;

%if not %symexist(mymvar) then %global mymvar;
%let mymvar=Hi there;

%mend mymacro;


%mymacro;
%put My Macro Varaible = &mymvar;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 780 views
  • 2 likes
  • 3 in conversation