<?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 multiple variabes using a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738651#M230448</link>
    <description>What are the new names? Show examples of your macro variable lists and how you call it. And the log. &lt;BR /&gt;Add options mprint; before your code, re-run it and include the log.&lt;BR /&gt;&lt;BR /&gt;Also, use PROC DATASETS to rename instead of data step.</description>
    <pubDate>Mon, 03 May 2021 17:11:32 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-05-03T17:11:32Z</dc:date>
    <item>
      <title>Renaming multiple variabes using a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738648#M230446</link>
      <description>&lt;P&gt;I want to rename 276 variables. The old names are nL1-nL276&amp;nbsp; and the new name to be recoded to is contained in a macro variable (&amp;amp;L)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the code I came up with but it only renames nL1 and not the rest of the 275 variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro rename(oldvarlist, newvarlist);&lt;BR /&gt;%let k=1;&lt;BR /&gt;%let old = %scan(&amp;amp;oldvarlist, &amp;amp;k);&lt;BR /&gt;%let new = %scan(&amp;amp;newvarlist, &amp;amp;k);&lt;BR /&gt;%do %while(("&amp;amp;old" NE "") &amp;amp; ("&amp;amp;new" NE ""));&lt;/P&gt;&lt;P&gt;data xx; set xx;&lt;BR /&gt;rename &amp;amp;old = &amp;amp;new;&lt;BR /&gt;run;&lt;BR /&gt;%let k = %eval(&amp;amp;k + 1);&lt;BR /&gt;%let old = %scan(&amp;amp;oldvarlist, &amp;amp;k);&lt;BR /&gt;%let new = %scan(&amp;amp;newvarlist, &amp;amp;k);&lt;BR /&gt;%end;&lt;BR /&gt;%mend rename;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%rename(nL1 - nL276, &amp;amp;L);&lt;/P&gt;</description>
      <pubDate>Mon, 03 May 2021 17:01:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738648#M230446</guid>
      <dc:creator>Neff</dc:creator>
      <dc:date>2021-05-03T17:01:31Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variabes using a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738650#M230447</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;It does not work indeed!&lt;/P&gt;
&lt;P&gt;But what you try to do is also very, very inefficient and greedy.&lt;/P&gt;
&lt;P&gt;You recreate the data set XX for every variable to be renamed (276 times!).&lt;/P&gt;
&lt;P&gt;You should use a PROC DATASETS with a MODIFY statement and then renaming the 276 variables at once.&lt;/P&gt;
&lt;P&gt;This way you alter the metadata (variable names) all at once without going through the observations (not even 1 time).&lt;/P&gt;
&lt;P&gt;I will produce an example!&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 May 2021 17:10:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738650#M230447</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-05-03T17:10:34Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variabes using a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738651#M230448</link>
      <description>What are the new names? Show examples of your macro variable lists and how you call it. And the log. &lt;BR /&gt;Add options mprint; before your code, re-run it and include the log.&lt;BR /&gt;&lt;BR /&gt;Also, use PROC DATASETS to rename instead of data step.</description>
      <pubDate>Mon, 03 May 2021 17:11:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738651#M230448</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-05-03T17:11:32Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variabes using a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738653#M230449</link>
      <description>&lt;P&gt;An example will be greatly appreciated&lt;/P&gt;</description>
      <pubDate>Mon, 03 May 2021 17:21:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738653#M230449</guid>
      <dc:creator>Neff</dc:creator>
      <dc:date>2021-05-03T17:21:17Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variabes using a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738654#M230450</link>
      <description>&lt;P&gt;Here's an example.&lt;/P&gt;
&lt;P&gt;It uses (what I call) 'data-driven code generation' instead of a macro.&lt;/P&gt;
&lt;P&gt;I can turn it into a macro but I hope this solution is OK for you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data oldnew;
input oldvarname $ newvarname $;
cards;
height HHHHH
weight WWWWW
age    AAAAA
;
run;

data test; set sashelp.class; run;

data _NULL_;
if cexist('work.t') then
call execute('PROC CATALOG cat=work.t; delete t.source; run; QUIT;');
run;

filename tt CATALOG 'work.t.t.source';

data _NULL_;
 set oldnew end=last;
 file tt;
if _N_=1 then do;
 PUT 'PROC DATASETS library=WORK NoList;';
 PUT 'modify test;';
 PUT 'rename';
