<?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 many variables at once from a data set list in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Renaming-many-variables-at-once-from-a-data-set-list/m-p/394386#M95024</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/40498"&gt;@jcis7&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Sorry but I don't understand your question/the remaining problem. Can you please explain a bit more in detail ideally illustrated with some sample data?&lt;/P&gt;</description>
    <pubDate>Sat, 09 Sep 2017 01:43:38 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2017-09-09T01:43:38Z</dc:date>
    <item>
      <title>Renaming many variables at once from a data set list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-many-variables-at-once-from-a-data-set-list/m-p/394380#M95020</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let prioryr=PY;&lt;BR /&gt;&lt;BR /&gt;data want;
	set have;
  array rename

    TOT_ENROL TOT_ALLRE TOT_DTP TOT_POLIO TOT_MMR TOT_HEPB TOT_VARI

    PB_ENROL PB_ALLRE PB_DTP PB_POLIO PB_MMR PB_HEPB PB_VARI

    PR_ENROL PR_ALLRE PR_DTP PR_POLIO PR_MMR PR_HEPB PR_VARI;

	do i=1 to dim(rename);
	rename(i)=rename(i)_&amp;amp;prioryr;
	end;
run;&lt;/PRE&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the SAS code above with current variable names (more then those listed but I shortened the list).&amp;nbsp; I need to rename the variables by adding the prior year (&amp;amp;prioryr) to the end of each variable name, ie TOT_ENROL_PY TOT_ALLRE_PY&amp;nbsp; ...etc.&amp;nbsp; PB_ENROL_PY, PB_ALLRE_PY..ETC, PR_ENROL_PY, PR_ALLRE_PY...etc&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried using an array but that didn't work.&lt;/P&gt;
&lt;P&gt;I realized an array changes the value of the variable itself vs its actual name.&amp;nbsp;Is there an alternative to using the rename statement for each variable&amp;nbsp;in the set statement and dropping the&amp;nbsp;old variables in the data statement?&amp;nbsp; &amp;nbsp;Any help is much appreciated!&amp;nbsp; Thanks so much!&lt;/P&gt;</description>
      <pubDate>Sat, 09 Sep 2017 00:30:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-many-variables-at-once-from-a-data-set-list/m-p/394380#M95020</guid>
      <dc:creator>jcis7</dc:creator>
      <dc:date>2017-09-09T00:30:46Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming many variables at once from a data set list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-many-variables-at-once-from-a-data-set-list/m-p/394382#M95021</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/40498"&gt;@jcis7&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Something like below should do.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample;
  array have {*} 8
    TOT_ENROL TOT_ALLRE TOT_DTP TOT_POLIO TOT_MMR TOT_HEPB TOT_VARI
    PB_ENROL PB_ALLRE PB_DTP PB_POLIO PB_MMR PB_HEPB PB_VARI
    PR_ENROL PR_ALLRE PR_DTP PR_POLIO PR_MMR PR_HEPB PR_VARI;
run;

%let prioryr=PY;
proc datasets lib=work nolist;
  modify sample;
  rename 
    TOT_ENROL =TOT_ENROL_&amp;amp;prioryr
    TOT_ALLRE =TOT_ALLRE_&amp;amp;prioryr
    TOT_DTP =TOT_DTP_&amp;amp;prioryr
    TOT_POLIO =TOT_POLIO_&amp;amp;prioryr
    TOT_MMR =TOT_MMR_&amp;amp;prioryr
    TOT_HEPB =TOT_HEPB_&amp;amp;prioryr
    TOT_VARI =TOT_VARI_&amp;amp;prioryr
    PB_ENROL =PB_ENROL_&amp;amp;prioryr
    PB_ALLRE =PB_ALLRE_&amp;amp;prioryr
    PB_DTP =PB_DTP_&amp;amp;prioryr
    PB_POLIO =PB_POLIO_&amp;amp;prioryr
    PB_MMR =PB_MMR_&amp;amp;prioryr
    PB_HEPB =PB_HEPB_&amp;amp;prioryr
    PB_VARI =PB_VARI_&amp;amp;prioryr
    PR_ENROL =PR_ENROL_&amp;amp;prioryr
    PR_ALLRE =PR_ALLRE_&amp;amp;prioryr
    PR_DTP =PR_DTP_&amp;amp;prioryr
    PR_POLIO =PR_POLIO_&amp;amp;prioryr
    PR_MMR =PR_MMR_&amp;amp;prioryr
    PR_HEPB =PR_HEPB_&amp;amp;prioryr
    PR_VARI =PR_VARI_&amp;amp;prioryr
  ;
  run;
  contents data=sample;
  run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 09 Sep 2017 01:13:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-many-variables-at-once-from-a-data-set-list/m-p/394382#M95021</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-09-09T01:13:10Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming many variables at once from a data set list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-many-variables-at-once-from-a-data-set-list/m-p/394385#M95023</link>
      <description>It worked to rename the variables.  However, my original dataset has number values for each variable.  How do I rename the variables and retain the values of each observation?   Sorry I wasn't clearn.  Thank you!&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sat, 09 Sep 2017 01:39:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-many-variables-at-once-from-a-data-set-list/m-p/394385#M95023</guid>
      <dc:creator>jcis7</dc:creator>
      <dc:date>2017-09-09T01:39:59Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming many variables at once from a data set list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-many-variables-at-once-from-a-data-set-list/m-p/394386#M95024</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/40498"&gt;@jcis7&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Sorry but I don't understand your question/the remaining problem. Can you please explain a bit more in detail ideally illustrated with some sample data?&lt;/P&gt;</description>
      <pubDate>Sat, 09 Sep 2017 01:43:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-many-variables-at-once-from-a-data-set-list/m-p/394386#M95024</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-09-09T01:43:38Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming many variables at once from a data set list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-many-variables-at-once-from-a-data-set-list/m-p/394394#M95027</link>
      <description>&lt;P&gt;You'll want a method that's more dynamic, consider using the SASHELP tables to build your rename statement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example that renames variables wiht the suffix of _DATE.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/82d9f2854edc01560e0f" target="_blank"&gt;https://gist.github.com/statgeek/82d9f2854edc01560e0f&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Sep 2017 03:07:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-many-variables-at-once-from-a-data-set-list/m-p/394394#M95027</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-09-09T03:07:29Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming many variables at once from a data set list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-many-variables-at-once-from-a-data-set-list/m-p/394404#M95029</link>
      <description>&lt;P&gt;Your syntax is wrong. You cannot use array references in a RENAME statement, just the actual names.&lt;/P&gt;
