BookmarkSubscribeRSS Feed
smohando
Calcite | Level 5

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?

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

smohando
Calcite | Level 5
I want to rename with the value of the variable which is set dynamically. I cannot hardcode that in my code.
mkeintz
PROC Star

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

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Shmuel
Garnet | Level 18

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

 

 

 

 

 

 

 

Reeza
Super User

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-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2537 views
  • 0 likes
  • 5 in conversation