I want to rename a variable with value of another variable. This is what I have.
data c;
array a{3} $ a1-a3 ("column1","column2","column3");
array b{3} $ b1-b3; drop a1-a3;
run;
data d;
set c(rename=(b1=a1));
run;
In the dataset d, I want to rename the variable b1 with the value of a1 which is "column1". How is it possible?
Search is your friend, use the search bar to find things:
For example, a simple way, if you know the variable names:
data d; set c; rename b1--b3=column1--column3; run;
Or do you mean the "column" part can be different. In which case a data _null_ step to generate a proc datasets may be appropriate. POst some actual test data which reflect what you have.
Then show us an example of the input, with the dynamic naming values, and an example of the output.
As @RW9 proposed: use a data _null_ step to generate a proc datasets.
You can use either call execute to submit the proc datasets statements or
generate and create a temporary program (text file) to run using %include <fileref>.
Anyway you should know which value will rename which variable, or what is the logic to match
new name with the old name.
Do you have a psecific example - how do you get the new names and how to match it to
variables of a given dataset (c in your post).
This is an example of labels being driven from a data set, but you can easily modify it for the RENAME statement.
https://gist.github.com/statgeek/f18931085f6a0009185c
*Create label data set;
data label_data_set;
length name label $25.;
name="Sex"; label="Gender"; output;
name="height"; label="Height (in)"; output;
name="weight"; label="Weight (lbs)"; output;
run;
*Create sample dataset to apply label;
data class;
set sashelp.class;
run;
*Create macro variable that holds label statement;
proc sql noprint;
select catx("=", name, quote(trim(label)))
into :label_list separated by " "
from label_data_set;
quit;
*Display macro variable in log;
%put &label_list.;
*Apply labels without recreating dataset;
proc datasets library=work;
modify class;
label &label_list.;
run;quit;
*Print the dataset to display new labels;
proc print data=class label noobs;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.