BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

6 REPLIES 6
TomKari
Onyx | Level 15

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;

art297
Opal | Level 21

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;

Reeza
Super User

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;

jakarman
Barite | Level 11

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 --<-----
HarshadMadhamshettiwar
Obsidian | Level 7
JaKarman,
sir yes indeed that is the ideal thing to do, but in my case users are closer to long forms...
HarshadMadhamshettiwar
Obsidian | Level 7

All answers are correct and exactly what I was seeking.

Thanks everyone.

Cheers Smiley Happy...!!!

Harshad.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 13567 views
  • 15 likes
  • 5 in conversation