My dataset has following variables: 'DATE', 'TIME', 'PRICE' etc.
I want to change the variables names to user specified e.g. dAte, Price, timE etc.
Tried the rename statement with & without modity statement but does not work.
data have;
set have;
modify have;
rename DATE = Date;
rename TIME = timE;
rename PRICE = Price;
run;
Log says dataset updated but variable name still remain the same case.
NOTE: There were 2692 observations read from the data set have.
NOTE: There were 2692 observations read from the data set have.
NOTE: The data set have has been updated. There were 2692 observations rewritten, 0 observations added and 0
observations deleted.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
PS: I only want to change variable name no change required in the format of the observations.
Thanks in advance.
SAS variable names are not case sensitive so trying to use the rename statement to rename a variable to the same name in a data step is not really going to have any effect. SAS will have already stored the name of the variable in the PDV at the point that you first define it. In your example that is when it sees that variable in the dataset referenced by the SET statement.
You could use dataset options to change the name that is read from the incoming dataset.
data have;
set have(rename= (date= Date time= timE price= Price));
run;
Or how it is written on the way out.
data have(rename= (date= Date time= timE price= Price));
set have ;
run;
Or use PROC DATASETS to modify the metadata directly since that is not running a datastep and is instead just telling SAS to write direcly into the metadata for the existing dataset.
proc datasets nolist lib=work;
modify have ;
rename DATE = Date;
rename TIME = timE;
rename PRICE = Price;
run;
Regarding case and variable names, SAS treats all cases as representing the same variable. So it's not really a rename situation as far as SAS is concerned. You do have two choices, however. One is a LABEL statement:
label Date = 'Date';
Some procedures use the label rather than the variable name.
A second choice is to change the case in this way:
data want;
length Date timE Price 8;
set have;
run;
For display purposes, SAS uses the case when the variable was first defined as the case to use when displaying. Of course, this example assumes that the length of 8 is appropriate, identical to the current length for these variables.
@Astounding: Good idea using the LENGTH statement, but wouldn't a RETAIN statement work as effectively, and have the added benefit of not needing pre-knowledge of variable lengths or types?
Not only will RETAIN work, but pretty much all the proposed solutions would work. That's what I get for posting when I don't have access to a computer. The point was made that PROC DATASETS doesn't need to actually read the data, and thus will be fastest. That's a valid and deciding argument.
Hi,
How about this? This is on the same lines as suggested by @Astounding.
data have;
set have (rename = (DATE = Date));
run;
You only need to reprocess the data if you need to change the variable type or the variable length. There is no need to reprocess the data for variable attributes which have no impact on the internal storage of the data.
The "correct" approach to change such attributes is to use PROC DATASETS (or Proc SQL / Alter Table) which only processes the descriptor part of your data.
data have;
date=today();
format date date9.;
output;
run;
proc datasets lib=work nolist;
title 'Before change';
contents data=have;
run;
title;
modify have;
rename date=DaTe;
label date='My Date';
format date yymmdd10.;
run;
title 'After change';
contents data=have;
run;
title;
quit;
SAS variable names are not case sensitive so trying to use the rename statement to rename a variable to the same name in a data step is not really going to have any effect. SAS will have already stored the name of the variable in the PDV at the point that you first define it. In your example that is when it sees that variable in the dataset referenced by the SET statement.
You could use dataset options to change the name that is read from the incoming dataset.
data have;
set have(rename= (date= Date time= timE price= Price));
run;
Or how it is written on the way out.
data have(rename= (date= Date time= timE price= Price));
set have ;
run;
Or use PROC DATASETS to modify the metadata directly since that is not running a datastep and is instead just telling SAS to write direcly into the metadata for the existing dataset.
proc datasets nolist lib=work;
modify have ;
rename DATE = Date;
rename TIME = timE;
rename PRICE = Price;
run;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: