<?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: Error renaming variables in a do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522792#M141988</link>
    <description>&lt;P&gt;Note that the RENAME statement (or RENAME= dataset option) wants variable names. Using ARRAY references makes&amp;nbsp;no&amp;nbsp; sense because the RENAME operation is something that is calculated when the data step is complied. ARRAYs are a useful tool for dynamically referencing variables when the data step is running but cannot be used with RENAME statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looks like want to change the TYPE of the variables.&amp;nbsp; Unlike simply changing the display FORMAT attached to a variable to change its type you&amp;nbsp; need to create new variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a method that might make it easier. First transpose the character variables into a tall structure. Then convert the new single character variable into a number.&amp;nbsp; You could then transpose it back.&amp;nbsp; But perhaps you want to leave it in the tall structure instead.How are you planning to use 1,400 variables effectively anyway?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=step1 ;
  by id ;
  var _character_;
run;

data step2;
  set step1 ;
  col2 = input(col1,32.);
run;

proc transpose data=step2 out=want;
  by id;
  var col2 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 20 Dec 2018 04:36:28 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-12-20T04:36:28Z</dc:date>
    <item>
      <title>Error renaming variables in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522715#M141959</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Hi all,&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;I have a data set with several variables (more than 1000) that are numbers but saved as character variables. I would like to change their format to numeric. For example, the run below is for variables that have single digit numbers. I am getting the following error when I use arrays (but not when I do the run without array). Please let me know what I am doing wrong. Thanks.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Data&lt;/STRONG&gt; lib.tmp2;&lt;/P&gt;&lt;P&gt;&amp;nbsp; Set tmp1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ARRAY B (&lt;STRONG&gt;30&lt;/STRONG&gt;) B1-B30;&lt;/P&gt;&lt;P&gt;ARRAY C (&lt;STRONG&gt;30&lt;/STRONG&gt;) C1-C30;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DO i=&lt;STRONG&gt;1&lt;/STRONG&gt; TO &lt;STRONG&gt;30&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp; C(i)=input(B(i), &lt;STRONG&gt;1.&lt;/STRONG&gt;); drop B(i); rename C(i)=B(i);&lt;/P&gt;&lt;P&gt;END;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR 22-322: Syntax error, expecting one of the following: a name, -, :, ;, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.&lt;/P&gt;&lt;P&gt;ERROR 22-322: Syntax error, expecting one of the following: -, :, =.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Dec 2018 21:33:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522715#M141959</guid>
      <dc:creator>Jason2020</dc:creator>
      <dc:date>2018-12-19T21:33:55Z</dc:date>
    </item>
    <item>
      <title>Re: Error renaming variables in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522720#M141961</link>
      <description>&lt;P&gt;try this :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input b1 $ b2 $ b3 $;
cards;
1 4 7
2 5 8
3 6 9
run; 

data want;
	set have;
	array a(3) b1-b3;
	array c(3) c1-c3;

	do i=1 to dim(a);
		c[i]=input(a[i],$2.);
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Dec 2018 21:54:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522720#M141961</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2018-12-19T21:54:16Z</dc:date>
    </item>
    <item>
      <title>Re: Error renaming variables in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522721#M141962</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can make use of Dictionary.Columns and reconstruct the rename, drop and conversion.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
b1='1';b2='2';b3='3';
run;


proc sql;
select  cats(name,'_'), /* Drop variables */
		cats(name,'=',name,'_'), /* Rename variables */
		cats(name,'=input(',name,'_',',1.);') /* Convert to Numeric */
	into:Drop separated by ' ', :rename separated by ' ',:Convert_num  separated by ' '
from dictionary.columns
where libname='WORK' and memname='HAVE'
;
quit;

%put "&amp;amp;rename";
%put "&amp;amp;Convert_num";
%put "&amp;amp;Drop";

data want(drop=&amp;amp;drop);
set have(rename=(&amp;amp;rename));
&amp;amp;convert_num;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Dec 2018 22:03:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522721#M141962</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-12-19T22:03:18Z</dc:date>
    </item>
    <item>
      <title>Re: Error renaming variables in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522760#M141979</link>
      <description>&lt;P&gt;Try this....&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
  input B1 $3.;
CARDS;
1
2
3
4
5
;
run;

DATA HAVE;
 SET HAVE;
 B2=B1;
 B3=B1;
run;

data MYVARSLIST;
  infile datalines dlm="|";
  input 
  var: $32.
  renvar: $32.;
datalines;
B1|C1
B2|C2
B3|C3
;
run;

proc sql noprint;
  select catt(count(*)) into :varn from MYVARSLIST;
  select var, renvar into :var1-:var&amp;amp;varn, :renvar1-:renvar&amp;amp;varn
  from MYVARSLIST;
quit;


%put &amp;amp;varn.;

%macro test;
data want;
 set have;

  %do i=1 %to &amp;amp;varn.;
    &amp;amp;&amp;amp;renvar&amp;amp;i=input(&amp;amp;&amp;amp;var&amp;amp;i.,3.);
    drop &amp;amp;&amp;amp;var&amp;amp;i.;
	rename &amp;amp;&amp;amp;renvar&amp;amp;i=&amp;amp;&amp;amp;var&amp;amp;i.;
  %end;

