<?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 variable names with macro and loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-names-with-macro-and-loop/m-p/640203#M190620</link>
    <description>&lt;P&gt;Macro generates the source code that is submitted.&lt;/P&gt;
&lt;P&gt;A running DATA step can't interact with macro and modify it's own running source code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can rely on knowing your variables and use `numbered range lists` to rename the variables&lt;/P&gt;
&lt;PRE&gt;data have;
  array x x1-x10;
run;

data want;
  set have;
  rename x1-x10=y1-y10;  * numbered range list;
run;&lt;/PRE&gt;
&lt;P&gt;or use a step to access to variable names (part of a data set's metadata) and write the rename statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 15 Apr 2020 20:01:36 GMT</pubDate>
    <dc:creator>RichardDeVen</dc:creator>
    <dc:date>2020-04-15T20:01:36Z</dc:date>
    <item>
      <title>Renaming variable names with macro and loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-names-with-macro-and-loop/m-p/640159#M190596</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I would like to replace a fixed string of charaters in the variable names of a dataset. I created the follwing example. I don't want give the number of columns, but automatically loop over all columns. However, I always get this error message:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #ff0000; font-family: Consolas, Courier, 'Courier New'; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"&gt;ERROR: Required operator not found in expression: dim(cols)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Many thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;/* create test data */
data test (drop= i);
	array a{*} A1-A3;
	do i=1 to dim(a);
		a{i}=i;
	end;
run;

/* with specifying the number of coluns */
%macro doit(ncols=3);
data testB;
	set test;
	%do i = 1 %to &amp;amp;ncols;
		rename A&amp;amp;i.=B&amp;amp;i.;
	%end;
run;
%mend doit;
%doit;

/* with loop automatically over all columns */
%macro doit();
data testC;
	set test;
	array cols(*) _all_;
	%do i = 1 %to dim(cols); /* here is the problem*/
		rename A&amp;amp;i.=B&amp;amp;i.;
	%end;
run;
%mend doit;
%doit;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Apr 2020 22:41:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-names-with-macro-and-loop/m-p/640159#M190596</guid>
      <dc:creator>SamuelKnapp</dc:creator>
      <dc:date>2020-04-15T22:41:06Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variable names with macro and loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-names-with-macro-and-loop/m-p/640178#M190606</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code doesn't work because the macro language is a preprocessor.&amp;nbsp; Macro code executes before any DATA step code executes (or even compiles).&amp;nbsp; The macro language does not know about data sets, data set variables, and arrays.&amp;nbsp; So in this line:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i = 1 %to dim(cols);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;dim(cols) is just a series of 9 characters to the macro language, thus the error.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Can you describe a bit more of your real problem?&amp;nbsp; Do you really have only one list of variable to rename, with numeric suffixes (A1-A3) or do you have other variables as well?&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;One approach to this is to build a macro variable that has the list of variables to be renamed.&amp;nbsp; And you can use the macro language to loop over that list.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;For you current example, you don't actually need a macro, as you can do:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data testC;
  set test (rename=(A1-A3=B1-B3));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Apr 2020 18:32:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-names-with-macro-and-loop/m-p/640178#M190606</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2020-04-15T18:32:26Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variable names with macro and loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-names-with-macro-and-loop/m-p/640199#M190617</link>
      <description>&lt;P&gt;Thanks for your quick answer.&lt;BR /&gt;The actual problem is to rename Cov1-Cov90 (output lsmeans) into Col1-Col90 (for use as coefficent matrix in proc mixed). As I need to run it several times, I don't want to specify the number of columns by hand, and it differs between the datasets.&lt;BR /&gt;I found this other post, where it was possible in a macro, but also by specifying the number of columns by hand (like the first approach):&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-variables-using-a-loop-data-step/td-p/337185" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-variables-using-a-loop-data-step/td-p/337185&lt;/A&gt;&lt;BR /&gt;If there is any replace method, all I need is to replace "Cov" by "Col".&lt;/P&gt;&lt;P&gt;And yes, there are also other variables in the dataset, but my idea was to split these variables off, change their names and merge it again...&lt;BR /&gt;I'm a bit surprised that this seems to be so difficult, as in R it's one line and done...&lt;BR /&gt;Thanks again&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 19:36:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-names-with-macro-and-loop/m-p/640199#M190617</guid>
      <dc:creator>SamuelKnapp</dc:creator>
      <dc:date>2020-04-15T19:36:33Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variable names with macro and loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-names-with-macro-and-loop/m-p/640203#M190620</link>
      <description>&lt;P&gt;Macro generates the source code that is submitted.&lt;/P&gt;
&lt;P&gt;A running DATA step can't interact with macro and modify it's own running source code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can rely on knowing your variables and use `numbered range lists` to rename the variables&lt;/P&gt;
&lt;PRE&gt;data have;
  array x x1-x10;
run;

data want;
  set have;
  rename x1-x10=y1-y10;  * numbered range list;
run;&lt;/PRE&gt;
&lt;P&gt;or use a step to access to variable names (part of a data set's metadata) and write the rename statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 20:01:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-names-with-macro-and-loop/m-p/640203#M190620</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-04-15T20:01:36Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming variable names with macro and loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-names-with-macro-and-loop/m-p/640216#M190629</link>
      <description>&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;It's trivial to add a step that reads the variable names from metadata, and generates a list of rename pairs, e.g.:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  array a{*} x y Cov1-Cov90;
run;

proc sql noprint;
  select cats(name,"=",transtrn(name,'Cov','Col'))
    into :renamelist separated by " "
    from dictionary.columns
    where libname='WORK' and memname='HAVE' and name like 'Cov%'
  ;
quit ;

%Put &amp;amp;renamelist ;

data want ;
  set have ;
  rename &amp;amp;renamelist ;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Apr 2020 20:47:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Renaming-variable-names-with-macro-and-loop/m-p/640216#M190629</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2020-04-15T20:47:15Z</dc:date>
    </item>
  </channel>
</rss>

