BookmarkSubscribeRSS Feed
cypher85
Calcite | Level 5

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".

3 REPLIES 3
Tom
Super User Tom
Super User

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;

PaigeMiller
Diamond | Level 26

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;

--
Paige Miller
cypher85
Calcite | Level 5

Hi,

both methods work great! Thanks!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 3 replies
  • 3254 views
  • 2 likes
  • 3 in conversation