In SAS, use a loop to rename indexed columns?

I have a dataset with variables like col1, col2, col3, ..., col15. I want to rename them to new1, new2, new3, ..., new 15. I can write 15 times the same rename col1 = new1; in SAS, but how can I achieve this usage cycle? Thank.

+5
source share
2 answers

First, it is unclear whether you are talking about an instruction renamein proc datasetsor at a data step. If you do not need to do anything with the data, you should definitely use the proc datasets to do this, because otherwise (in the data step) you do not need to read / write each record in the data set, just change the variable names.

,

rename col1-col15=new1-new15;

, proc. . , , / 15 . , :

data _null_;
  length myVar $ 1000;
  *myVar='';
  do i=1 to 15;
    myVar=catx(' ',myVar,' ',cats('col',i,'=','new',i));
  end;
  call symput('rename',myVar);
run;

%put &rename;

proc datasets library=mylibrary;
  modify mydataset;
  rename &rename;
run;
+9

, :

 DATA out;
 SET  in;
 ARRAY oldnames (15) col1-col15;
 ARRAY newnames (15) new1-new15;
 DO i = 1 TO 15;
      newnames(i) = oldnames(i) ;
 END;
 RUN;

, :

 DATA out;
 SET  in;
 ARRAY oldnames (4) abc def ghi jkl ;
 ARRAY newnames (4) mno pqr stu vwx ;
 DO i = 1 TO 4;
      newnames(i) = oldnames(i) ;
 END;
 RUN;
+3

All Articles