end;
PUT  oldvarname '=' newvarname;
if last then do;
 PUT '; run;';
 PUT 'QUIT;'; 
end;
run;

options source2;
%INCLUDE tt;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Mon, 03 May 2021 17:25:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738654#M230450</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-05-03T17:25:10Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variabes using a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738655#M230451</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _new_list;
 set rename_names end=eof;
 
 
 *start proc datasets;
 if _n_=1 then
    call execute ('proc datasets lib=WORK nodetails nolist; change');
    
 
 *pass new and old name to proc datasets;
 call execute (oldname);
 call execute ('=');
 call execute (new_name);
 
 *if last record then quit;
 If eof then
    call execute (';run;quit;');
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Modified from a reverse example. This assumes you have a data set with old_name and new_name.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/ba360dd1cf748bf6d0da7e2e16aafe66" target="_blank"&gt;https://gist.github.com/statgeek/ba360dd1cf748bf6d0da7e2e16aafe66&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FYI - if it's just a prefix a RENAME statement works fine.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rename NL1-NL256 = START1-START256;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 May 2021 17:25:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738655#M230451</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-05-03T17:25:26Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variabes using a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738660#M230453</link>
      <description>&lt;P&gt;First thing is that the macro language does not use variable lists, so the parameter value of nL1 - nL276 is one piece of text and you have nothing in you code that actually parses out the "names" for all of the variable names that I think you intend.&lt;/P&gt;
&lt;P&gt;As an absolute minimum you need to show what the &amp;amp;L parameter looks like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This may show part of what is happening:&lt;/P&gt;
&lt;PRE&gt;75   %macro dummy(oldvarlist, newvarlist);
76   %let k=1;
77   %let old = %scan(&amp;amp;oldvarlist, &amp;amp;k);
78   %let new = %scan(&amp;amp;newvarlist, &amp;amp;k);
79   %do %while(("&amp;amp;old" NE "") &amp;amp; ("&amp;amp;new" NE ""));
80   %put Old: &amp;amp;old.   New: &amp;amp;new.;
81   %let k = %eval(&amp;amp;k + 1);
82   %let old = %scan(&amp;amp;oldvarlist, &amp;amp;k);
83   %let new = %scan(&amp;amp;newvarlist, &amp;amp;k);
84   %end;
85   %mend dummy;
86
87   %dummy(A1-A10, B1-B10);
Old: A1   New: B1
Old: A10   New: B10
&lt;/PRE&gt;
&lt;P&gt;I "assumed" that you newvarlist was provided in a manner similar to the old var. Note that only the first and last are processed when that happens.&lt;/P&gt;
&lt;P&gt;Or if the newvarlist is provided in a different manner:&lt;/P&gt;
&lt;PRE&gt;88   %dummy(A1-A10, B1 B3 B5);
Old: A1   New: B1
Old: A10   New: B3
&lt;/PRE&gt;
&lt;P&gt;In any case the the OLD list only ever has 2 values so only 2 are used from the "new" list and quite likely not the correct one.&lt;/P&gt;
&lt;P&gt;If your "old" list is &lt;EM&gt;&lt;U&gt;&lt;STRONG&gt;always&lt;/STRONG&gt;&lt;/U&gt;&lt;/EM&gt; a sequential list then you could provide a "start" and "end" along with at prefix for parameters such as :&lt;/P&gt;
&lt;PRE&gt;97   %macro dumdum(stem=, start=,end=);
98
99   %do i=&amp;amp;start. %to &amp;amp;end.;
100  %put varname is: &amp;amp;stem.&amp;amp;i;
101  %end;
102  %mend;
103
104  %dumdum(stem=AL,start=3, end=6)
varname is: AL3
varname is: AL4
varname is: AL5
varname is: AL6
&lt;/PRE&gt;
&lt;P&gt;But without knowing what your "new" list actually looks like I can't at this time make a suggestion on matching the old and new.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And create a rename list in proc datasets . The syntax would look like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc datasets libname=&amp;lt;library the dataset resides&amp;gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; modify datasetname;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; rename&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; oldname1 = newname1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; oldname2 = newname2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;repeat as needed&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; ;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;quit;&lt;/STRONG&gt; /*Data sets is one of the procedures that has run group processing and requires QUIT to end*/&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 May 2021 17:34:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738660#M230453</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-03T17:34:00Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variabes using a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738661#M230454</link>
      <description>&lt;P&gt;Example using data set SASHELP.PRICEDATA and variables price1-price10. Requires the %EXPANDVARLIST macro from&amp;nbsp; &lt;A href="https://support.sas.com/resources/papers/proceedings13/032-2013.pdf" target="_blank" rel="noopener"&gt;https://support.sas.com/resources/papers/proceedings13/032-2013.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro rename(oldvarlist, newvarlist, datasetname);
