<?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 Rename many variables with similar names in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Rename-many-variables-with-similar-names/m-p/514041#M138579</link>
    <description>&lt;P&gt;I have many variables&amp;nbsp; as the following (Year and Month go from 20170701 until 20170930:&amp;nbsp;&lt;/P&gt;&lt;P&gt;FACE_YEARMONTHDAY&lt;/P&gt;&lt;P&gt;Bucket_YEARMONTHDAY&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have saved a list of this variables in&amp;nbsp;&lt;SPAN&gt;variables1709,&amp;nbsp;variables1708&amp;nbsp; and&amp;nbsp;variables1707 as follows:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;proc contents data= charge.actdays_&amp;amp;yymm. noprint out=conts&amp;amp;yymm; run;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select name into :variables&amp;amp;yymm. separated by " " from conts&amp;amp;yymm where&lt;BR /&gt;(name like "FACE%") or&lt;BR /&gt;(name like "BUCKET%") ;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But i need to rename them so that every variable that begins with "FACE" now begins with "AMT" and every variable that begins with "BUCKET" now begins with "SERVICE".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 16 Nov 2018 19:53:03 GMT</pubDate>
    <dc:creator>gracegon103</dc:creator>
    <dc:date>2018-11-16T19:53:03Z</dc:date>
    <item>
      <title>Rename many variables with similar names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-many-variables-with-similar-names/m-p/514041#M138579</link>
      <description>&lt;P&gt;I have many variables&amp;nbsp; as the following (Year and Month go from 20170701 until 20170930:&amp;nbsp;&lt;/P&gt;&lt;P&gt;FACE_YEARMONTHDAY&lt;/P&gt;&lt;P&gt;Bucket_YEARMONTHDAY&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have saved a list of this variables in&amp;nbsp;&lt;SPAN&gt;variables1709,&amp;nbsp;variables1708&amp;nbsp; and&amp;nbsp;variables1707 as follows:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;proc contents data= charge.actdays_&amp;amp;yymm. noprint out=conts&amp;amp;yymm; run;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select name into :variables&amp;amp;yymm. separated by " " from conts&amp;amp;yymm where&lt;BR /&gt;(name like "FACE%") or&lt;BR /&gt;(name like "BUCKET%") ;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But i need to rename them so that every variable that begins with "FACE" now begins with "AMT" and every variable that begins with "BUCKET" now begins with "SERVICE".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Nov 2018 19:53:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-many-variables-with-similar-names/m-p/514041#M138579</guid>
      <dc:creator>gracegon103</dc:creator>
      <dc:date>2018-11-16T19:53:03Z</dc:date>
    </item>
    <item>
      <title>Re: Rename many variables with similar names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-many-variables-with-similar-names/m-p/514044#M138580</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt; has a great solution here I often use. You'll need to cater it a&lt;BR /&gt;bit to your need but should work just fine for you.&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Renaming-a-group-of-variables-with-the-same-prefix/m-p/148914/highlight/true#M29415" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Renaming-a-group-of-variables-with-the-same-prefix/m-p/148914/highlight/true#M29415&lt;/A&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 16 Nov 2018 20:13:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-many-variables-with-similar-names/m-p/514044#M138580</guid>
      <dc:creator>JoshB</dc:creator>
      <dc:date>2018-11-16T20:13:10Z</dc:date>
    </item>
    <item>
      <title>Re: Rename many variables with similar names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-many-variables-with-similar-names/m-p/514049#M138582</link>
      <description>&lt;P&gt;For a small number of variables you can start with:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data= charge.actdays_&amp;amp;yymm. noprint out=conts&amp;amp;yymm; run;

data _null_;
  set  conts&amp;amp;yymm;
        new_name = tranwrd(name,'FACE_','AMT_');
        new_name = tranwrd(name,'BUCKET_','SERVICE_');
        ....&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But as you need to rename many variables better create a format:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format lib=work;
    value $ren
       'FACE' = 'AMT'
       'BUCKET' = 'SERVICE'
      ....
   ;
run;

data temp;
 set conts&amp;amp;yymm;
      length v1 v2 $40;
      v1 = strip(scan(name,1,'_');
      v2 = strip(putc(v1,$ren.));
      new_name = catx('_',v2,substr(name,length(v2)+1));

    /* next code common to both methods */
     keep name new_name;
run;

data _null_;
  set temp end = eof;
        length ren_list $1000; /* adapt length to max needed */
        retain ren_list;
        ren_list = strip(ren_list) || strip(name) || '=' ||strip(new_name);

      if eof then call symput('REN_LIST", strip(ren_list));
run;

data want;
  set have(rename = (&amp;amp;ren_list));
run;

/* last step can be done with proc datasets in case no need to copy the data */&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 16 Nov 2018 20:43:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-many-variables-with-similar-names/m-p/514049#M138582</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2018-11-16T20:43:11Z</dc:date>
    </item>
    <item>
      <title>Re: Rename many variables with similar names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-many-variables-with-similar-names/m-p/514050#M138583</link>
      <description>&lt;A href="https://gist.github.com/statgeek/82d9f2854edc01560e0f" target="_blank"&gt;https://gist.github.com/statgeek/82d9f2854edc01560e0f&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;You can use the same approach as above, just change what you're putting into the macro variable. Have it generate the text, Bucket0107 to AMT01017 by using some string functions. An example is in the link.</description>
      <pubDate>Fri, 16 Nov 2018 20:40:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-many-variables-with-similar-names/m-p/514050#M138583</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-16T20:40:57Z</dc:date>
    </item>
    <item>
      <title>Re: Rename many variables with similar names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-many-variables-with-similar-names/m-p/514070#M138593</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/246934"&gt;@gracegon103&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have many variables&amp;nbsp; as the following (Year and Month go from 20170701 until 20170930:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FACE_YEARMONTHDAY&lt;/P&gt;
&lt;P&gt;Bucket_YEARMONTHDAY&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have saved a list of this variables in&amp;nbsp;&lt;SPAN&gt;variables1709,&amp;nbsp;variables1708&amp;nbsp; and&amp;nbsp;variables1707 as follows:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;proc contents data= charge.actdays_&amp;amp;yymm. noprint out=conts&amp;amp;yymm; run;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;BR /&gt;select name into :variables&amp;amp;yymm. separated by " " from conts&amp;amp;yymm where&lt;BR /&gt;(name like "FACE%") or&lt;BR /&gt;(name like "BUCKET%") ;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But i need to rename them so that every variable that begins with "FACE" now begins with "AMT" and every variable that begins with "BUCKET" now begins with "SERVICE".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Almost any data set that has variables with dates as part of the name, especially if you are continuing to add them , likely would benefit from transposition to a long data set with an actual date of the value and then the value&lt;/P&gt;
&lt;P&gt;Such as&lt;/P&gt;
&lt;P&gt;DATE&amp;nbsp;&amp;nbsp; Face&amp;nbsp; Bucket&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then it becomes extremely easy to 1) add data as no "new variable names" are needed, 2) perform data queries based on dates 3) group data for summaries by use of date formats.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And as a last benefit you would only be renaming 2(in this case) variables instead of nearly 200.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Nov 2018 22:35:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-many-variables-with-similar-names/m-p/514070#M138593</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-11-16T22:35:12Z</dc:date>
    </item>
    <item>
      <title>Re: Rename many variables with similar names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-many-variables-with-similar-names/m-p/514501#M138743</link>
      <description>&lt;P&gt;Thanks!!! It really helped!&lt;/P&gt;</description>
      <pubDate>Mon, 19 Nov 2018 17:08:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-many-variables-with-similar-names/m-p/514501#M138743</guid>
      <dc:creator>gracegon103</dc:creator>
      <dc:date>2018-11-19T17:08:44Z</dc:date>
    </item>
  </channel>
</rss>

