<?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: Renaming a string of variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667234#M199756</link>
    <description>&lt;P&gt;I may run into a jam where they are not sequential.&amp;nbsp; Can you help?&lt;/P&gt;</description>
    <pubDate>Mon, 06 Jul 2020 16:32:05 GMT</pubDate>
    <dc:creator>Bluekeys49</dc:creator>
    <dc:date>2020-07-06T16:32:05Z</dc:date>
    <item>
      <title>Renaming a string of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667229#M199751</link>
      <description>&lt;P&gt;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; cursor: text; font-family: inherit; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;I have a string of 50 variables named VAR_0001, VAR_0002, VAR_0003,...,&lt;/SPAN&gt;VAR_0050.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to rename them to abbreviated names of VR_1, VR_2, VR_3,...VR_50.&amp;nbsp; Thanks in advance for help!&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 16:22:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667229#M199751</guid>
      <dc:creator>Bluekeys49</dc:creator>
      <dc:date>2020-07-06T16:22:04Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming a string of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667230#M199752</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rename var_0001-var_0050 = vr_1-vr_50;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can rename a series easily as above.&lt;/P&gt;
&lt;P&gt;If your variable names are not sequential as in your example, you will need to use a different approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294090"&gt;@Bluekeys49&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; cursor: text; font-family: inherit; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;I have a string of 50 variables named VAR_0001, VAR_0002, VAR_0003,...,&lt;/SPAN&gt;VAR_0050.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to rename them to abbreviated names of VR_1, VR_2, VR_3,...VR_50.&amp;nbsp; Thanks in advance for help!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 16:24:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667230#M199752</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-06T16:24:24Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming a string of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667234#M199756</link>
      <description>&lt;P&gt;I may run into a jam where they are not sequential.&amp;nbsp; Can you help?&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 16:32:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667234#M199756</guid>
      <dc:creator>Bluekeys49</dc:creator>
      <dc:date>2020-07-06T16:32:05Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming a string of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667236#M199758</link>
      <description>Is there a pattern or not?&lt;BR /&gt;If not, how do you know what the new names of each variable will be?</description>
      <pubDate>Mon, 06 Jul 2020 16:34:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667236#M199758</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-06T16:34:41Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming a string of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667243#M199761</link>
      <description>This previous answer to a similar question may help:&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#M29415" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Renaming-a-group-of-variables-with-the-same-prefix/m-p/148914#M29415&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 06 Jul 2020 17:22:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667243#M199761</guid>
      <dc:creator>ketpt42</dc:creator>
      <dc:date>2020-07-06T17:22:14Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming a string of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667249#M199764</link>
      <description>&lt;P&gt;From data I am working with now, yes, they are in sequential order.&amp;nbsp; However, another data set I am working with has variable names with hyphens in them (and not underscores).&amp;nbsp; Will the rename function work the same way as my original example?&amp;nbsp; For example, &lt;SPAN style="background-color: #ffffff; box-sizing: border-box; color: #333333; cursor: text; display: inline; float: none; font-family: inherit; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;VAR-0001, VAR-0002, VAR-0003,...,&lt;/SPAN&gt;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 150%; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;VAR-0050.&amp;nbsp; How do I rename them to shorter version WITH underscores so that they are VR_1, VR_2, VR3,...VR50?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 17:52:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667249#M199764</guid>
      <dc:creator>Bluekeys49</dc:creator>
      <dc:date>2020-07-06T17:52:35Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming a string of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667253#M199766</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rename 'var-001'n - 'var-0050'n = VR_1 - VR_50;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you're reading multiple files that are supposed to have the same layout and then you plan to combine them, this would not be the best approach. Instead, while reading the files you should be specifying the names and types and then you can read them either all at once or all with the same specifications to make your life easier when you try and analyze the data. Or at least set the option so that you're not importing data with hyphens in the variable name. The following option ensures that spaces and hyphens are converted to underscores automatically when importing the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;option validvarname = v7;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294090"&gt;@Bluekeys49&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;From data I am working with now, yes, they are in sequential order.&amp;nbsp; However, another data set I am working with has variable names with hyphens in them (and not underscores).&amp;nbsp; Will the rename function work the same way as my original example?&amp;nbsp; For example, &lt;SPAN style="background-color: #ffffff; box-sizing: border-box; color: #333333; cursor: text; display: inline; float: none; font-family: inherit; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;VAR-0001, VAR-0002, VAR-0003,...,&lt;/SPAN&gt;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 150%; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;VAR-0050.&amp;nbsp; How do I rename them to shorter version WITH underscores so that they are VR_1, VR_2, VR3,...VR50?&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 17:58:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667253#M199766</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-06T17:58:39Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming a string of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667255#M199767</link>
      <description>&lt;P&gt;Thank you very much! &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 18:06:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667255#M199767</guid>
      <dc:creator>Bluekeys49</dc:creator>
      <dc:date>2020-07-06T18:06:09Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming a string of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667260#M199769</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294090"&gt;@Bluekeys49&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I may run into a jam where they are not sequential.&amp;nbsp; Can you help?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In such case you may need a more complicated method, as attached here:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
     array v {*} var_0001-var_0004;
     do i=1 to 4; v(i)=i; end;
     output; 
run;

proc sql noprint;
    select name into :vlist separated by ' '
     from dictionary.columns  
     where libname='WORK' and memname='HAVE' ;
quit;
%put VLIST=&amp;amp;vlist;

data _null_;
  length ren_list $1200 ix $2;
  dsid = open('test');
  do i=1 to 5; /* adapt to max suffix or even bigger */
     ix = put(i,2.);
     varname = 'var_'||put(i,z4.); 
     newname = 'vr_'||strip(ix); 
     if findw("&amp;amp;vlist",varname) then do;
     if findw("&amp;amp;vlist",varname) then        
        ren_list = compbl(ren_list||varname||'='||newname||' ');
     else  put varname= ' not found';
  end;
  call symput('REN_LIST', trim(ren_list));
run;
%put REN_LIST=&amp;amp;ren_list;

proc datasets lib=work;
   modify have;
   rename &amp;amp;ren_list;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Jul 2020 18:15:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-a-string-of-variables/m-p/667260#M199769</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-07-06T18:15:39Z</dc:date>
    </item>
  </channel>
</rss>