&lt;P&gt;If sounds like you want some method to make it easier to add a suffix to a series of names?&lt;/P&gt;
&lt;P&gt;Personally if you are just doing it once then any good editor should let you generate the statement with a couple&amp;nbsp;of copy and pastes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also write a quick program to generate a macro variable with the list of oldname=newname pairs.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let varlist= 
TOT_ENROL TOT_ALLRE TOT_DTP TOT_POLIO TOT_MMR TOT_HEPB TOT_VARI 
PB_ENROL PB_ALLRE PB_DTP PB_POLIO PB_MMR PB_HEPB PB_VARI 
PR_ENROL PR_ALLRE PR_DTP PR_POLIO PR_MMR PR_HEPB PR_VARI
;
data _null_;
  length old $32 rename $4000 ;
  do i=1 to countw("&amp;amp;varlist");
    old=scan("&amp;amp;varlist",i);
    rename=catx(' ',catx('=',old,catx('_',old,'PY')));
  end;
  call symputx('rename',rename);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then use the macro variable in a RENAME statement. &amp;nbsp;Either in a simple data step or a dataset option or even a PROC DATASETS step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  rename &amp;amp;rename ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Your list looks even easier as it apears to really be three lists with different prefixes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  length prefix old $32 rename $4000 ;
  do prefix='TOT','PB','PR';
    do old = 'ENROL','ALLRE','DTP','POLIO','MMR','HEPB','VARI';
       old=catx('_',prefix,old);
       rename=catx(' ',catx('=',old,catx('_',old,'PY')));
    end;
  end;
  call symputx('rename',rename);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Sep 2017 04:06:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-many-variables-at-once-from-a-data-set-list/m-p/394404#M95029</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-09-09T04:06:55Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming many variables at once from a data set list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-many-variables-at-once-from-a-data-set-list/m-p/394405#M95030</link>
      <description>Or, assuming this is because you have multiple years of data, append the data NOT merge, and then use OROC TRANSPOSE to flip the data.</description>
      <pubDate>Sat, 09 Sep 2017 04:09:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-many-variables-at-once-from-a-data-set-list/m-p/394405#M95030</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-09-09T04:09:21Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming many variables at once from a data set list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-many-variables-at-once-from-a-data-set-list/m-p/394501#M95076</link>
      <description>&lt;PRE&gt;

Here is an example.



data class;
 set sashelp.class;
run;





%let prioryr=PY;
data _null_;
 set sashelp.vcolumn(keep=libname memname name where=(libname='WORK' and memname='CLASS')) end=last;
 if _n_=1 then call execute('proc datasets library=work nolist nodetails; modify class;');
 call execute(cat('rename ',name,'=',cats(name,"_&amp;amp;prioryr"),';'));
 if last then call execute(';quit;');
run;
 
&lt;/PRE&gt;</description>
      <pubDate>Sun, 10 Sep 2017 12:24:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-many-variables-at-once-from-a-data-set-list/m-p/394501#M95076</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-09-10T12:24:38Z</dc:date>
    </item>
  </channel>
</rss>

