<?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: Macro to rename variables with the same prefix in SAS Studio</title>
    <link>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673740#M9300</link>
    <description>how would I proceed, can you help me with more details ?</description>
    <pubDate>Fri, 31 Jul 2020 15:10:27 GMT</pubDate>
    <dc:creator>skavli</dc:creator>
    <dc:date>2020-07-31T15:10:27Z</dc:date>
    <item>
      <title>Macro to rename variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673659#M9289</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I'm new to sas macro and i'm wondering how I could rename the prefix of my variables that have the same prefix using a sas macro, I have found something in the internet but it does not seem to fit, can you help me please ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class="language-sas"&gt;&lt;CODE&gt;%macro RenameList(vars= , prefix=);
   %let list=;
   %do i = 1 %to %sysfunc(countw(&amp;amp;vars));
      %let var=%scan(&amp;amp;vars,&amp;amp;i);
      %let list= &amp;amp;list %sysfunc(catx(=,&amp;amp;var,&amp;amp;prefix.&amp;amp;var));
   %end;
   &amp;amp;list
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 31 Jul 2020 10:43:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673659#M9289</guid>
      <dc:creator>skavli</dc:creator>
      <dc:date>2020-07-31T10:43:29Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to rename variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673660#M9290</link>
      <description>&lt;P&gt;Do you want to rename all variables or just some of them?&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jul 2020 10:52:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673660#M9290</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-07-31T10:52:01Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to rename variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673672#M9291</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/299342"&gt;@skavli&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have found something in the internet but it does not seem to fit&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why doesn't it fit? You need to explain in more detail.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, please provide two or three small examples of what you are trying to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And why does the solution have to be a macro?&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jul 2020 11:27:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673672#M9291</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-07-31T11:27:01Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to rename variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673678#M9292</link>
      <description>I have approximatively 70 variables like this IPN_RAORAOR1 .... IPN_RAORAOR41 IPN_VOLVOL1 ... IPN_VOLVOL29 and i want to substitute the prefixes with another string, I want to use a macro to be able to do that to other datasets where i might encouter the same issue</description>
      <pubDate>Fri, 31 Jul 2020 11:50:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673678#M9292</guid>
      <dc:creator>skavli</dc:creator>
      <dc:date>2020-07-31T11:50:47Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to rename variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673682#M9293</link>
      <description>&lt;PRE&gt;data test;
set work.bdd_media_physique_2020_hebdo;
run;

%macro RenameList(vars= , prefix=);
   %let list=;
   %do i = 1 %to %sysfunc(countw(&amp;amp;vars));
      %let var=%scan(&amp;amp;vars,&amp;amp;i);
      %let list= &amp;amp;list %sysfunc(catx(=,&amp;amp;var,&amp;amp;prefix.&amp;amp;var));
   %end;
   &amp;amp;list
%mend;

data test2;
set test;
run;

%RenameList(vars = IPN_AFFAFF: IPN_RAORAO: IPN_VOLVOL:, prefix = IPN_AFFR IPN_RADON IPN_VOL);&lt;/PRE&gt;&lt;P&gt;Knowing that my variables have prefixes like what I mentioned in the code (IPN_AFFAFF IPN_RAORAO IPN_VOLVOL ) however the ending is definitely not the same and the variables does not end with the successive numbers, for the macro question i want to use a macro to be able to use elswhere on other datasets where i might encounter the same issue&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jul 2020 11:55:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673682#M9293</guid>
      <dc:creator>skavli</dc:creator>
      <dc:date>2020-07-31T11:55:41Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to rename variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673695#M9294</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/299342"&gt;@skavli&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I have approximatively 70 variables like this IPN_RAORAOR1 .... IPN_RAORAOR41 IPN_VOLVOL1 ... IPN_VOLVOL29 and i want to substitute the prefixes with another string, I want to use a macro to be able to do that to other datasets where i might encouter the same issue&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No macro needed. A macro variable (not the same thing as a macro) is needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=have out=_contents_ noprint;
run;
proc sql noprint;
    select cats(name,'=new_',substr(name,5)) into :renames separated by ' ' from have where name eqt 'IPN_';
run;
proc datasets library=work;
    modify have;
    rename &amp;amp;renames;
run; quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This renames variables in data set WORK.HAVE whose names begin with IPN_ to begin with NEW_&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jul 2020 12:14:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673695#M9294</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-07-31T12:14:56Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to rename variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673700#M9295</link>
      <description>&lt;P&gt;You are not using the macro the way it was intended.&amp;nbsp; First it just generates part of a statement so you need to call it in a RENAME statement or RENAME= option.&amp;nbsp; Second the macro does not support variable lists, only lists of actual variable names. And third the macro does not support multiple different prefixes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First let's fix the logic of the macro.&amp;nbsp; There is no reason to use CATX() for macro variable concatenation. Just expand the macro variables next to each other.&amp;nbsp; Also it does not make its local macro variables local.&amp;nbsp; So the macro could be re-written as this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro RenameList(vars= , prefix=);
   %local i var;
   %do i = 1 %to %sysfunc(countw(&amp;amp;vars,%str( )));
     %let var=%scan(&amp;amp;vars,&amp;amp;i,%str( ));
     &amp;amp;var=&amp;amp;prefix.&amp;amp;var
   %end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now how can we make a way that you can use a variable list like&amp;nbsp;&lt;FONT face="courier new,courier"&gt;IPN_AFFAFF: IPN_RAORAO: IPN_VOLVOL:&lt;/FONT&gt; ?&lt;/P&gt;
