<?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 Help with renaming large groups of variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62374#M13593</link>
    <description>I'm sure there is an easy solution, but I can't seem to figure it out.&lt;BR /&gt;
&lt;BR /&gt;
I just need to rename a large group of variables that contain the prefix E4 to E1.&lt;BR /&gt;
&lt;BR /&gt;
E4CONT&lt;BR /&gt;
E4_AGE&lt;BR /&gt;
E4_DATE&lt;BR /&gt;
&lt;BR /&gt;
rename to:&lt;BR /&gt;
&lt;BR /&gt;
E1CONT&lt;BR /&gt;
E1_AGE&lt;BR /&gt;
E1_DATE&lt;BR /&gt;
&lt;BR /&gt;
I have to do this three times where E4 should be E1, E5 should be E2, and E6 should be E3.&lt;BR /&gt;
&lt;BR /&gt;
Thanks in advance for your help.  I've been struggling with this for awhile.</description>
    <pubDate>Wed, 04 May 2011 20:54:54 GMT</pubDate>
    <dc:creator>statadm</dc:creator>
    <dc:date>2011-05-04T20:54:54Z</dc:date>
    <item>
      <title>Help with renaming large groups of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62374#M13593</link>
      <description>I'm sure there is an easy solution, but I can't seem to figure it out.&lt;BR /&gt;
&lt;BR /&gt;
I just need to rename a large group of variables that contain the prefix E4 to E1.&lt;BR /&gt;
&lt;BR /&gt;
E4CONT&lt;BR /&gt;
E4_AGE&lt;BR /&gt;
E4_DATE&lt;BR /&gt;
&lt;BR /&gt;
rename to:&lt;BR /&gt;
&lt;BR /&gt;
E1CONT&lt;BR /&gt;
E1_AGE&lt;BR /&gt;
E1_DATE&lt;BR /&gt;
&lt;BR /&gt;
I have to do this three times where E4 should be E1, E5 should be E2, and E6 should be E3.&lt;BR /&gt;
&lt;BR /&gt;
Thanks in advance for your help.  I've been struggling with this for awhile.</description>
      <pubDate>Wed, 04 May 2011 20:54:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62374#M13593</guid>
      <dc:creator>statadm</dc:creator>
      <dc:date>2011-05-04T20:54:54Z</dc:date>
    </item>
    <item>
      <title>Re: Help with renaming large groups of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62375#M13594</link>
      <description>Hello Statadm,&lt;BR /&gt;
&lt;BR /&gt;
This is a possible solution:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data i;&lt;BR /&gt;
  E4CONT=1;&lt;BR /&gt;
  E4_AGE=2;&lt;BR /&gt;
  E4_DATE=3;&lt;BR /&gt;
run;&lt;BR /&gt;
%macro a (in=E4, out=E1);&lt;BR /&gt;
proc SQL;&lt;BR /&gt;
  select COUNT(distinct name) as n into :n&lt;BR /&gt;
  from sashelp.vcolumn&lt;BR /&gt;
  where libname="WORK" and memname="I" and SUBSTR(UPCASE(name),1,2)="&amp;amp;in";&lt;BR /&gt;
  %let n=%TRIM(&amp;amp;n);&lt;BR /&gt;
  select distinct name as names into:n1-:n&amp;amp;n&lt;BR /&gt;
  from sashelp.vcolumn&lt;BR /&gt;
  where libname="WORK" and memname="I" and SUBSTR(UPCASE(name),1,2)="&amp;amp;in";&lt;BR /&gt;
quit;&lt;BR /&gt;
data i;&lt;BR /&gt;
  set i;&lt;BR /&gt;
  rename &lt;BR /&gt;
  %do i=1 %to 3;&lt;BR /&gt;
    &amp;amp;&amp;amp;n&amp;amp;i=&amp;amp;out.%SUBSTR(&amp;amp;&amp;amp;n&amp;amp;i,3)  &lt;BR /&gt;
  %end;&lt;BR /&gt;
  ;&lt;BR /&gt;
run;&lt;BR /&gt;
%mend a;&lt;BR /&gt;
%a(in=E1, out=E5);&lt;BR /&gt;
[/pre]&lt;BR /&gt;
Sincerely,&lt;BR /&gt;
SPR</description>
      <pubDate>Wed, 04 May 2011 21:33:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62375#M13594</guid>
      <dc:creator>SPR</dc:creator>
      <dc:date>2011-05-04T21:33:35Z</dc:date>
    </item>
    <item>
      <title>Re: Help with renaming large groups of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62376#M13595</link>
      <description>I am not sure if it is a good idea to rewrite the whole dataset just to rename the variables... I would rather recommend using proc datasets. Something like &lt;A href="http://listserv.uga.edu/cgi-bin/wa?A2=ind0903A&amp;amp;L=sas-l&amp;amp;D=0&amp;amp;H=0&amp;amp;O=T&amp;amp;T=1&amp;amp;P=20268"&gt;this sas-l posting&lt;/A&gt;.</description>
      <pubDate>Wed, 04 May 2011 21:51:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62376#M13595</guid>
      <dc:creator>chang_y_chung_hotmail_com</dc:creator>
      <dc:date>2011-05-04T21:51:23Z</dc:date>
    </item>
    <item>
      <title>Re: Help with renaming large groups of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62377#M13596</link>
      <description>Here is another approach. This macro removes the first character from almost all the variable names in a file. You can easily modify to do what you want.&lt;BR /&gt;
