<?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 loops indexes in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/macro-loops-indexes/m-p/601611#M16742</link>
    <description>&lt;P&gt;Always use %local to define variables as local that are not needed elsewhere.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro macro1();
%local i;
	%do i = 1 %to 5;
		%put first_level_macro: &amp;amp;i.;
		%macro2;
	%end;
%mend;

%macro macro2();
%local i;
	%do i = 1 %to 5;
		%put second_level_macro: &amp;amp;i.;
	%end;
%mend;

%macro1&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 05 Nov 2019 10:08:57 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-11-05T10:08:57Z</dc:date>
    <item>
      <title>macro loops indexes</title>
      <link>https://communities.sas.com/t5/New-SAS-User/macro-loops-indexes/m-p/601610#M16741</link>
      <description>&lt;P&gt;Hello!&amp;nbsp;&lt;/P&gt;&lt;P&gt;for a project I need to make macro invocations at different levels in do loops (see next example) so inside a master macro I have to loop over other ones and these macros that I invoke can do de same. My problem is that if I use the same index in the do loop in the master macro and the invoked macro (in this case "i") when the invoked macro is executed it modifies the index of the loop in the master macro, resulting in an unexpected result:&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro macro1();
	%do i = 1 %to 5;
		%put first_level_macro: &amp;amp;i.;
		%macro2;
	%end;
%mend;

%macro macro2();
	%do i = 1 %to 5;
		%put second_level_macro: &amp;amp;i.;
	%end;
%mend;

%macro1;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Result:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;first_level_macro: 1
second_level_macro: 1
second_level_macro: 2
second_level_macro: 3
second_level_macro: 4
second_level_macro: 5&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired result:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;first_level_macro: 1
second_level_macro: 1
second_level_macro: 2
second_level_macro: 3
second_level_macro: 4
second_level_macro: 5

first_level_macro: 2
second_level_macro: 1
second_level_macro: 2
second_level_macro: 3
second_level_macro: 4
second_level_macro: 5

first_level_macro: 3
second_level_macro: 1
second_level_macro: 2
second_level_macro: 3
second_level_macro: 4
second_level_macro: 5

first_level_macro: 4
second_level_macro: 1
second_level_macro: 2
second_level_macro: 3
second_level_macro: 4
second_level_macro: 5

first_level_macro: 5
second_level_macro: 1
second_level_macro: 2
second_level_macro: 3
second_level_macro: 4
second_level_macro: 5&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I know that I can simply change the name of the index in the master macro, but given the requirements of my project this is not possible, because we are integrating a lot of macros at several levels and we can not change the indexes names on each one of them.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to solve this without changing the names of the indexes?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Nov 2019 10:05:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/macro-loops-indexes/m-p/601610#M16741</guid>
      <dc:creator>jazambrano2211</dc:creator>
      <dc:date>2019-11-05T10:05:42Z</dc:date>
    </item>
    <item>
      <title>Re: macro loops indexes</title>
      <link>https://communities.sas.com/t5/New-SAS-User/macro-loops-indexes/m-p/601611#M16742</link>
      <description>&lt;P&gt;Always use %local to define variables as local that are not needed elsewhere.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro macro1();
%local i;
	%do i = 1 %to 5;
		%put first_level_macro: &amp;amp;i.;
		%macro2;
	%end;
%mend;

%macro macro2();
%local i;
	%do i = 1 %to 5;
		%put second_level_macro: &amp;amp;i.;
	%end;
%mend;

%macro1&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Nov 2019 10:08:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/macro-loops-indexes/m-p/601611#M16742</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-11-05T10:08:57Z</dc:date>
    </item>
    <item>
      <title>Re: macro loops indexes</title>
      <link>https://communities.sas.com/t5/New-SAS-User/macro-loops-indexes/m-p/602261#M16804</link>
      <description>&lt;P&gt;FWIW this is almost exactly the example I used to help teach basic scope of macro variables and likely problems with nested macro calls.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And the solution as ably pointed out by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2019 22:47:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/macro-loops-indexes/m-p/602261#M16804</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-11-06T22:47:16Z</dc:date>
    </item>
  </channel>
</rss>

