<?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: Rename variables using a loop - data step in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-variables-using-a-loop-data-step/m-p/337280#M22419</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Well, its really not a good idea to name a range of variables with the incrementor not at the end of the name. &amp;nbsp;The reason is that you can't use inbuilt lists as easily with such a setup e.g:&lt;/P&gt;
&lt;PRE&gt;array period_sum_{23}; 
or
sum(of period_sum_:);

Versus (in your method):
array abc{23} period_1_sum--period_23_sum;
or 
sum(of period_1_sum--period_23_sum);&lt;/PRE&gt;
&lt;P&gt;A further tip here is that if you were to normalise your data, i.e. having the data go down rather than across, you would make your programming simpler - and you would be inline with industry standards such as CDISC.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, if you really have to go this way, then you don't need macro att all, nor do you need to loop as:&lt;/P&gt;
&lt;PRE&gt;data want;
  set train;
  array period_sum_{&amp;amp;num_period.};
run;&lt;/PRE&gt;
&lt;P&gt;Will effectively create the 23 elements for you, no need to loop.&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;
&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>Thu, 02 Mar 2017 09:45:26 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-03-02T09:45:26Z</dc:date>
    <item>
      <title>Rename variables using a loop - data step</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-variables-using-a-loop-data-step/m-p/337185#M22405</link>
      <description>&lt;P&gt;I have a macro variable, named 'Num_period', which was used to create new columns.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%global Num_period;
%let Num_period=23;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now, names for those new columns have a pattern like:&lt;/P&gt;&lt;P&gt;Period_1_Sum,&amp;nbsp;&lt;SPAN&gt;Period_2_Sum, ... , Period_23_Sum.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Now we would like to use a loop, and a rename function in data step to rename all columns to:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;0, 1, 2 ... 22;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The codes I expect would be something like:&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
      set train;

      do i = 1 to &amp;amp;Num_period;
          rename Period_{i}_Sum {i}-1;
      end;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;The codes provided above aren't correct obviously. Could you provide the correct way of doing it?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 23:35:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-variables-using-a-loop-data-step/m-p/337185#M22405</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-03-01T23:35:49Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variables using a loop - data step</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-variables-using-a-loop-data-step/m-p/337186#M22406</link>
      <description>&lt;P&gt;If your trying to change 1 thru 23 to be 0 thru 22, then wrapping your code in a macro would do it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro doit(Num_Period=23);
  data test;
      set train;
      %do i = 1 %to &amp;amp;Num_period;
          rename Period_&amp;amp;i._Sum=Period_%eval(&amp;amp;i.-1)_Sum;
      %end;
  run;
 %mend doit;

%doit
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 23:54:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-variables-using-a-loop-data-step/m-p/337186#M22406</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-01T23:54:17Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variables using a loop - data step</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-variables-using-a-loop-data-step/m-p/337188#M22407</link>
      <description>Thanks! Can we just use the number (e.g. 0) as the column name? so instead of 'Period_0_Sum', it would be just '0'.</description>
      <pubDate>Wed, 01 Mar 2017 23:59:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-variables-using-a-loop-data-step/m-p/337188#M22407</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-03-01T23:59:31Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variables using a loop - data step</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-variables-using-a-loop-data-step/m-p/337189#M22408</link>
      <description>&lt;P&gt;No! SAS variables have to start with either a letter or underscore. I was asking if you wanted:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Period_1_Sum to&amp;nbsp;&lt;SPAN&gt;Period_&lt;/SPAN&gt;&lt;SPAN&gt;23&lt;/SPAN&gt;&lt;SPAN&gt;_Sumto be reccoded to&amp;nbsp;Period_0_Sum to&amp;nbsp;Period_22&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;That is what the macro I suggested would do.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Art, CEO, AnalystFinder.com&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2017 00:04:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-variables-using-a-loop-data-step/m-p/337189#M22408</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-02T00:04:35Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variables using a loop - data step</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-variables-using-a-loop-data-step/m-p/337280#M22419</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Well, its really not a good idea to name a range of variables with the incrementor not at the end of the name. &amp;nbsp;The reason is that you can't use inbuilt lists as easily with such a setup e.g:&lt;/P&gt;
&lt;PRE&gt;array period_sum_{23}; 
or
sum(of period_sum_:);

Versus (in your method):
array abc{23} period_1_sum--period_23_sum;
or 
sum(of period_1_sum--period_23_sum);&lt;/PRE&gt;
&lt;P&gt;A further tip here is that if you were to normalise your data, i.e. having the data go down rather than across, you would make your programming simpler - and you would be inline with industry standards such as CDISC.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, if you really have to go this way, then you don't need macro att all, nor do you need to loop as:&lt;/P&gt;
&lt;PRE&gt;data want;
  set train;
  array period_sum_{&amp;amp;num_period.};
run;&lt;/PRE&gt;
&lt;P&gt;Will effectively create the 23 elements for you, no need to loop.&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;
&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>Thu, 02 Mar 2017 09:45:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-variables-using-a-loop-data-step/m-p/337280#M22419</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-03-02T09:45:26Z</dc:date>
    </item>
  </channel>
</rss>