&lt;BR /&gt;
%macro HILDARenameStripFirst(dataset);&lt;BR /&gt;
/* Generates a rename statement of the form&lt;BR /&gt;
   Xvar1=var1 Xvar2=var2 etc&lt;BR /&gt;
   where XvarN includes all variables (except Xwaveid).&lt;BR /&gt;
   Usage example :&lt;BR /&gt;
&lt;BR /&gt;
   data long2;&lt;BR /&gt;
     set hilda.rperson_a (rename=(%HILDARenameStripFirst(hilda.rperson_a)) );&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
   BB. Aug07. I used SAS paper 107-28 by Derek Morgan as inspiration.&lt;BR /&gt;
-------------------------------------------------------------------- */&lt;BR /&gt;
%let DS = %sysfunc(open(&amp;amp;dataset,i)); /* open dataset to get variable names */&lt;BR /&gt;
%if (&amp;amp;DS = 0) %then %put %sysfunc(sysmsg()); /* if cant open */&lt;BR /&gt;
%else &lt;BR /&gt;
  %do i=1 %to %sysfunc(attrn(&amp;amp;DS,NVARS));&lt;BR /&gt;
    %let varname = %sysfunc(varname(&amp;amp;DS,&amp;amp;i)); /* get the ith variable name */&lt;BR /&gt;
    %let newvarname = %substr(&amp;amp;varname,2);    /* strip off the first character */&lt;BR /&gt;
	%if %upcase(&amp;amp;varname)^=XWAVEID %then %do;&lt;BR /&gt;
      %* output code here;&lt;BR /&gt;
      &amp;amp;varname=&amp;amp;newvarname&lt;BR /&gt;
	%end;&lt;BR /&gt;
  %end;&lt;BR /&gt;
%let CloseCode = %sysfunc(close(&amp;amp;DS));&lt;BR /&gt;
%mend HILDARenameStripFirst;</description>
      <pubDate>Thu, 05 May 2011 00:32:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62377#M13596</guid>
      <dc:creator>BruceBrad</dc:creator>
      <dc:date>2011-05-05T00:32:53Z</dc:date>
    </item>
    <item>
      <title>Re: Help with renaming large groups of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62378#M13597</link>
      <description>Are you suggesting I strip off the first two characters and then add back on the prefix that I want?&lt;BR /&gt;
&lt;BR /&gt;
That should be doable. &lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
Kim

Message was edited by: statadm</description>
      <pubDate>Thu, 05 May 2011 13:03:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62378#M13597</guid>
      <dc:creator>statadm</dc:creator>
      <dc:date>2011-05-05T13:03:06Z</dc:date>
    </item>
    <item>
      <title>Re: Help with renaming large groups of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62379#M13598</link>
      <description>The only problem with using this code is that once I remove the prefix, there will be a lot of variables with the same name.  The prefix is what makes the variables different.&lt;BR /&gt;
&lt;BR /&gt;
Is there any way to just change the number in the prefix for all variables to a new number?&lt;BR /&gt;
&lt;BR /&gt;
I use macros to change variables names all the time, but only know how to do this by listing all the variables within a set range.  I have too many variables to do this.&lt;BR /&gt;
&lt;BR /&gt;
Thanks!</description>
      <pubDate>Thu, 05 May 2011 19:19:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62379#M13598</guid>
      <dc:creator>statadm</dc:creator>
      <dc:date>2011-05-05T19:19:31Z</dc:date>
    </item>
    <item>
      <title>Re: Help with renaming large groups of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62380#M13599</link>
      <description>I wouldn't use a macro, possibly just a macro variable created via proc sql.&lt;BR /&gt;
&lt;BR /&gt;
Use proc sql to create a macro variable that contains ALL of the recodes you want to accomplish and then submit the macro variable, as code, within proc datasets like Chang suggested.&lt;BR /&gt;
&lt;BR /&gt;
Should be quite easy to do.&lt;BR /&gt;
&lt;BR /&gt;
Art</description>
      <pubDate>Thu, 05 May 2011 22:45:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62380#M13599</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2011-05-05T22:45:32Z</dc:date>
    </item>
    <item>
      <title>Re: Help with renaming large groups of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62381#M13600</link>
      <description>I was suggesting changing the following line&lt;BR /&gt;