%local i inputlist;
%let inputlist=%expandvarlist(data=&amp;amp;datasetname,var=&amp;amp;oldvarlist);

proc datasets library=work nolist;
    modify &amp;amp;datasetname;
    rename
    %do i=1 %to %sysfunc(countw(&amp;amp;inputlist));
            %scan(&amp;amp;inputlist,&amp;amp;i,%str( )) = %scan(&amp;amp;newvarlist,&amp;amp;i,%str( ))
    %end;
    ;
quit;
%mend rename;

data pricedata;
    set sashelp.pricedata;
run;
%rename(price1-price10, name1 name2 name3 george hector gorilla dog cat ocelot guinea_pig, pricedata)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 May 2021 18:13:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738661#M230454</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-05-03T18:13:02Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variabes using a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738670#M230457</link>
      <description>&lt;P&gt;Why are you passing in two parameters if the list of original names is known?&lt;/P&gt;
&lt;P&gt;You might re-do the macro to just generate the OLD=NEW pairs.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro rename(newvarlist);
  %local i ;
  %do i=1 %to %sysfunc(countw(&amp;amp;newvarlist,%str( ));
      NL&amp;amp;i=%scan(&amp;amp;newvarlist,&amp;amp;i,%str( ))
  %end;
%mend rename;

proc datasets nolist lib=work;
  modify xx ;
    rename %rename(&amp;amp;L);
  run;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could modify it to allow a prefix for the old variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro rename2(prefix,newvarlist);
  %local i ;
  %do i=1 %to %sysfunc(countw(&amp;amp;newvarlist,%str( ));
     &amp;amp;prefix&amp;amp;i=%scan(&amp;amp;newvarlist,&amp;amp;i,%str( ))
  %end;
%mend rename2;

... rename %rename(NL,&amp;amp;L);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really want a macro that allows you to use variable lists then you will need to do more work to translate the variable lists into an actual list of variables.&amp;nbsp; Here is a sketch of how you might do that using PROC TRANSPOSE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let oldlist=A B C1-C5 D ;
%let newlist=AGE RACE INCOME2001-INCOME2005 OUTCOME ;

data ;
  retain &amp;amp;oldlist;
  call missing(of &amp;amp;oldlist);
  stop;
run;
proc transpose out=oldnames name=old; run;
data ;
  retain &amp;amp;newlist;
  call missing(of &amp;amp;newlist);
  stop;
run;
proc transpose out=newnames name=new; run;
data pairs ;
  set oldnames end=eof1;
  set newnames end=eof2;
  length rename $32767;
  if upcase(old) ne upcase(new) then 
    rename=catx(' ',rename,catx('=',nliteral(old),nliteral(new)))
  ;
  retain rename;
  if eof1 or eof2 then call symputx('rename',rename);
run;

proc datasets nolist lib=work;
  modify xx ;
  rename &amp;amp;rename ;
  run;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Resulting list of rename pairs:&lt;/P&gt;
&lt;PRE&gt;891   %put &amp;amp;rename;
A=AGE B=RACE C1=INCOME2001 C2=INCOME2002 C3=INCOME2003 C4=INCOME2004 C5=INCOME2005 D=OUTCOME

&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 May 2021 18:37:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738670#M230457</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-03T18:37:09Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming multiple variabes using a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738684#M230464</link>
      <description>&lt;P&gt;Thank you all for the answers provided&lt;/P&gt;</description>
      <pubDate>Mon, 03 May 2021 19:24:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-multiple-variabes-using-a-macro/m-p/738684#M230464</guid>
      <dc:creator>Neff</dc:creator>
      <dc:date>2021-05-03T19:24:21Z</dc:date>
    </item>
  </channel>
</rss>

