<?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 Renaming variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables/m-p/70074#M15176</link>
    <description>I'm trying to achieve, what I thought would be, a simple goal. I've got a data table with text variables named m0, m1 up to m62. Each month this dataset is updated, with the latest month's data stored in m0 while all the rest need to move up on index, i.e. m0 should be renamed m1, m1 should be renamed m2, etc. up to m62 which should be renamed m63. &lt;BR /&gt;
&lt;BR /&gt;
What would be the best way to accomplish this in SAS? Any help will be much appreciated.</description>
    <pubDate>Mon, 06 Sep 2010 07:40:13 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2010-09-06T07:40:13Z</dc:date>
    <item>
      <title>Renaming variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables/m-p/70074#M15176</link>
      <description>I'm trying to achieve, what I thought would be, a simple goal. I've got a data table with text variables named m0, m1 up to m62. Each month this dataset is updated, with the latest month's data stored in m0 while all the rest need to move up on index, i.e. m0 should be renamed m1, m1 should be renamed m2, etc. up to m62 which should be renamed m63. &lt;BR /&gt;
&lt;BR /&gt;
What would be the best way to accomplish this in SAS? Any help will be much appreciated.</description>
      <pubDate>Mon, 06 Sep 2010 07:40:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variables/m-p/70074#M15176</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-09-06T07:40:13Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables/m-p/70075#M15177</link>
      <description>Hi&lt;BR /&gt;
&lt;BR /&gt;
Hope the code below will give you the idea:&lt;BR /&gt;
&lt;BR /&gt;
data previous_all_months (drop=i);&lt;BR /&gt;
  length id 8;&lt;BR /&gt;
  array m {64} 8 m0 - m63;&lt;BR /&gt;
  do id=1 to 2;&lt;BR /&gt;
    do i= 1 to dim(m);&lt;BR /&gt;
      m(i)=i;&lt;BR /&gt;
    end;&lt;BR /&gt;
    output;&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data new_month;&lt;BR /&gt;
  id=1;&lt;BR /&gt;
  m0=101;&lt;BR /&gt;
  output;&lt;BR /&gt;
&lt;BR /&gt;
  id=2;&lt;BR /&gt;
  m0=102;&lt;BR /&gt;
  output;&lt;BR /&gt;
&lt;BR /&gt;
  id=3;&lt;BR /&gt;
  m0=103;&lt;BR /&gt;
  output;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
/*  create table current_all_months as*/&lt;BR /&gt;
    select&lt;BR /&gt;
      coalesce(p.id,n.id) as id&lt;BR /&gt;
      ,n.m0&lt;BR /&gt;
      ,p.m0 as m1&lt;BR /&gt;
      ,p.m1 as m2&lt;BR /&gt;
      ,p.m2 as m3&lt;BR /&gt;
      ,p.m3 as m4&lt;BR /&gt;
      /* and so on until p.m62 as m63 */&lt;BR /&gt;
    from previous_all_months p full join new_month n&lt;BR /&gt;
      on p.id = n.id&lt;BR /&gt;
    order by id&lt;BR /&gt;
  ;&lt;BR /&gt;
quit; &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Better would be to have the data organised in the following way:&lt;BR /&gt;
ID, Date, Var.&lt;BR /&gt;
&lt;BR /&gt;
Adding a new month would then simply be a Proc Append. &lt;BR /&gt;
Getting rid of an old month would simply be a where clause 'where Date GT &lt;SOME sas="" date=""&gt;&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick&lt;/SOME&gt;</description>
      <pubDate>Mon, 06 Sep 2010 09:57:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variables/m-p/70075#M15177</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-09-06T09:57:41Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables/m-p/70076#M15178</link>
      <description>I agree with Patrick recommendation - convert your file to be stored "vertically" for maintenance/update/change and then consider a SAS view or another parallel-file technique to give the "horizontal" perspective.  This would be similar to generating a PivotTable report output with time periods across the columns based on existing master file data that is stored as one observation period time-period.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Mon, 06 Sep 2010 14:54:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variables/m-p/70076#M15178</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-09-06T14:54:56Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables/m-p/70077#M15179</link>
      <description>Thanks Patrick. I am new to SAS so it was extremely frustrating when I could not get this simple task done. Next time I will know what to do :).</description>
      <pubDate>Mon, 06 Sep 2010 23:44:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variables/m-p/70077#M15179</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-09-06T23:44:30Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variables/m-p/70078#M15180</link>
      <description>You "can" do this by renaming, using descending ranges.  Using descending ranges prevents renaming to a name that already exists.  &lt;BR /&gt;
