<?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 column names in reversed order for suffix  in the column names in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Rename-column-names-in-reversed-order-for-suffix-in-the-column/m-p/624398#M183937</link>
    <description>&lt;P&gt;Much easier if they actually share the same prefix before the numeric suffix.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  rename x1-x5=x5-x1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 13 Feb 2020 03:14:38 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-02-13T03:14:38Z</dc:date>
    <item>
      <title>Rename column names in reversed order for suffix  in the column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-column-names-in-reversed-order-for-suffix-in-the-column/m-p/624393#M183934</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help me out to change the column names with suffix by&amp;nbsp; digit number in ascending order to descending order?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&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; have;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; a_1 $ b_2 $ c_3 $ d_4 e_5;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;cards&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;a . a 1 3&lt;/P&gt;
&lt;P&gt;. b . 2 4&lt;/P&gt;
&lt;P&gt;a a a . 5&lt;/P&gt;
&lt;P&gt;. . b 3 5&lt;/P&gt;
&lt;P&gt;a a a . 6&lt;/P&gt;
&lt;P&gt;a a a . 7&lt;/P&gt;
&lt;P&gt;a a a 2 8&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;run&lt;/STRONG&gt;&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;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; want;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; a_5 $ b_4 $ c_3 $ d_2 e_1;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;cards&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;a . a 1 3&lt;/P&gt;
&lt;P&gt;. b . 2 4&lt;/P&gt;
&lt;P&gt;a a a . 5&lt;/P&gt;
&lt;P&gt;. . b 3 5&lt;/P&gt;
&lt;P&gt;a a a . 6&lt;/P&gt;
&lt;P&gt;a a a . 7&lt;/P&gt;
&lt;P&gt;a a a 2 8&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance &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;Suzy&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2020 02:05:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-column-names-in-reversed-order-for-suffix-in-the-column/m-p/624393#M183934</guid>
      <dc:creator>Suzy_Cat</dc:creator>
      <dc:date>2020-02-13T02:05:29Z</dc:date>
    </item>
    <item>
      <title>Re: Rename column names in reversed order for suffix  in the column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-column-names-in-reversed-order-for-suffix-in-the-column/m-p/624398#M183937</link>
      <description>&lt;P&gt;Much easier if they actually share the same prefix before the numeric suffix.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  rename x1-x5=x5-x1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Feb 2020 03:14:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-column-names-in-reversed-order-for-suffix-in-the-column/m-p/624398#M183937</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-13T03:14:38Z</dc:date>
    </item>
    <item>
      <title>Re: Rename column names in reversed order for suffix  in the column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-column-names-in-reversed-order-for-suffix-in-the-column/m-p/624399#M183938</link>
      <description>&lt;P&gt;Cool . Nice and easy. Thank you Tom!&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2020 03:32:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-column-names-in-reversed-order-for-suffix-in-the-column/m-p/624399#M183938</guid>
      <dc:creator>Suzy_Cat</dc:creator>
      <dc:date>2020-02-13T03:32:37Z</dc:date>
    </item>
    <item>
      <title>Re: Rename column names in reversed order for suffix  in the column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-column-names-in-reversed-order-for-suffix-in-the-column/m-p/624401#M183940</link>
      <description>&lt;P&gt;Essentially you want to use PROC DATASETS to do a rename of the variables in your dataset.&amp;nbsp;&amp;nbsp; You can use the DICTIONAIRES capability of PROC SQL to fetch metadata (number of variables,&amp;nbsp; variable names, and variable position) to construct a rename expression to use in PROC DATASETS.&amp;nbsp; Something like this will&amp;nbsp;work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input a_1 $ b_2 $ c_3 $ d_4 e_5;
cards;
a . a 1 3
. b . 2 4
a a a . 5
. . b 3 5
a a a . 6
a a a . 7
a a a 2 8
;

proc sql noprint;
  select cats(nvar) into :nv from dictionary.tables   
    where libname='WORK' and memname='HAVE' ;
  %put &amp;amp;=nv;

  select cats(name,'=',scan(name,1,'_'),'_',&amp;amp;nv+1-varnum) 
    into :rename_list separated by ' ' from dictionary.columns
    where libname='WORK' and memname='HAVE';
  %put =&amp;amp;rename_list;
quit;

proc datasets library=work  nolist ;
  modify have;
  rename &amp;amp;rename_list ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The proc sql first gets the number of variables into macrovar NV.&amp;nbsp; Then it gets all the variable names (in storage order), appends and equal sign,&amp;nbsp; and then append just the first part of the variable name, and an underscore, and finaly a number calculated as &amp;amp;NV+1-varnum, where varnum is the storage position.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This produces the rename statement in PRFOC DATASETS, as below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    rename a_1=a_5   b_2=b_4  c_3=c_3  d_4=d_2  e_5=e_1;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But even though it works, it also generates the error message below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;841  proc datasets library=work  nolist ;
842    modify have;
843    rename &amp;amp;rename_list ;
NOTE: Renaming variable a_1 to a_5.
NOTE: Renaming variable b_2 to b_4.
ERROR: Variable c_3 already exists on file WORK.HAVE.
NOTE: Renaming variable d_4 to d_2.
NOTE: Renaming variable e_5 to e_1.
844  quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can eliminate that by taking out the "c_3=c_3" component of the rename macrovar, by adding&amp;nbsp;the where condition in proc SQL to include&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;"and&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; scan(name,-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;,&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;'_'&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;) ^= cats(&amp;amp;nv+&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;-varnum)"&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select cats(nvar) into :nv from dictionary.tables   
    where libname='WORK' and memname='HAVE' ;
  %put &amp;amp;=nv;

  select cats(name,'=',scan(name,1,'_'),'_',&amp;amp;nv+1-varnum) 
    into :rename_list separated by ' ' from dictionary.columns
    where libname='WORK' and memname='HAVE'
    and scan(name,-1,'_') ^= cats(&amp;amp;nv+1-varnum);
  %put &amp;amp;=rename_list;
quit;

proc datasets library=work  nolist ;
  modify have;
  rename &amp;amp;rename_list ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This takes advantage of some "magic" in the CATS (concatenate and strip trailing/leading blanks), because it accepts not only lists of character values (&lt;STRONG&gt;'=' and '_'&lt;/STRONG&gt;) and variables (i.e. the variable &lt;STRONG&gt;name&lt;/STRONG&gt;), but also numeric variables&amp;nbsp;(&lt;STRONG&gt;varnum&lt;/STRONG&gt;)&amp;nbsp; and &lt;EM&gt;&lt;STRONG&gt;numeric expressions (&amp;amp;nv+1-varnum)&lt;/STRONG&gt;&lt;/EM&gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2020 03:49:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-column-names-in-reversed-order-for-suffix-in-the-column/m-p/624401#M183940</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-02-13T03:49:31Z</dc:date>
    </item>
  </channel>
</rss>