run;
%mend;
%test;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Dec 2018 00:42:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522760#M141979</guid>
      <dc:creator>ShiroAmada</dc:creator>
      <dc:date>2018-12-20T00:42:28Z</dc:date>
    </item>
    <item>
      <title>Re: Error renaming variables in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522768#M141986</link>
      <description>This will note solve my issue. I have more than 1,400 variables and I want to keep the variable names. I therefore used the DROP and RENAME statements so that I can keep the same names of the variables. However, using these statements inside the do loop generates an error.&lt;BR /&gt;Using your example, here is what I want the program to do:&lt;BR /&gt;data have;&lt;BR /&gt;input b1 $ b2 $ b3 $;&lt;BR /&gt;cards;&lt;BR /&gt;1 4 7&lt;BR /&gt;2 5 8&lt;BR /&gt;3 6 9&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;set have;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;array b(3) b1-b3;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;array c(3) c1-c3;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;do i=1 to dim(b);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;c[i]=input(b[i],$2.);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;drop b(i);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rename C(i)=b(i);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;end;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 20 Dec 2018 02:09:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522768#M141986</guid>
      <dc:creator>Jason2020</dc:creator>
      <dc:date>2018-12-20T02:09:47Z</dc:date>
    </item>
    <item>
      <title>Re: Error renaming variables in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522769#M141987</link>
      <description>&lt;P&gt;Let me know if this fulfills your requirement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input b1 $ b2 $ b3 $;
cards;
1 4 7
2 5 8
3 6 9
run;

data want;
    set have;
    array b(3) b1-b3;
    array c(3) c1-c3;

    do i=1 to dim(b);
        c[i]=input(b[i],$2.);
       
     end;
     drop b: i;
run;


proc contents data=want out=temp(keep=name) nodetails noprint;
run;

proc sql noprint;
select cats(name,'=',tranwrd(name,'c','b')) into :ren_str separated by ' '
from temp;
quit;

%put &amp;amp;=ren_str;

proc datasets library=work nodetails noprint;
	modify want ;
	rename &amp;amp;ren_str;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Dec 2018 02:36:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522769#M141987</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2018-12-20T02:36:45Z</dc:date>
    </item>
    <item>
      <title>Re: Error renaming variables in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522792#M141988</link>
      <description>&lt;P&gt;Note that the RENAME statement (or RENAME= dataset option) wants variable names. Using ARRAY references makes&amp;nbsp;no&amp;nbsp; sense because the RENAME operation is something that is calculated when the data step is complied. ARRAYs are a useful tool for dynamically referencing variables when the data step is running but cannot be used with RENAME statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looks like want to change the TYPE of the variables.&amp;nbsp; Unlike simply changing the display FORMAT attached to a variable to change its type you&amp;nbsp; need to create new variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a method that might make it easier. First transpose the character variables into a tall structure. Then convert the new single character variable into a number.&amp;nbsp; You could then transpose it back.&amp;nbsp; But perhaps you want to leave it in the tall structure instead.How are you planning to use 1,400 variables effectively anyway?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=step1 ;
  by id ;
  var _character_;
run;

data step2;
  set step1 ;
  col2 = input(col1,32.);
run;

proc transpose data=step2 out=want;
  by id;
  var col2 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Dec 2018 04:36:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522792#M141988</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-12-20T04:36:28Z</dc:date>
    </item>
    <item>
      <title>Re: Error renaming variables in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522849#M142002</link>
      <description>&lt;P&gt;The DROP and RENAME statements are not executable, they are declarations in the data step. Thus it makes no difference if you put them inside or outside a loop, and they cannot be used with arrays.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would suggest creating a small macro to rename your variables, e.g.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro rename(prefix1,prefix2,from,to);
  %local i;
rename %do i=&amp;amp;from %to &amp;amp;to; &amp;amp;prefix1.&amp;amp;i=&amp;amp;prefix2.&amp;amp;i%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Then your SAS program can be rewritten as&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data lib.tmp2;
  Set tmp1;
  ARRAY B (30) B1-B30;
  ARRAY C (30) C1-C30;
  DO i=1 TO 30;
    C(i)=input(B(i), 1.); 
  END;
  drop B1-B30;
  %rename(C,B,1,30);
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Dec 2018 13:20:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522849#M142002</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-12-20T13:20:27Z</dc:date>
    </item>
    <item>
      <title>Re: Error renaming variables in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522888#M142007</link>
      <description>&lt;P&gt;Unfortunately, none of the proposed solutions worked for my data (possibly because I did not give enough details). However, all the suggestions were helpful. After some thinking, I solved the problem as follows. Thanks to all for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; want1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array b (&lt;STRONG&gt;30&lt;/STRONG&gt;)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l1m l1md l1mm l2m l2md l2mm lc lcd lcm li lid lim lp lpd lpm&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; u1m u1md u1mm u2m u2md u2mm uc ucd ucm ui uid uim up upd upm;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array c (&lt;STRONG&gt;30&lt;/STRONG&gt;) c1-c30;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do i=&lt;STRONG&gt;1&lt;/STRONG&gt; to dim(b);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; c(i)=input(b[i], $2.);&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drop b: i;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; want2;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set want1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array b (&lt;STRONG&gt;30&lt;/STRONG&gt;)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l1m l1md l1mm l2m l2md l2mm lc lcd lcm li lid lim lp lpd lpm&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; u1m u1md u1mm u2m u2md u2mm uc ucd ucm ui uid uim up upd upm;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array c (&lt;STRONG&gt;30&lt;/STRONG&gt;) c1-c30;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do i=&lt;STRONG&gt;1&lt;/STRONG&gt; to dim(b);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; b(i)=c[i];&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drop c: i;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Dec 2018 15:47:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-renaming-variables-in-a-do-loop/m-p/522888#M142007</guid>
      <dc:creator>Jason2020</dc:creator>
      <dc:date>2018-12-20T15:47:07Z</dc:date>
    </item>
  </channel>
</rss>

