SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

How to dynamically rename multiple variables

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

How to dynamically rename multiple variables

Hi Experts,

I tried to find by reading lot of stuff for renaming multiple variables dynamically but I couldn't find right solution for it.

In my dataset I have 159 variables named in short form (ex. AR_1) and I want to rename all the variables with there respective longforms.

Good news is I have a KEY dataset which is having short and long form names in vertical format for all of the (i.e. two columns short and long form for all 159 variables (which are observations in this dataset) ).

I tried using transpose and applying a user defined format(created using KEY dataset) but I want to know whether there is any way we can rename variables dynamically using some macro or some proc sql methods.(ex. in case if I dont have KEY dataset).

Thanks.

Harshad.


Accepted Solutions
Solution
‎11-23-2014 07:03 PM
Super User
Posts: 17,837

Re: How to dynamically rename multiple variables

Do your long form variables meet SAS variable naming restrictions, i.e. 32 characters or less, no spaces, and starts with character?

If they don't you'll want to set them as labels instead.

If so I usually do something like the following:

proc sql noprint;

select catx("=", old_name, new_name)

     into :rename_list separated by " "

from key_dataset;

quit;

proc datasets library=work;

modify my_data;

rename &rename_list;

run;

View solution in original post


All Replies
PROC Star
Posts: 1,093

Re: How to dynamically rename multiple variables

Hi, Harshad

Give this a try;

  Tom

data NameLookup;
   length OldName NewName $16;
   input OldName NewName;
   cards;
Name    XX_Name
Sex     YY_Sex
Age     II_Age
Height  AA_Height
Weight  BB_Weight
run;

data _null_;
   set NameLookup end=LastRec;

   if _n_ = 1 then
      call execute('proc sql; create table NewClass as select ');

   if ^LastRec then
      call execute(Oldname || ' as ' || NewName || ',');
   else call execute(Oldname || ' as ' || NewName);

   if LastRec then
      call execute(' from sashelp.class; quit;');
run;

PROC Star
Posts: 7,363

Re: How to dynamically rename multiple variables

Of course, if you just want to modify the original file, you could use 's approach to use proc datasets:

/* create a dataset for testing */

data have;

  set sashelp.class;

run;

data _null_;

  set NameLookup end=LastRec;

  if _n_ = 1 then

    call execute('proc datasets library=work nolist; modify have;');

  call execute('rename '|| Oldname || '=' || NewName ||';');

  if LastRec then call execute('quit;');

run;

Solution
‎11-23-2014 07:03 PM
Super User
Posts: 17,837

Re: How to dynamically rename multiple variables

Do your long form variables meet SAS variable naming restrictions, i.e. 32 characters or less, no spaces, and starts with character?

If they don't you'll want to set them as labels instead.

If so I usually do something like the following:

proc sql noprint;

select catx("=", old_name, new_name)

     into :rename_list separated by " "

from key_dataset;

quit;

proc datasets library=work;

modify my_data;

rename &rename_list;

run;

Valued Guide
Posts: 3,208

Re: How to dynamically rename multiple variables

Why not uis the variable label for the long names,

Keeps as easier coding for the programmer and having the longer name on all type of reports.

---->-- ja karman --<-----
Contributor
Posts: 37

Re: How to dynamically rename multiple variables

JaKarman,
sir yes indeed that is the ideal thing to do, but in my case users are closer to long forms...
Contributor
Posts: 37

Re: How to dynamically rename multiple variables

All answers are correct and exactly what I was seeking.

Thanks everyone.

Cheers Smiley Happy...!!!

Harshad.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 2487 views
  • 12 likes
  • 5 in conversation