<?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: rename many variables using array? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617708#M181053</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks all for your replies. The reason why I'm trying to do so is that I have many "character" variables that actually have numeric values. Let's say A, B, and C. Using array, I generated A2, B2, and C2(, which are numeric now) and dropped A, B, and C, (characters). Thereafter, I wanted to rename A2, B2, and C2 to A, B, and C, respectively. Many thanks for having informed me.&lt;/P&gt;</description>
    <pubDate>Thu, 16 Jan 2020 08:30:54 GMT</pubDate>
    <dc:creator>braam</dc:creator>
    <dc:date>2020-01-16T08:30:54Z</dc:date>
    <item>
      <title>rename many variables using array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617575#M181007</link>
      <description>&lt;P&gt;Can I use array to "rename" many variables? I know there are some other ways using SQL, but don't know if it's possible with array. The first code works of course, but the second doesn't. I'm also curious why the second code with array doesn't work. Perhaps I don't understand the array's characteristics.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
	data want; set sashelp.cars;
		rename Model= Mo;
		rename Type= Ty;
		rename Origin= Or;
		run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	data want; set sashelp.cars;
		array VAR 	Model Type Origin;
		array TEMP	Mo Ty Or;

		do i=1 to dim(VAR);
			rename VAR[i]= TEMP[i];
		end;
		run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jan 2020 21:11:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617575#M181007</guid>
      <dc:creator>braam</dc:creator>
      <dc:date>2020-01-15T21:11:11Z</dc:date>
    </item>
    <item>
      <title>Re: rename many variables using array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617578#M181009</link>
      <description>&lt;P&gt;You can't use an array to rename variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the variable names all have a pattern (such as x1-x27) and the renamed variables have a pattern (such as paige1-paige27) — which by the way are highly recommended variable names &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&amp;nbsp; — then you can resort to some PROC SQL trickery. If there is no pattern, as in your example, you'd have to type the rename statement yourself.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2020 21:19:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617578#M181009</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-15T21:19:21Z</dc:date>
    </item>
    <item>
      <title>Re: rename many variables using array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617581#M181011</link>
      <description>&lt;P&gt;The RENAME statement needs the names of the variables.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't need so many RENAME statements.&amp;nbsp; One statement can include as many pairs of old=new names as you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rename
  Model= Mo 
 Type= Ty
 Origin= Or
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jan 2020 21:22:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617581#M181011</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-01-15T21:22:27Z</dc:date>
    </item>
    <item>
      <title>Re: rename many variables using array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617618#M181021</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;If the variable names all have a pattern (such as x1-x27) and the renamed variables have a pattern (such as paige1-paige27) — which by the way are highly recommended variable names &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&amp;nbsp; — then you can resort to some PROC SQL trickery.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;: I think you mean the RENAME statement in PROC DATASETS, which indeed doesn't accept variable lists -- unlike the RENAME statement of the DATA step where &lt;EM&gt;numbered&lt;/EM&gt; variable lists are allowed:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rename x1-x27 = paige1-paige27;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Even if the ranges differ (but have the same length):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rename x1-x27 = paige20-paige46;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the pattern is &lt;EM&gt;not&lt;/EM&gt; of this special form, &lt;EM&gt;then&lt;/EM&gt; I would start thinking about "PROC SQL trickery".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Replicating &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279507"&gt;@braam&lt;/a&gt;'s example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select cats(name,'=',substr(name,1,2)) into :renlist separated by ' ' 
from dictionary.columns
where libname='SASHELP' &amp;amp; memname='CARS' &amp;amp; 2&amp;lt;=varnum&amp;lt;=4;
quit;

