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,
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
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 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.
mapping chart
A B
StaffID ID
Cartype car
Food food
Alcohol alcohollevel
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.
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
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.
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.