<?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 Macro do loop help in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-help/m-p/701546#M214836</link>
    <description>&lt;P&gt;Hi, instead of writing all the below out for 100 variables&amp;nbsp;I would like to simplify it with a macro do loop. However,&amp;nbsp;I am unsure of how&amp;nbsp;I would do this as I need not only the value of 'i' but the value of 'i'+1 in each iteration of the do loop to make it work. How would I resolve this issue - see&amp;nbsp;in the macro&amp;nbsp;I am trying to write below, I have left question marks where I am unsure how to write a variable that I essentially want to&amp;nbsp;resolve as&amp;nbsp;x(&amp;amp;i.+1). is there any way of actually writing this do loop so I can simplify the x1=x2 x2=x3 etc.?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;rename x1=x2 x2=x3 x3=x4 x4=x5.....x99=x100;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro abc;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;rename %do i=1 %to&amp;nbsp;99; x&amp;amp;i.=x????? %end;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;mend;&lt;/P&gt;&lt;P&gt;%abc;&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 25 Nov 2020 15:00:50 GMT</pubDate>
    <dc:creator>EC27556</dc:creator>
    <dc:date>2020-11-25T15:00:50Z</dc:date>
    <item>
      <title>Macro do loop help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-help/m-p/701546#M214836</link>
      <description>&lt;P&gt;Hi, instead of writing all the below out for 100 variables&amp;nbsp;I would like to simplify it with a macro do loop. However,&amp;nbsp;I am unsure of how&amp;nbsp;I would do this as I need not only the value of 'i' but the value of 'i'+1 in each iteration of the do loop to make it work. How would I resolve this issue - see&amp;nbsp;in the macro&amp;nbsp;I am trying to write below, I have left question marks where I am unsure how to write a variable that I essentially want to&amp;nbsp;resolve as&amp;nbsp;x(&amp;amp;i.+1). is there any way of actually writing this do loop so I can simplify the x1=x2 x2=x3 etc.?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;rename x1=x2 x2=x3 x3=x4 x4=x5.....x99=x100;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro abc;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;rename %do i=1 %to&amp;nbsp;99; x&amp;amp;i.=x????? %end;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;mend;&lt;/P&gt;&lt;P&gt;%abc;&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Nov 2020 15:00:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-help/m-p/701546#M214836</guid>
      <dc:creator>EC27556</dc:creator>
      <dc:date>2020-11-25T15:00:50Z</dc:date>
    </item>
    <item>
      <title>Re: Macro do loop help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-help/m-p/701553#M214840</link>
      <description>&lt;P&gt;Major error: missing % for %mend statement. If you have actually run that code the SAS session is likely unstable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro abc;
data want;
   set have;
   rename 
    %do i=1 %to 99; 
       x&amp;amp;i.=x%eval(&amp;amp;i+1)
    %end;
   ; 
run;
%mend;&lt;/PRE&gt;
&lt;P&gt;Please post code in a code box opened on the forum with either the &amp;lt;/&amp;gt; or "running man" icon.&lt;/P&gt;
&lt;P&gt;Note the indentation to tell where the macro do loop starts and ends a bit easier than all on one line and that the semicolon that ends the Rename is also more visible to that purpose.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the sole purpose of code is to rename variables then you should do it that with Proc Datasets. A data step like this has to process every observation and for large data sets code be quite time consuming. Proc datasets will do this in place.&lt;/P&gt;
&lt;PRE&gt;Proc datasets library=work;
   modify have;
   rename x1=x2
          x2=x3
          &amp;lt;repeat&amp;gt;
   ;
run;
quit;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Nov 2020 15:26:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-help/m-p/701553#M214840</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-11-25T15:26:51Z</dc:date>
    </item>
    <item>
      <title>Re: Macro do loop help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-help/m-p/701661#M214882</link>
      <description>Fab, thank you, works great &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Wed, 25 Nov 2020 19:01:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-help/m-p/701661#M214882</guid>
      <dc:creator>EC27556</dc:creator>
      <dc:date>2020-11-25T19:01:44Z</dc:date>
    </item>
  </channel>
</rss>