data want;
set sashelp.cars;
rename &amp;amp;renlist;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jan 2020 22:12:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617618#M181021</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-01-15T22:12:03Z</dc:date>
    </item>
    <item>
      <title>Re: rename many variables using array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617628#M181022</link>
      <description>&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/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;If the variable names all have a pattern (such as x1-x27) and the renamed variables have a pattern (such as paige1-paige27) — which by the way are highly recommended variable names &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&amp;nbsp; — then you can resort to some PROC SQL trickery.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;: I think you mean the RENAME statement in PROC DATASETS, which indeed doesn't accept variable lists -- unlike the RENAME statement of the DATA step where &lt;EM&gt;numbered&lt;/EM&gt; variable lists are allowed:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rename x1-x27 = paige1-paige27;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Even if the ranges differ (but have the same length):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rename x1-x27 = paige20-paige46;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the pattern is &lt;EM&gt;not&lt;/EM&gt; of this special form, &lt;EM&gt;then&lt;/EM&gt; I would start thinking about "PROC SQL trickery".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The problem using the DATA step rename is that the entire data set has to be read and written, and if this is a large data set then this gets time consuming. Using the PROC SQL trickery, combined with PROC DATASETS, only the metadata for a data set is changed, and no records are read or written. So generally, I do not like the DATA step rename, despite its programming simplicity.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2020 23:18:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617628#M181022</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-15T23:18:40Z</dc:date>
    </item>
    <item>
      <title>Re: rename many variables using array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617629#M181023</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;So generally, I do not like the DATA step rename, despite its programming simplicity.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Same with me. Luckily (for my suggested code), the OP's example (using sashelp.cars) was not amenable to the metadata approach anyway.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2020 23:30:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617629#M181023</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-01-15T23:30:12Z</dc:date>
    </item>
    <item>
      <title>Re: rename many variables using array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617638#M181027</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;The problem using the DATA step rename is that the entire data set has to be read and written, and if this is a large data set then this gets time consuming.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;That is normally not a problem. The reason to rename the variables is normally to use the data in some way.&amp;nbsp; There generally no good reason to go back and rename the variable in an existing dataset. Instead fix the process that creates the dataset or the process that uses it.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 01:10:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617638#M181027</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-01-16T01:10:44Z</dc:date>
    </item>
    <item>
      <title>Re: rename many variables using array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617692#M181046</link>
      <description>&lt;P&gt;I have to agree to &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; comment: renaming many variables is hardly ever a good idea. So why do have to change the names of many variables? Creating a view with the new names using the original dataset could be a solution, because you can use the lists mentioned by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt; and still avoid reading the dataset completely.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 06:33:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617692#M181046</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-01-16T06:33:28Z</dc:date>
    </item>
    <item>
      <title>Re: rename many variables using array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617708#M181053</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks all for your replies. The reason why I'm trying to do so is that I have many "character" variables that actually have numeric values. Let's say A, B, and C. Using array, I generated A2, B2, and C2(, which are numeric now) and dropped A, B, and C, (characters). Thereafter, I wanted to rename A2, B2, and C2 to A, B, and C, respectively. Many thanks for having informed me.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 08:30:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617708#M181053</guid>
      <dc:creator>braam</dc:creator>
      <dc:date>2020-01-16T08:30:54Z</dc:date>
    </item>
    <item>
      <title>Re: rename many variables using array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617711#M181056</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279507"&gt;@braam&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can rename variables with Proc Datasets, and you can use arrays to automate the process with a &lt;EM&gt;data _null_&lt;/EM&gt; step that generates statements and executes them with call execute as shown below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You cannot mix variable types in the same array, so you neew two arrays, one for numeric variables and another for character variables. The automatic arrays _character_ and _numeric_ takes all variables, that's why the length statement in the following code is placed after the array statements, otherwise they would be part of the array too.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code handles all variables with names consisting of one or more letters followed by one or more digits. The digits are removed with prxchange, and if the resulting name is different, the variable is renamed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I prefer to avoid changing things "in place", like renaming variables in an existing data set or using the same data set for input and output in a data step, because it destroys input data if anything goes wrong, so I added another step with a few changes to do the same with &lt;EM&gt;data step rename&lt;/EM&gt;. It makes a copy, so it is ineffecient in comparison, byt I consider it "best practice".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	ID = 123;
	A1 = 'A';
	B2 = 'B';
	C2 = 1;
	D2D = 'D2D';
run;

data _null_; set have end=end;
	array char _character_;
	array num _numeric_;
	if _N_ = 1 then call execute('proc datasets library=work nolist; modify have;');

	length name newname $32;
	do i = 1 to dim(char);
		name = vname(char[i]);
		newname = prxchange('s/(\D+)(\d+)$/$1/',-1,trim(name));
		if newname ne name then do;
			cmd = catx(' ','rename',name,'=',newname,';');
			call execute(cmd);
		end;
	end;

	length name newname $32;
	do i = 1 to dim(num);
		name = vname(num[i]);
		newname = prxchange('s/(\D+)(\d+)/$1/',-1,name);
		if newname ne name then do;
			cmd = catx(' ','rename',name,'=',newname,';');
			call execute(cmd);
		end;
	end;
	if end then call execute('run; quit;'); 
run;

data _null_; set have end=end;
	array char _character_;
	array num _numeric_;
	if _N_ = 1 then call execute('data want; set have;');

	length name newname $32;
	do i = 1 to dim(char);
		name = vname(char[i]);
		newname = prxchange('s/(\D+)(\d+)$/$1/',-1,trim(name));
		if newname ne name then do;
			cmd = catx(' ','rename',name,'=',newname,';');
			call execute(cmd);
		end;
	end;

	length name newname $32;
	do i = 1 to dim(num);
		name = vname(num[i]);
		newname = prxchange('s/(\D+)(\d+)/$1/',-1,name);
		if newname ne name then do;
			cmd = catx(' ','rename',name,'=',newname,';');
			call execute(cmd);
		end;
	end;
	if end then call execute('run;'); 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 09:30:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617711#M181056</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2020-01-16T09:30:49Z</dc:date>
    </item>
    <item>
      <title>Re: rename many variables using array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617729#M181067</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;The problem using the DATA step rename is that the entire data set has to be read and written, and if this is a large data set then this gets time consuming.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;That is normally not a problem. The reason to rename the variables is normally to use the data in some way.&amp;nbsp; There generally no good reason to go back and rename the variable in an existing dataset. Instead fix the process that creates the dataset or the process that uses it.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I can't agree that this is not normally a problem. It is normally a problem. In most applications, we have no control over the naming of the variables in the databases we have to use, or the layout/structure of how the data arrives at our doorstep. In many cases, the data has to be re-arranged and/or renamed before we can then go ahead and do SAS analyses on them.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 12:04:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rename-many-variables-using-array/m-p/617729#M181067</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-16T12:04:17Z</dc:date>
    </item>
  </channel>
</rss>

