BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8
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;
5 REPLIES 5
data_null__
Jade | Level 19
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]
SASPhile
Quartz | Level 8
Data _null_,
Thanks for the help.
Its neat and nice.
SASPhile.
abdullala
Calcite | Level 5
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?
abdullala
Calcite | Level 5
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1726 views
  • 0 likes
  • 4 in conversation