BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dhir
Obsidian | Level 7

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.  

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

7 REPLIES 7
Astounding
PROC Star

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.

mkeintz
PROC Star

@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?

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

--------------------------
Astounding
PROC Star

@mkeintz,

 

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.

stat_sas
Ammonite | Level 13

Hi,

 

How about this? This is on the same lines as suggested by @Astounding.

 

data have;
set have (rename = (DATE = Date));

run;

Patrick
Opal | Level 21

@dhir

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;

 

Tom
Super User Tom
Super User

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;
dhir
Obsidian | Level 7
Thank you, Patrick

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 13425 views
  • 5 likes
  • 6 in conversation