DATA Step, Macro, Functions and more

Change variable name

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

Change variable name

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.  

 


Accepted Solutions
Solution
‎09-25-2017 08:33 AM
Super User
Super User
Posts: 7,860

Re: Change variable name

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


All Replies
Super User
Posts: 6,543

Re: Change variable name

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.

Trusted Advisor
Posts: 1,294

Re: Change variable name

Posted in reply to Astounding

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

Super User
Posts: 6,543

Re: Change variable name

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

Trusted Advisor
Posts: 1,270

Re: Change variable name

Hi,

 

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

 

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

run;

Respected Advisor
Posts: 4,567

Re: Change variable name

[ Edited ]

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

 

Solution
‎09-25-2017 08:33 AM
Super User
Super User
Posts: 7,860

Re: Change variable name

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;
Occasional Contributor
Posts: 17

Re: Change variable name

Thank you, Patrick
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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