DATA Step, Macro, Functions and more

How to rename a variable in a dataset with value of another variable

Reply
New Contributor
Posts: 4

How to rename a variable in a dataset with value of another variable

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?

Super User
Super User
Posts: 9,840

Re: How to rename a variable in a dataset with value of another variable

Search is your friend, use the search bar to find things:

https://communities.sas.com/t5/forums/searchpage/tab/message?advanced=false&allow_punctuation=false&...

 

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.

New Contributor
Posts: 4

Re: How to rename a variable in a dataset with value of another variable

I want to rename with the value of the variable which is set dynamically. I cannot hardcode that in my code.
Trusted Advisor
Posts: 1,394

Re: How to rename a variable in a dataset with value of another variable

Then show us an example of the input, with the dynamic naming values, and an example of the output.

Trusted Advisor
Posts: 1,848

Re: How to rename a variable in a dataset with value of another variable

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).

 

 

 

 

 

 

 

Super User
Posts: 24,012

Re: How to rename a variable in a dataset with value of another variable

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;
Ask a Question
Discussion stats
  • 5 replies
  • 162 views
  • 0 likes
  • 5 in conversation