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.

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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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
  • 1140 views
  • 2 likes
  • 3 in conversation