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

Hello everyone. I have two datasets A and B.  They have some same variables but with different names. I'd like to rename these variable names in A using the variable names in B. For example, I have variable names StaffID, Cartype, Food, Alcohol, ... up to 200 variables in dataset A.  ID, car, food, alcohollevel,....  up to 300 variables in dataset B. I want to rename staffID=ID, cartype=car, alcohol= alcohollevel. Is there a way to rename all the 200 variables in dataset A using array or macros?

 

Thanks,

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

The following should work:

data a;
  input StaffID Cartype Food Alcohol;
  cards;
1 2 3 4
5 6 7 8
;

data B;
  input ID car food alcohollevel;
  cards;
1 2 3 4
5 6 7 8
;

data mapping_chart;
  informat A B $32.;
  input A B;
  cards;
StaffID      ID
Cartype    car
Food        food
Alcohol     alcohollevel
;

proc sql;
  select catt(A,'=',B)
    into :renames separated by ' '
      from mapping_chart
  ;
quit;

proc datasets lib=work nolist;
  modify A;
    rename &renames.;
quit;
run;

Art, CEO, AnalystFinder.com

 

View solution in original post

8 REPLIES 8
Reeza
Super User

Are the data sets ordered in the exact same manner - variables are listed in the exact same order?

How do you know which names you want from which data set?

Alucard_L
Calcite | Level 5
They are not in the same order. Variable names in dataset B are the names I want. I need to change variable names in dataset A. I have the mapping chart. So I know which variable maps to which.
ballardw
Super User

@Alucard_L wrote:
They are not in the same order. Variable names in dataset B are the names I want. I need to change variable names in dataset A. I have the mapping chart. So I know which variable maps to which.

Show your mapping chart.

If you can create a data step with the old/new pairs then it is very likely we can use that with Proc Datasets to change as requested.

Alucard_L
Calcite | Level 5

mapping chart
A               B
StaffID      ID
Cartype    car
Food        food
Alcohol     alcohollevel

Astounding
PROC Star

GIven that you haven't done this before, here is a simple way.  Create your own RENAME statement, stored in a file.  For example:

 

filename myfile 'path to a file of your choice';

 

data _null_;

file myfile noprint;

put 'rename';

do until (done);

   set mapping_chart end=done;

   put a '=' b;

end;

put ';' ;

stop;

run;

 

Inspect the file, so you understand how the program works.  Then apply it in whatever way you are comfortable.  A DATA step has the disadvantage of needing to read the data, but the advantage of being simple:

 

data want;

set have;

%include myfile;

run;

 

Or you can use PROC DATASETS with the MODIFY statement.  Either way should get the job done.

Alucard_L
Calcite | Level 5
Thanks! Your method also works!
art297
Opal | Level 21

The following should work:

data a;
  input StaffID Cartype Food Alcohol;
  cards;
1 2 3 4
5 6 7 8
;

data B;
  input ID car food alcohollevel;
  cards;
1 2 3 4
5 6 7 8
;

data mapping_chart;
  informat A B $32.;
  input A B;
  cards;
StaffID      ID
Cartype    car
Food        food
Alcohol     alcohollevel
;

proc sql;
  select catt(A,'=',B)
    into :renames separated by ' '
      from mapping_chart
  ;
quit;

proc datasets lib=work nolist;
  modify A;
    rename &renames.;
quit;
run;

Art, CEO, AnalystFinder.com

 

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
  • 8 replies
  • 4278 views
  • 0 likes
  • 5 in conversation