&lt;BR /&gt;
Rename requires descending ranges to have the have leading zeros or you get this error.  &lt;BR /&gt;
[pre]&lt;BR /&gt;
2587     merge trans current(rename=(m63-m0=m64-m1));&lt;BR /&gt;
ERROR: Either roots don't match or start suffix after end suffix.&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Which is fine because SAS allows us to rename a range to a new range with leading zeros then rename it again using the descending range with leading zeros.  See example.&lt;BR /&gt;
&lt;BR /&gt;
I also included a step to determine the largest M variable name, so the code would work from month to month. &lt;BR /&gt;
&lt;BR /&gt;
You really should keep the data in tall form, but it's fun to see how rename works.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data Trans;&lt;BR /&gt;
   do id = 1 to 3;&lt;BR /&gt;
      m0 = ranuni(1243);&lt;BR /&gt;
      output;&lt;BR /&gt;
      end;&lt;BR /&gt;
   run;&lt;BR /&gt;
data Current;&lt;BR /&gt;
   do id = 1 to 2;&lt;BR /&gt;
      array m&lt;LI&gt; m0-m24;&lt;BR /&gt;
      do _n_ = 1 to dim(m);&lt;BR /&gt;
         m[_n_]=_n_; &lt;BR /&gt;
         end;&lt;BR /&gt;
      output;&lt;BR /&gt;
      end;&lt;BR /&gt;
   run;&lt;BR /&gt;
proc print;&lt;BR /&gt;
   run;&lt;BR /&gt;
* Find max M variable;&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
   select max(input(substr(name,2),8.)) into :m &lt;BR /&gt;
      from dictionary.columns&lt;BR /&gt;
      where libname='WORK' and memname='CURRENT' and upcase(name) eqT 'M'&lt;BR /&gt;
      ;&lt;BR /&gt;
   quit;&lt;BR /&gt;
   run;&lt;BR /&gt;
%let M=&amp;amp;m;&lt;BR /&gt;
%let P=%eval(&amp;amp;m+1);&lt;BR /&gt;
%put NOTE: M=&amp;amp;m P=&amp;amp;p;&lt;BR /&gt;
&lt;BR /&gt;
data new(/*drop=M&amp;amp;p*/);&lt;BR /&gt;
   merge trans &lt;BR /&gt;
      current&lt;BR /&gt;
         (&lt;BR /&gt;
            rename=&lt;BR /&gt;
               (&lt;BR /&gt;
               /* This is too complicated&lt;BR /&gt;
               m0-m&amp;amp;m      =  m0000-m00&amp;amp;m &lt;BR /&gt;
               m00&amp;amp;m-m0000 =  m00&amp;amp;p-m0001&lt;BR /&gt;
               m0001-m00&amp;amp;p =  m1-m&amp;amp;p&lt;BR /&gt;
               */ &lt;BR /&gt;
               m0-m&amp;amp;m   =  _0-_&amp;amp;m&lt;BR /&gt;
               _0-_&amp;amp;m   =  m1-m&amp;amp;p&lt;BR /&gt;
&lt;BR /&gt;
               )&lt;BR /&gt;
         );&lt;BR /&gt;
   by id;&lt;BR /&gt;
   run;&lt;BR /&gt;
proc contents varnum;&lt;BR /&gt;
   run;&lt;BR /&gt;
proc print;&lt;BR /&gt;
   run;&lt;BR /&gt;
[/pre]

Complex method replaced by simpler rename&lt;BR /&gt;
&lt;BR /&gt;
    &lt;BR /&gt;
Message was edited by: data _null_;&lt;/LI&gt;</description>
      <pubDate>Tue, 07 Sep 2010 13:50:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variables/m-p/70078#M15180</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2010-09-07T13:50:01Z</dc:date>
    </item>
  </channel>
</rss>

