DATA Step, Macro, Functions and more

date to datetime

Posts: 39

date to datetime

Kudoes to anyone who can figure out why this is not working!! (I can't)

I am converting a sas date to a sas datetime. Simple enough, right?

This works:

data newtreat2;
set newtreatments;
format work_datetime datetime23.3;

BUT.....when I use almost the same code in a merge step, it does not work. In fact, the answer it does give is very interesting, though wrong.

(I am NOT showing the whole merge step, way too complicated)

ADF_USER_DATE_5 =work_datetime;

The value, adf_user_field_5 comes back as a certain number of hours minutes and seconds in January 1st 1960.....if you figure out the total number of seconds that those hours minutes and seconds represent is is EQUAL to the sas date value of workout_date.

Trusted Advisor
Posts: 2,125

Re: date to datetime

Posted in reply to steve_citi
Your code looks OK. Suspect your problem is elsewhere. Put a put statement at the beginning an end of the DO group for diagnostics. You may need to simply the program to the point that you can show it all.
Valued Guide
Posts: 653

Re: date to datetime

Double check to make sure that the length of the datetime variable has not been shortened.
Occasional Contributor
Posts: 11

Re: date to datetime

Posted in reply to steve_citi
can you put the example of corresponding datetime value after using the formula. Message was edited by: sastech
Super Contributor
Super Contributor
Posts: 3,176

Re: date to datetime

Posted in reply to sfsdtegsdsdgdffhgfh
As was suggested, adding a PUT (or PUTLOG _ALL_; with SAS 9) will help but then also if the SAS LENGTH for the DATETIME (numeric variable, time in seconds since 1/1/1960 - minimum length of 7, better to be 8 with STORE_CLOCK values) variable is truncated, you will not see evidence of a problem until a subsequent PROC or DATA step, where the variable is referenced.

Another self-initiated desk-checking technique by the OP would be to perform a PROC CONTENTS on the file immediately after the assignment "step", to verify/inspect the SAS NUMERIC variable "length" attribute for the DATETIME variable.

Also, consider that declaring a SAS FORMAT is not the equivalent of declaring a SAS LENGTH (either as a statement or as a keyword for the ATTRIB statement) -- your SAS variable may still be truncated without warning.

Scott Barry
SBBWorks, Inc.

Encouraged SAS system/programming reading:

SAS Language Reference: Concepts, About SAS Date, Time, and Datetime Values

SAS Language Reference: Concepts, Numeric Precision in SAS Software

Recommended Google advanced search argument, this topic/post:

datetime numeric variable truncated
Ask a Question
Discussion stats
  • 4 replies
  • 5 in conversation