Dates in Datetime Fields

Thursday, 22 September, 2011

⨳ 1 minute read ⨳ 103 words ⨳ date valuessas

Got date values intermingled with datetime values? Here’s a brute force way to convert the date values into date time values.

Code

data _null_;
    x1='01JAN1960:05:09:56'dt;
    x2='12SEP2010:00:00:00'dt;
    array x(*) x:;
    do i = 1 to 2;
        if timepart(x[i]) then x[i]=dhms(x[i], 0, 0, 0);
        x[i] = datepart(x[i]);
    end;
    put x1 date.
    /   x2 date.;
run;

Log

30NOV10
12SEP10

Note that if the date value is a product of 86400 (the number of seconds in a day), then timepart(date) will resolve to 0. Fortunately, the next time this will happen (after 01JAN1960) will be 21JUL2196.


Dates in Datetime Fields - September 22, 2011 - Richard Koopmann