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

A good ol' SAS dates problem that is stealing hours away from my life.

 

I have a variable, Datez, which is formatted date9.

 

I use it in a date step:

 

Datez2 = Datez;

 

In the new dataset Datez22 is now BEST12, Length 8, and I just can't seem to get it back into date9. format. 

 

For example a value gets converted from 02DEC2017 to 21155.788194.

 

Help is greatly welcomed. I tried put and format as well as rename and the values  are not correctly getting converted. Ideally I just want to rename Datez to Datez2 and not lose its formatting, date9., and drop Datez from the dataset.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Post an example dataset with all the values that bother you. Just to illustrate what I told you with the data you posted, see this:

data have;
datez = 21155.788194;
format datez date9.;
run;

data want;
set have;
datez2 = datez;
format datez2 date9.;
put datez2=;
format datez; /* deassigns any formats */
put datez=;
run;

Partial log:

32         data want;
33         set have;
34         datez2 = datez;
35         format datez2 date9.;
36         put datez2=;
37         format datez; /* deassigns any formats */
38         put datez=;
39         run;

datez2=02DEC2017
datez=21155.788194

 


@H wrote:

I tried using a format statement and it didn't provide the dates back for all values and the ones it did provide dates for were incorrect. That is why I am posting here. I seem to be missing something 🙂

 


 

View solution in original post

8 REPLIES 8
Kurt_Bremser
Super User

When you store a SAS date value (which is a count of days from 1960-01-01) in a variable, you need to also assign it a date format to display correctly. 

H
Pyrite | Level 9 H
Pyrite | Level 9

Believable. Do you have suggested code.

 

Thx

H
Pyrite | Level 9 H
Pyrite | Level 9

I tried using a format statement and it didn't provide the dates back for all values and the ones it did provide dates for were incorrect. That is why I am posting here. I seem to be missing something 🙂

 

Kurt_Bremser
Super User

Post an example dataset with all the values that bother you. Just to illustrate what I told you with the data you posted, see this:

data have;
datez = 21155.788194;
format datez date9.;
run;

data want;
set have;
datez2 = datez;
format datez2 date9.;
put datez2=;
format datez; /* deassigns any formats */
put datez=;
run;

Partial log:

32         data want;
33         set have;
34         datez2 = datez;
35         format datez2 date9.;
36         put datez2=;
37         format datez; /* deassigns any formats */
38         put datez=;
39         run;

datez2=02DEC2017
datez=21155.788194

 


@H wrote:

I tried using a format statement and it didn't provide the dates back for all values and the ones it did provide dates for were incorrect. That is why I am posting here. I seem to be missing something 🙂

 


 

H
Pyrite | Level 9 H
Pyrite | Level 9

@Kurt_Bremser 

 

Yes, your code snippet works and is a better option (IMHO) since it doesn't require a second data step.  Of note, it turns the original variable's value into a number, but that is moot, since I have that variable in a drop statement with in the statement, when putting it into production.

 

Thank you.

H
Pyrite | Level 9 H
Pyrite | Level 9

My weak hack was:

 

proc datasets nolist lib=work;
	modify MRSA_Pred_second;
	rename datez = datez2;
run;

But this is less than ideal in my mind.

Reeza
Super User

If you have decimal values, I'm suspecting a sas datetime, not a date. 

 

Regardless, apply formats is trivial. 

 

in a data step:

 

data have;
set have;

format date date9.;
run;

or via proc datasets

 

proc datasets lib=work nodetails nolist;
modify have;
format date date9.;
run;quit;

@H wrote:

A good ol' SAS dates problem that is stealing hours away from my life.

 

I have a variable, Datez, which is formatted date9.

 

I use it in a date step:

 

Datez2 = Datez;

 

In the new dataset Datez22 is now BEST12, Length 8, and I just can't seem to get it back into date9. format. 

 

For example a value gets converted from 02DEC2017 to 21155.788194.

 

Help is greatly welcomed. I tried put and format as well as rename and the values  are not correctly getting converted. Ideally I just want to rename Datez to Datez2 and not lose its formatting, date9., and drop Datez from the dataset.


 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 8 replies
  • 2514 views
  • 2 likes
  • 3 in conversation