&lt;P&gt;Here is a way using PROC TRANSPOSE and PROC SQL INTO to convert VARLIST into NAMELIST.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let varlist=IPN_AFFAFF: IPN_RAORAO: IPN_VOLVOL: ;
proc transpose data=have (obs=0 keep=&amp;amp;varlist)  out=names ; var _all_; run;
proc sql noprint;
select _name_ into :namelist separated by ' ' 
from names
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now can we make it use multiple prefixes?&amp;nbsp; It would be more complex to try to do it the style of the first macro that only generates part of a statement.&amp;nbsp; (somethings called a "function" macro).&amp;nbsp; So let's not bother and either just write a program or write a macro that generate multiple SAS steps.&amp;nbsp; SO just use a loop to take each pair from VARLIST and PREFIXLIST and build up one long rename list.&amp;nbsp; Then run a step that will use the list to do the renaming.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro rename(inds,outds,varlist,prefixlist);
%local i vars prefix namelist renames;
%do i=1 %to %sysfunc(countw(&amp;amp;varlist,%str( )));
  %let vars=%scan(&amp;amp;varlist,&amp;amp;i,%str( ));
  %let prefix=%scan(&amp;amp;prefixlist,&amp;amp;i,%str( ));
proc transpose data=&amp;amp;inds(obs=0 keep=&amp;amp;vars) out=names; var _all_; run;
proc sql noprint;
  select _name_ into : namelist separated by ' ' from names;
quit;
  %let renames=&amp;amp;renames %renamelist(vars=&amp;amp;namelist,prefix=&amp;amp;prefix);
%end;

data &amp;amp;outds;
  set &amp;amp;inds(rename=(&amp;amp;renames));
run;
%mend ; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now your call becomes:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%renamet(inds=test,outds=test2
,varlist = IPN_AFFAFF: IPN_RAORAO: IPN_VOLVOL:
,prefixlist = IPN_AFFR IPN_RADON IPN_VOL
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jul 2020 15:18:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673700#M9295</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-31T15:18:36Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to rename variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673731#M9297</link>
      <description>&lt;P&gt;You should realize that you are asking for complexity when simplicity will suffice.&amp;nbsp; With variables that have the same prefix and a numbered suffix, renaming is easy.&amp;nbsp; Consider:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rename IPN_RAORAOR1 - INP_RAORAOR41 = prefixA_1 - prefixA_41
       IPN_VOLVOL1 - IPNVOLVOL29    = prefixB_1 - prefixB_29;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 31 Jul 2020 14:50:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673731#M9297</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-07-31T14:50:53Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to rename variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673734#M9298</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/299342"&gt;@skavli&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I have approximatively 70 variables like this IPN_RAORAOR1 .... IPN_RAORAOR41 IPN_VOLVOL1 ... IPN_VOLVOL29 and i want to substitute the prefixes with another string, I want to use a macro to be able to do that to other datasets where i might encouter the same issue&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Note that the currently selected answer does NOT do that.&amp;nbsp; It will instead add an additional prefix onto the variables, not replace the existing prefix with something else.&amp;nbsp; To do that you will need a different method.&amp;nbsp; Either a query of the variable names (form one of the metadata tables/views or PROC CONTENTS output) of if the names are simple X1-X5 type lists just a simple RENAME statement.&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jul 2020 14:57:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673734#M9298</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-31T14:57:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to rename variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673737#M9299</link>
      <description>I haven't expressed my problem very well, each IPN_RAORAOR does not have successive numbers in the end for example I have IPN_RAORAOR17 then IPN_RAORAOR48 sometimes IPN_RAORAOX1 etc thing is i just wanna replace the IPN_RAORAO by IPN_RAO and so on for other variables</description>
      <pubDate>Fri, 31 Jul 2020 14:59:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673737#M9299</guid>
      <dc:creator>skavli</dc:creator>
      <dc:date>2020-07-31T14:59:40Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to rename variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673740#M9300</link>
      <description>how would I proceed, can you help me with more details ?</description>
      <pubDate>Fri, 31 Jul 2020 15:10:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673740#M9300</guid>
      <dc:creator>skavli</dc:creator>
      <dc:date>2020-07-31T15:10:27Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to rename variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673744#M9301</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/299342"&gt;@skavli&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;how would I proceed, can you help me with more details ?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The non-macro code I provided earlier should work.&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jul 2020 15:17:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673744#M9301</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-07-31T15:17:43Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to rename variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673747#M9302</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/299342"&gt;@skavli&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I haven't expressed my problem very well, each IPN_RAORAOR does not have successive numbers in the end for example I have IPN_RAORAOR17 then IPN_RAORAOR48 sometimes IPN_RAORAOX1 etc thing is i just wanna replace the IPN_RAORAO by IPN_RAO and so on for other variables&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=have out=contents noprint; run;
data mapping;
  set contents (keep=name);
  newname=upcase(name);
  if newname =: 'IPN_RAORAO' then
   newname= 'IPN_RAO' || substr(name,length( 'IPN_RAORAO')+1)
  ; 
run;

proc sql noprint;
  select catx('=',name,newname) into :renames separted by ' '
  from mapping where upcase(name) ne upcase(newname)
  ;
quit;

data want;
  set have;
  rename &amp;amp;renames;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 31 Jul 2020 15:24:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Macro-to-rename-variables-with-the-same-prefix/m-p/673747#M9302</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-31T15:24:56Z</dc:date>
    </item>
  </channel>
</rss>

