<?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: Help with Macro Variables in Do-Loops in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-Variables-in-Do-Loops/m-p/373932#M89479</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "i" you use for looping is a column of your datase not a macrovariable so you&lt;/P&gt;
&lt;P&gt;can not refer to it with "&amp;amp;i".&lt;/P&gt;
&lt;P&gt;You can use a macro loop such as&lt;/P&gt;
&lt;P&gt;%do i=1 %to &amp;amp;NUMROWS.;&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and refer to your indexed macrovariables with :&lt;/P&gt;
&lt;P&gt;&amp;amp;&amp;amp;OUTTABLE&amp;amp;i.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note the "&amp;amp;&amp;amp;" as &amp;amp;OUTABLE&amp;amp;i. would separately try to resolve &amp;amp;OUTABLE and &amp;amp;i. (and fail).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 07 Jul 2017 12:12:03 GMT</pubDate>
    <dc:creator>gamotte</dc:creator>
    <dc:date>2017-07-07T12:12:03Z</dc:date>
    <item>
      <title>Help with Macro Variables in Do-Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-Variables-in-Do-Loops/m-p/373893#M89456</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking for assistance on what should hopefully be a simple question &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to build the following table, using the macro variables defined below, and the DO-loop.&amp;nbsp; I'm having problems with the Target variable, as this is taking the number suffix from the loop counter...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;i&lt;/TD&gt;&lt;TD&gt;CellID&lt;/TD&gt;&lt;TD&gt;Target&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Work.Out1&lt;/TD&gt;&lt;TD&gt;Work.Out1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Work.Out1&lt;/TD&gt;&lt;TD&gt;Work.Out2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;Work.Out1&lt;/TD&gt;&lt;TD&gt;Work.Out3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;Work.Out1&lt;/TD&gt;&lt;TD&gt;Work.Out4&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%LET&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; NUMROWS=4;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%LET&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; OUTTABLE1=Work.Out1;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%LET&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; OUTTABLE2=Work.Out2;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%LET&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; OUTTABLE3=Work.Out3;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%LET&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; OUTTABLE4=Work.Out4;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;Data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Cell_Splits;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;DO&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; i=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;TO&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; &amp;amp;NUMROWS;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;CellID = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"&amp;amp;OUTTABLE1"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Target = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"&amp;amp;OUTTABLE&amp;amp;i"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;Output&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;END&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;Run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be greatly appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jul 2017 11:10:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-Variables-in-Do-Loops/m-p/373893#M89456</guid>
      <dc:creator>HoaTruong</dc:creator>
      <dc:date>2017-07-07T11:10:03Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Macro Variables in Do-Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-Variables-in-Do-Loops/m-p/373896#M89458</link>
      <description>&lt;P&gt;Sorry, why are you using macro variables for this? &amp;nbsp;Its a simple data task:&lt;/P&gt;
&lt;PRE&gt;data want (drop=i);
  do i=1 to 4;
    cellid=cats("work.out",put(i,1.));
    target=cellid;&lt;BR /&gt;  end;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Jul 2017 11:19:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-Variables-in-Do-Loops/m-p/373896#M89458</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-07-07T11:19:14Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Macro Variables in Do-Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-Variables-in-Do-Loops/m-p/373897#M89459</link>
      <description>&lt;P&gt;My advice is skip the macros and use a DATA step. &amp;nbsp;Always remember that the macro substituions occur before the data step runs. &amp;nbsp;&amp;amp;I (if it actually existed) would have one value. &amp;nbsp;The data step variable i does exist and it has 4 values: 1 to 4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Data Cell_Splits;
DO i=1 TO 4;
CellID = 'Work.Out1';
Target = cats('Woork.Data',i);
Output;
END;
Run;
proc print; run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Jul 2017 11:19:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-Variables-in-Do-Loops/m-p/373897#M89459</guid>
      <dc:creator>WarrenKuhfeld</dc:creator>
      <dc:date>2017-07-07T11:19:54Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Macro Variables in Do-Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-Variables-in-Do-Loops/m-p/373898#M89460</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Cell_Splits;
