%let season = sunny;
%put &season;
%macro mn;
%let weather = rainy;
%put &weather;
%global weather;
%let season = winter;
%put &season;
%local season;
%mend;
%mn;
data _null_;
if %symglobl(weather) then put 'True';
else put 'False';
if %symlocal(season) then put 'True';
else put 'False';
run;
Hello everyone,
1) In the above program i have created a macro variable called season as a global(by using %let method i.e. outside macro definition) and redefined it as local (by defining inside Macro definition) at the same time am redefining it as local by using %local . even then %symlocal(season) says FALSE. why?
my question is now what is the scope of this season variable global or local?
2) defined weather macro variable inside macro definition so it will be having local scope for sure. but redefining it's scope as global by using %global even then %symglobl(weather) and %symlocal(weather) both were showing it as FALSE. why?
please help me in understanding this concept.
First of all, your code produces a ERROR message:
16 %let season = sunny; 17 %put &season; sunny 18 19 %macro mn; 20 %let weather = rainy; 21 %put &weather; 22 %global weather; 23 24 %let season = winter; 25 %put &season; 26 %local season; 27 %mend; 28 29 %mn; rainy ERROR: Attempt to %GLOBAL a name (WEATHER) which exists in a local environment. winter
(when run on a "clean" environment with no user-defined macro variables present)
This happens because you already "defined" weather as local by the %let. It was created in the local table and will stay there for the duration of the macro.
Therefore the %global and %local statement needs to be moved inside the macro. See later example.
Now, local macro variables are only viewable inside the macro where they are defined as local.
Outside, you can only see global macro variables, never the versions local to a macro.
%let season = sunny;
%put &season;
%macro mn;
%global weather;
%let weather = rainy;
%put &weather;
%local season;
%let season = winter;
%put &season;
data _null_;
put 'Inside macro';
if %symglobl(weather)
then put 'True';
else put 'False';
if %symlocal(season)
then put 'True';
else put 'False';
run;
%mend;
%mn;
data _null_;
put 'Outside macro';
if %symglobl(weather)
then put 'True';
else put 'False';
if %symlocal(season)
then put 'True';
else put 'False';
run;
see this log:
16 %let season = sunny; 17 %put &season; sunny 18 19 %macro mn; 20 %global weather; 21 %let weather = rainy; 22 %put &weather; 23 24 %local season; 25 %let season = winter; 26 %put &season; 27 data _null_; 28 put 'Inside macro'; 29 if %symglobl(weather) 30 then put 'True'; 31 else put 'False'; 32 if %symlocal(season) 33 then put 'True'; 34 else put 'False'; 35 run; 36 %mend; 37 38 %mn; rainy winter Inside macro True True NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds 39 40 data _null_; 41 put 'Outside macro'; 42 if %symglobl(weather) 43 then put 'True'; 44 else put 'False'; 45 if %symlocal(season) 46 then put 'True'; 47 else put 'False'; 48 run; Outside macro True False NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
&weather, that only exists as global, will report as global in both cases.
&season, which exists globally, but also locally to the macro, now reports as local in the macro and global outside.
As an aside:
keep in mind that the macro functions %symglobl and %symlocal are resolved by the macro processor before the data steps are compiled and executed, so the code of the data step will look like this:
data _null_;
put 'Inside macro';
if 1
then put 'True';
else put 'False';
if 0
then put 'True';
else put 'False';
run;
PS I removed the first version of this post, as I thought it contained a logical mistake. This one illustrates clearly what happens.
Make sure to run the example on a freshly started SAS instance, as pre-existing macro variables might change the results.
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!
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.