New SAS User

Completely new to SAS or trying something new with SAS? Post here for help getting started.
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 is hosting free webinars!
Next up: SAS Trivia Quiz hosted by SAS on Wednesday May 21.
Register now 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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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