%let newvarname = %substr(&amp;amp;varname,2);&lt;BR /&gt;
to do whatever it is you want to do. Something like the following, if I understand your requirement correctly (untested)&lt;BR /&gt;
%let newvarname = %substr(&amp;amp;varname,1,1) %eval(%substr(&amp;amp;varname,2,1) +5) %substr(&amp;amp;varname,2);</description>
      <pubDate>Fri, 06 May 2011 02:17:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62381#M13600</guid>
      <dc:creator>BruceBrad</dc:creator>
      <dc:date>2011-05-06T02:17:35Z</dc:date>
    </item>
    <item>
      <title>Re: Help with renaming large groups of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62382#M13601</link>
      <description>If you have lots and lots of variables.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data have;&lt;BR /&gt;
  E4CONT=1;&lt;BR /&gt;
  E4_AGE=2;&lt;BR /&gt;
  E4_DATE=3;&lt;BR /&gt;
run;&lt;BR /&gt;
options mprint mlogic symbolgen;&lt;BR /&gt;
%macro rename;&lt;BR /&gt;
proc sql ;&lt;BR /&gt;
   select cats( name,'=',tranwrd(name,'E4','E1'))&lt;BR /&gt;
   from dictionary.columns where libname='WORK' and memname='HAVE';&lt;BR /&gt;
&lt;BR /&gt;
   select cats( name,'=',tranwrd(name,'E4','E1'))&lt;BR /&gt;
    into : rcode1 - : rcode&amp;amp;sqlobs.&lt;BR /&gt;
   from dictionary.columns where libname='WORK' and memname='HAVE';&lt;BR /&gt;
   quit ;&lt;BR /&gt;
proc datasets library=work ;&lt;BR /&gt;
   modify have ;&lt;BR /&gt;
   rename %do i=1 %to &amp;amp;sqlobs.;&lt;BR /&gt;
          &amp;amp;&amp;amp;rcode&amp;amp;i &lt;BR /&gt;
          %end;&lt;BR /&gt;
          ;&lt;BR /&gt;
   quit ;&lt;BR /&gt;
%mend ;&lt;BR /&gt;
&lt;BR /&gt;
%rename&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Fri, 06 May 2011 07:02:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62382#M13601</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-05-06T07:02:36Z</dc:date>
    </item>
    <item>
      <title>Re: Help with renaming large groups of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62383#M13602</link>
      <description>I had trouble getting any of the examples to work, so I took the easy route and did it the amateurish way just so I could get it done.  I thought I would share.  &lt;BR /&gt;
&lt;BR /&gt;
Thank you to everyone for your help.  Sorry for the all caps, I have to program in all CAPS here.&lt;BR /&gt;
***************************************************************;&lt;BR /&gt;
&lt;BR /&gt;
PROC TRANSPOSE DATA=E2 OUT=ONE;&lt;BR /&gt;
 VAR _ALL_;&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
DATA TWO ; SET ONE;&lt;BR /&gt;
  _NAME_ =  TRANWRD(_NAME_,'E4','E1');&lt;BR /&gt;
  _NAME_ =  TRANWRD(_NAME_,'E5','E2');&lt;BR /&gt;
  _NAME_ =  TRANWRD(_NAME_,'E6','E3');&lt;BR /&gt;
&lt;BR /&gt;
DATA THREE; SET TWO;&lt;BR /&gt;
RENAME _NAME_ = NEW;&lt;BR /&gt;
&lt;BR /&gt;
PROC TRANSPOSE DATA=THREE OUT=FOUR;&lt;BR /&gt;
 ID NEW;&lt;BR /&gt;
 IDLABEL _LABEL_;&lt;BR /&gt;
 VAR COL1 COL2;&lt;BR /&gt;
 &lt;BR /&gt;
PROC PRINT;&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: statadm

Message was edited by: statadm</description>
      <pubDate>Fri, 06 May 2011 16:56:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62383#M13602</guid>
      <dc:creator>statadm</dc:creator>
      <dc:date>2011-05-06T16:56:06Z</dc:date>
    </item>
    <item>
      <title>Re: Help with renaming large groups of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62384#M13603</link>
      <description>Or If you like pure dataset code&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data i;&lt;BR /&gt;
  E4CONT=1;&lt;BR /&gt;
  E4_AGE=2;&lt;BR /&gt;
  E4_DATE=3;&lt;BR /&gt;
  E5CONT=1;&lt;BR /&gt;
  E5_AGE=2;&lt;BR /&gt;
  E5_DATE=3;&lt;BR /&gt;
  E6CONT=1;&lt;BR /&gt;
  E6_AGE=2;&lt;BR /&gt;
  E6_DATE=3;&lt;BR /&gt;
run;&lt;BR /&gt;
data _null_;&lt;BR /&gt;
 set sashelp.vcolumn(where=(libname='WORK' and memname='I')) end=last;&lt;BR /&gt;
 if _n_ = 1 then call execute('proc datasets library=work nolist;modify i;rename ');&lt;BR /&gt;
 call execute(strip(name)||'='||translate(name,'123','456'));&lt;BR /&gt;
 if last then call execute(';quit;');&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Mon, 09 May 2011 02:24:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-renaming-large-groups-of-variables/m-p/62384#M13603</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-05-09T02:24:47Z</dc:date>
    </item>
  </channel>
</rss>