DO i=1 TO &amp;amp;NUMROWS;
CellID = "&amp;amp;OUTTABLE1";
Target = "&amp;amp;OUTTABLE&amp;amp;i";
Output;
END;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I see no place in your code where macro variable i is defined.&lt;/P&gt;
&lt;P&gt;Data step variables cannot be used as macro variables; the macro &lt;STRONG&gt;pre&lt;/STRONG&gt;processor does its work &lt;U&gt;before&lt;/U&gt; the data step is compiled and long before the data step runs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you literally define all the macro variables anyway, why don't you use a simple datastep with datalines?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cell_splits;
length cellid target $20;
input cellid target;
datalines;
Work.Out1 Work.Out1
Work.Out1 Work.Out2
Work.Out1 Work.Out3
Work.Out1 Work.Out4
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;or even simpler&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cell_splits;
retain cellid = "Work.Out1";
input target :$20.;
datalines;
Work.Out1
Work.Out2
Work.Out3
Work.Out4
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Jul 2017 11:22:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-Variables-in-Do-Loops/m-p/373898#M89460</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-07-07T11:22:15Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Macro Variables in Do-Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-Variables-in-Do-Loops/m-p/373913#M89467</link>
      <description>&lt;P&gt;Thanks for your reply.&amp;nbsp; When this code is implemented, it won't know what values to use in the table beforehand.&amp;nbsp; Even though, I've assigned simple values to the macro variables, the code will need to deal with any values that will passed to it.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jul 2017 11:46:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-Variables-in-Do-Loops/m-p/373913#M89467</guid>
      <dc:creator>HoaTruong</dc:creator>
      <dc:date>2017-07-07T11:46:14Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Macro Variables in Do-Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-Variables-in-Do-Loops/m-p/373917#M89469</link>
      <description>&lt;P&gt;When you have lists of values, it is best to keep them in datasets and work off those.&lt;/P&gt;
&lt;P&gt;To know how to implement this principle, we'd have to see your code.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jul 2017 11:50:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-Variables-in-Do-Loops/m-p/373917#M89469</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-07-07T11:50:13Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Macro Variables in Do-Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-Variables-in-Do-Loops/m-p/373932#M89479</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "i" you use for looping is a column of your datase not a macrovariable so you&lt;/P&gt;
&lt;P&gt;can not refer to it with "&amp;amp;i".&lt;/P&gt;
&lt;P&gt;You can use a macro loop such as&lt;/P&gt;
&lt;P&gt;%do i=1 %to &amp;amp;NUMROWS.;&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and refer to your indexed macrovariables with :&lt;/P&gt;
&lt;P&gt;&amp;amp;&amp;amp;OUTTABLE&amp;amp;i.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note the "&amp;amp;&amp;amp;" as &amp;amp;OUTABLE&amp;amp;i. would separately try to resolve &amp;amp;OUTABLE and &amp;amp;i. (and fail).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jul 2017 12:12:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-Variables-in-Do-Loops/m-p/373932#M89479</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2017-07-07T12:12:03Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Macro Variables in Do-Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-Variables-in-Do-Loops/m-p/374190#M89556</link>
      <description>&lt;P&gt;So you have a series of macro variables and you want to create a dataset.&lt;/P&gt;
&lt;P&gt;You can use the SYMGET() function to retreive the value of a macro variable by its name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  length i 8 CellID Target $41 ;
  do i=1 to &amp;amp;numrows ;
    cellid=symget(cats('outtable',i));
    target=cellid;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Although it probably would have been better to make the dataset directly instead of first creating the series of macro variables.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Jul 2017 20:03:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-Variables-in-Do-Loops/m-p/374190#M89556</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-07-08T20:03:47Z</dc:date>
    </item>
  </channel>
</rss>

