<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Global and Local Scope of Macro variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Global-and-Local-Scope-of-Macro-variables/m-p/312264#M67705</link>
    <description>Thanks a lot kurt by taking out your time to explain very elaborately. the concept is clear now.&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;Geetha</description>
    <pubDate>Thu, 17 Nov 2016 10:16:45 GMT</pubDate>
    <dc:creator>GeethaMN</dc:creator>
    <dc:date>2016-11-17T10:16:45Z</dc:date>
    <item>
      <title>Global and Local Scope of Macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Global-and-Local-Scope-of-Macro-variables/m-p/312233#M67691</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let season = sunny;
%put &amp;amp;season;

%macro mn;
	%let weather = rainy;
	%put &amp;amp;weather;
	%global weather;

	%let season = winter;
	%put &amp;amp;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;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my question is now what is the scope of this season variable global or local? &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;please help me in understanding this concept.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2016 07:15:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Global-and-Local-Scope-of-Macro-variables/m-p/312233#M67691</guid>
      <dc:creator>GeethaMN</dc:creator>
      <dc:date>2016-11-17T07:15:51Z</dc:date>
    </item>
    <item>
      <title>Re: Global and Local Scope of Macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Global-and-Local-Scope-of-Macro-variables/m-p/312243#M67697</link>
      <description>&lt;P&gt;First of all, your code produces a ERROR message:&lt;/P&gt;
&lt;PRE&gt;16         %let season = sunny;
17         %put &amp;amp;season;
sunny
18         
19         %macro mn;
20         	%let weather = rainy;
21         	%put &amp;amp;weather;
22         	%global weather;
23         
24         	%let season = winter;
25         	%put &amp;amp;season;
26         	%local season;
27         %mend;
28         
29         %mn;
rainy
ERROR: Attempt to %GLOBAL a name (WEATHER) which exists in a local environment.
winter
&lt;/PRE&gt;
&lt;P&gt;(when run on a "clean" environment with no user-defined macro variables present)&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;Therefore the %global and %local statement needs to be moved inside the macro. See later example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, local macro variables are only viewable inside the macro where they are defined as local.&lt;/P&gt;
&lt;P&gt;Outside, you can only see global macro variables, never the versions local to a macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let season = sunny;
%put &amp;amp;season;

%macro mn;
	%global weather;
	%let weather = rainy;
	%put &amp;amp;weather;

	%local season;
	%let season = winter;
	%put &amp;amp;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;see this log:&lt;/P&gt;
&lt;PRE&gt;16         %let season = sunny;
17         %put &amp;amp;season;
sunny
18         
19         %macro mn;
20         	%global weather;
21         	%let weather = rainy;
22         	%put &amp;amp;weather;
23         
24         	%local season;
25         	%let season = winter;
26         	%put &amp;amp;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
&lt;/PRE&gt;
&lt;P&gt;&amp;amp;weather, that only exists as global, will report as global in both cases.&lt;/P&gt;
&lt;P&gt;&amp;amp;season, which exists globally, but also locally to the macro, now reports as local in the macro and global outside.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As an aside:&lt;/P&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
put 'Inside macro';
if 1
then put 'True';
else put 'False';
if 0
then put 'True';
else put 'False';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS I removed the first version of this post, as I thought it contained a logical mistake. This one illustrates clearly what happens.&lt;/P&gt;
&lt;P&gt;Make sure to run the example on a freshly started SAS instance, as pre-existing macro variables might change the results.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2016 08:58:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Global-and-Local-Scope-of-Macro-variables/m-p/312243#M67697</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-11-17T08:58:34Z</dc:date>
    </item>
    <item>
      <title>Re: Global and Local Scope of Macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Global-and-Local-Scope-of-Macro-variables/m-p/312264#M67705</link>
      <description>Thanks a lot kurt by taking out your time to explain very elaborately. the concept is clear now.&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;Geetha</description>
      <pubDate>Thu, 17 Nov 2016 10:16:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Global-and-Local-Scope-of-Macro-variables/m-p/312264#M67705</guid>
      <dc:creator>GeethaMN</dc:creator>
      <dc:date>2016-11-17T10:16:45Z</dc:date>
    </item>
  </channel>
</rss>

