DATA Step, Macro, Functions and more

Rename

Reply
Super Contributor
Posts: 647

Rename

Whats wrong with the code below to rename all the varibles to F1 to F16?

%maCRO rename;
data national_temp_v2;
set national_temp_v1;
array fields(*) ndc_code--Dr__DEA;
%do i=1 %to 16;
rename fields(i)=F&i.;
%end;
run;
%mend rename;
%rename;
Respected Advisor
Posts: 3,777

Re: Rename

You can't use arrays and rename in that way. You might do it this way.

[pre]
*Create Test data;
data test;
if 0 then set sashelp.air;
length ndc_code $1;
if 0 then set sashelp.class sashelp.shoes;
length Dr__DEA 8;
call missing(of _all_);
stop;
run;
proc contents order=varnum;
run;

*Rename a range of variables to an enumerated list;
proc transpose data=test(obs=0) out=vars;
var ndc_code--Dr__DEA;
run;

data vars;
set vars;
length new $32;
new = cats('F',_n_);
run;
proc sql noprint;
select catx('=',_name_,new) into :rename separated by ' '
from vars;
quit;
run;
proc datasets;
modify test;
rename &rename;
run;
contents data=test order=varnum;
run;
quit;
[/pre]
Super Contributor
Posts: 647

Re: Rename

Data _null_,
Thanks for the help.
Its neat and nice.
SASPhile.
Contributor
Posts: 74

Re: Rename

1. if there are different data types in these variables array will not work.
2. the do loop in the data step should not use %, i.e. this is not a macro level loop.

what messages do you get from the log?
Contributor
Posts: 74

Re: Rename

or you may simply create new variables F1 -- F16 in the loop, as
array f (*) f1 - f16;

...

do i=1 to dim(f);
f(i)=field(i);
end;

then delete the original fields.
Super Contributor
Super Contributor
Posts: 3,174

Re: Rename

You are attempting to mix MACRO language with DATA step language - the two are not compatible.

Also you cannot use an ARRAY to reference variables for a rename process because RENAME occurs at the step-level and the ARRAY reference is not.

If you have no need to pass your data, suggest using PROC DATASETS and MODIFY.

You will find useful technical reference material at the SAS support http://suppor.sas.com/ website using the Google advanced argument listed below:

rename all variables site:sas.com


Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 5 replies
  • 233 views
  • 0 likes
  • 4 in conversation