<?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: Macro variables, local or not? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-variables-local-or-not/m-p/940125#M369055</link>
    <description>&lt;P&gt;That is a lovely test case you wrote up!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The point you are missing is that macro variables defined within a macro are not (necessarily) &lt;STRONG&gt;local&lt;/STRONG&gt; by default. Thus if you want a macro variable to be local (which 99.9% of the time is the right design IMHO), you need to use the %LOCAL statement to declare them to be local.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you add a %LOCAL statement to both macros, they will work as expected:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro macro_B();
	%local i ;
	%put macro_B ... ;	
	%do i = 1 %to 5;
		%put macro_B - looping [&amp;amp;=i];
	%end;
	%put macro_B - loop completed [&amp;amp;=i];
%mend;

%macro macro_A();
	%local i ;
	%put macro_A ... ;	
	%do i = 1 %to 3;
		%put macro_A - looping [&amp;amp;=i];
		%macro_B()
	%end;
	%put macro_A - loop completed [&amp;amp;=i];
%mend;

%macro_A()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Without the %local statement, your macro B will not create a local macro variable named i.&amp;nbsp; Instead the %do i=1 %to... statement in the macro B will use the macro variable i that belongs to the macro A.&amp;nbsp; And that will then cause your macro A to stop looping prematurely.&amp;nbsp; These kinds of scope collisions are a common problem if a macro does not declare the macro variables it creates to be %local.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 20 Aug 2024 16:04:57 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2024-08-20T16:04:57Z</dc:date>
    <item>
      <title>Macro variables, local or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variables-local-or-not/m-p/940109#M369052</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;As a newbie, I just came across "macro variables". They are said to be &lt;EM&gt;local&lt;/EM&gt;, but then I must misunderstand something. Below is an example that seems to generate a result that gives me the impression that a macro variable (in this case &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt;) is &lt;EM&gt;not&lt;/EM&gt; local within a macro, what do I miss?&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro macro_B();
	%put macro_B ... ;	
	%do i = 1 %to 5;
		%put macro_B - looping [&amp;amp;=i];
	%end;
	%put macro_B - loop completed [&amp;amp;=i];
%mend;

%macro macro_A();
	%put macro_A ... ;	
	%do i = 1 %to 3;
		%put macro_A - looping [&amp;amp;=i];
		%macro_B();
	%end;
	%put macro_A - loop completed [&amp;amp;=i];
%mend;

%macro_A();&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Yields the following output&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;macro_A ...
macro_A - looping [I=1]
macro_B ...
macro_B - looping [I=1]
macro_B - looping [I=2]
macro_B - looping [I=3]
macro_B - looping [I=4]
macro_B - looping [I=5]
macro_B - loop completed [I=6]
macro_A - loop completed [I=7]&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Happy for a quick explanation.&lt;BR /&gt;//C&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2024 15:18:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variables-local-or-not/m-p/940109#M369052</guid>
      <dc:creator>CalleC</dc:creator>
      <dc:date>2024-08-20T15:18:11Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables, local or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variables-local-or-not/m-p/940122#M369053</link>
      <description>&lt;P&gt;&lt;STRONG&gt;&lt;A href="https://documentation.sas.com/doc/en/mcrolref/1.0/n10i4tmalsyhgxn1hj4ud13ff074.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/mcrolref/1.0/n10i4tmalsyhgxn1hj4ud13ff074.htm&lt;/A&gt;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2024 15:50:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variables-local-or-not/m-p/940122#M369053</guid>
      <dc:creator>WarrenKuhfeld</dc:creator>
      <dc:date>2024-08-20T15:50:05Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables, local or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variables-local-or-not/m-p/940123#M369054</link>
      <description>&lt;P&gt;I suspect you do not understand what SAS means by LOCAL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you define a macro variable as LOCAL is means that it only exists while it is running.&amp;nbsp; If you call MACRO_B in the middle of running MACRO_A then MACRO_A&amp;nbsp; is still running so the macro variable defined in MACRO_A still exists and can be used by MACRO_B.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When the same macro variable name exists in multiple scopes (GLOBAL, MACRO_A, MACRO_B, etc.) then references will use the most local (inner most) version.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you define a macro variable as LOCAL that will prevent your macro from modifying some existing macro variable that happened to use the same name. So &lt;STRONG&gt;making a macro variable LOCAL will make your macro play nice with others.&lt;/STRONG&gt;&amp;nbsp; But it does not protect its macro variables from being modified by other macros it might call.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2024 15:56:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variables-local-or-not/m-p/940123#M369054</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-20T15:56:31Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables, local or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variables-local-or-not/m-p/940125#M369055</link>
      <description>&lt;P&gt;That is a lovely test case you wrote up!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The point you are missing is that macro variables defined within a macro are not (necessarily) &lt;STRONG&gt;local&lt;/STRONG&gt; by default. Thus if you want a macro variable to be local (which 99.9% of the time is the right design IMHO), you need to use the %LOCAL statement to declare them to be local.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you add a %LOCAL statement to both macros, they will work as expected:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro macro_B();
	%local i ;
	%put macro_B ... ;	
	%do i = 1 %to 5;
		%put macro_B - looping [&amp;amp;=i];
	%end;
	%put macro_B - loop completed [&amp;amp;=i];
%mend;

%macro macro_A();
	%local i ;
	%put macro_A ... ;	
	%do i = 1 %to 3;
		%put macro_A - looping [&amp;amp;=i];
		%macro_B()
	%end;
	%put macro_A - loop completed [&amp;amp;=i];
%mend;

%macro_A()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Without the %local statement, your macro B will not create a local macro variable named i.&amp;nbsp; Instead the %do i=1 %to... statement in the macro B will use the macro variable i that belongs to the macro A.&amp;nbsp; And that will then cause your macro A to stop looping prematurely.&amp;nbsp; These kinds of scope collisions are a common problem if a macro does not declare the macro variables it creates to be %local.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2024 16:04:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variables-local-or-not/m-p/940125#M369055</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-08-20T16:04:57Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables, local or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variables-local-or-not/m-p/940812#M369241</link>
      <description>Brilliant, many thanks all for your prompt help to improve my understanding of local macro variables &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Mon, 26 Aug 2024 07:06:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variables-local-or-not/m-p/940812#M369241</guid>
      <dc:creator>CalleC</dc:creator>
      <dc:date>2024-08-26T07:06:08Z</dc:date>
    </item>
  </channel>
</rss>

