Hi,
1. What's the best way to rename a variable that's in the SAME dataset? Is it PROC DATASETS?
2. For the RENAME statement, the examples I found online, they all used a different output dataset name for the NEW variable name. Is there any harm if I use the same dataset names as both the output and the input?
Example from online:
data qtr1 qtr2 ytd(drop=qtrtot);
set ytdsales;
if qtr=1 then output qtr1;
else if qtr=2 then output qtr2;
else output ytd;
rename total=qtrtot;
run;
Example if I want to rename the variable in the SAME dataset:
data ytdsales;
set ytdsales;
rename total=qtrtot;
run;
I want to use the same dataset because that's the dataset where I want the variable name to be renamed.
If you have to do something else that requires you to make a NEW dataset (like your first example) then using the RENAME statement (or properly placed RENAME= dataset option) is the best. The code is clearer since it does not use a proc that most users never have learned about.
But if making a new version of the dataset would take a long time, or too much disk space, or lose some other attributes of the dataset (the member label comes to mind for your second example) then using PROC DATASETS makes sense. It will run quickly and only make the changes you wanted.
proc datasets nolist lib=WORK;
modify ytdsales;
rename total=qtrtot;
run;
quit;
If you have to do something else that requires you to make a NEW dataset (like your first example) then using the RENAME statement (or properly placed RENAME= dataset option) is the best. The code is clearer since it does not use a proc that most users never have learned about.
But if making a new version of the dataset would take a long time, or too much disk space, or lose some other attributes of the dataset (the member label comes to mind for your second example) then using PROC DATASETS makes sense. It will run quickly and only make the changes you wanted.
proc datasets nolist lib=WORK;
modify ytdsales;
rename total=qtrtot;
run;
quit;
Yes. You can do a simple data step to rename a variable.
data have;
set have;
rename old=new;
run;
And for most simple analysis programs that is fine and appropriate.
But it will
@cosmid wrote:
Wow! Thank you! That's good to know. I wasn't aware of the things you listed. I'll use PROC DATASETS for renaming variables. Thanks again!
Like I said for most simple analysis programs it does not matter.
But the best thing is to modify the previous (or next) step so that renaming is not needed. Then you don't need to add an extra step at all.
So give the variables the names you want when you make the dataset the first time.
data step2;
set step1;
... other stuff ...
rename old=new;
run;
proc means data=step2;
var new;
run;
Or change the name at the point where you first read the dataset.
proc means data=step2(rename=(old=new));
var new;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.