DATA Step, Macro, Functions and more

Batch variable renaming

Reply
Contributor
Posts: 23

Batch variable renaming

Hi,

I have a question. I need to rename all of the variables in a sas dataset. I have another sas dataset that has 2 columns, the variable names, and what the variable names need to be renamed to. Is there a way for me to batch rename the variables in the one dataset based on the information contained in the other dataset, without doing it manually? Also, they are completely different names, it isn't simply adding a prefix or suffix. For example, I need to rename "_76532x2x26" to "HaveSurgery".

Super User
Super User
Posts: 6,499

Re: Batch variable renaming

A simple way (if the list is small enough) is to generate a macro variable with pairs from the RENAME table.

Then you can use the list in a RENAME statement in a data step or PROC DATASETS step or even in RENAME= dataset option.

Example:

proc sql noprint ;

select catx('=',oldname,newname) into :renames separated by ' ' from RENAME ;

quit;

data want ;

  set have ;

  rename &renames;

run;

Trusted Advisor
Posts: 1,612

Re: Batch variable renaming

Executing even faster is probably

proc sql noprint ;

select catx('=',oldname,newname) into :renames separated by ' ' from RENAME ;

quit;

proc datasets nolist library=work;

     modify datasetname;

  rename &renames;

run;

quit;

Contributor
Posts: 23

Re: Batch variable renaming

Hi,

both methods work great! Thanks!

Ask a Question
Discussion stats
  • 3 replies
  • 859 views
  • 2 likes
  • 3 in conversation