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

I had a problem when I renamed a date variable. The format was changed and the new variable could not be used as a date variable. Does this happen to all rename variables or only to data variables?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You just have the order of operations confused.

 

The RENAME statement tells SAS to change the name when it writes the OUTPUT.  During the current data step the name is not yet changed, so use its actual name.

 

I usually like to place RENAME/DROP/KEEP statements at the END of the data step.  It helps keep it clearer about when they are really applied.

data rename;
  set test;
  year=year(date);
  rename date=renamed_date;
run;

Make sure to read the LOG. 

Your code will generate warnings and important notes.

6    data rename;
7      set test;
8      rename date=renamed_date;
9      year=year(renamed_date);
10   run;

NOTE: Variable renamed_date is uninitialized.
WARNING: Variable renamed_date already exists on file WORK.RENAME.
NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 9:8
NOTE: There were 1 observations read from the data set WORK.TEST.
NOTE: The data set WORK.RENAME has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds

 

 

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

When I try this, I don't see the problem you described. Here is my code. Please show us your code.

 

data test;
    date='01JAN2024'd;
    format date date9.;
run;

data rename;
    set test(rename=(date=renamed_date));
run;
--
Paige Miller
trevand
Obsidian | Level 7

it seems that it's only problematic when I use rename outside of the set statement:

 

data test;
    date='01JAN2024'd;
    format date date9.;
run;

data rename;
 set test
rename date=renamed_date;
year=year(renamed_date);
run;

the year function gives me an empty column.

Tom
Super User Tom
Super User

You just have the order of operations confused.

 

The RENAME statement tells SAS to change the name when it writes the OUTPUT.  During the current data step the name is not yet changed, so use its actual name.

 

I usually like to place RENAME/DROP/KEEP statements at the END of the data step.  It helps keep it clearer about when they are really applied.

data rename;
  set test;
  year=year(date);
  rename date=renamed_date;
run;

Make sure to read the LOG. 

Your code will generate warnings and important notes.

6    data rename;
7      set test;
8      rename date=renamed_date;
9      year=year(renamed_date);
10   run;

NOTE: Variable renamed_date is uninitialized.
WARNING: Variable renamed_date already exists on file WORK.RENAME.
NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 9:8
NOTE: There were 1 observations read from the data set WORK.TEST.
NOTE: The data set WORK.RENAME has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds

 

 

ballardw
Super User

@trevand wrote:

it seems that it's only problematic when I use rename outside of the set statement:

 

data test;
    date='01JAN2024'd;
    format date date9.;
run;

data rename;
 set test
rename date=renamed_date;
year=year(renamed_date);
run;

the year function gives me an empty column.


When you abuse syntax expect unexpected results. Your data step should read either (semicolon after test)

data rename;
 set test ;
rename date=renamed_date;
year=year(renamed_date);
run;

Or with proper () for data set options

data rename;
 set test (rename=( date=renamed_date));
year=year(renamed_date);
run;

Your data step as shown does not actually run:

842  data rename;
843   set test
844  rename date=renamed_date;
                -
                22
                76
ERROR 22-322: Syntax error, expecting one of the following: CUROBS, END, INDSNAME, KEY, KEYRESET,
              KEYS, NOBS, POINT.

ERROR 76-322: Syntax error, statement will be ignored.

845  year=year(renamed_date);
846  run;

If you have a "rename" data set then it is the result of different code. So perhaps not surprising that a format definition did not survive.

ballardw
Super User

If you can provide an example data set in the form of working data step code to create 10 or so observations and the code the renamed the date variable in that data set that has this behavior we could make an attempt to describe what happened.

 

I.e can you duplicate the behavior consistently. I am going out on a limb and guessing that likely you can't OR something more than a simple rename is involved.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 5 replies
  • 609 views
  • 0 likes
  • 4 in conversation