Datetime with milliseconds in SAS: highest precision numbers out of nowhere
Can someone explain where the highest precision digits from variables d4 to d7 come from?
SAS program:
data foo;
format d1-d7 datetime30.6;
timestring = "23:59:59.997000000";
time = input(timestring,time18.);
d1 = dhms(0,0,0,time);
d2 = dhms('08DEC1981'd,0,0,time);
d3 = dhms('31DEC2503'd,0,0,time);
d4 = dhms('31DEC2504'd,0,0,time);
d5 = dhms('08DEC2981'd,0,0,time);
d6 = dhms('08DEC4981'd,0,0,time);
d7 = dhms('08DEC9999'd,0,0,time);
run;
proc print;run;
Output:
Obs d1 d2 d3
1 01JAN1960:23:59:59.997000 08DEC1981:23:59:59.997000 31DEC2503:23:59:59.997000
Obs d4 d5 d6
1 31DEC2504:23:59:59.997002 08DEC2981:23:59:59.997002 08DEC4981:23:59:59.996994
Obs d7 timestring time
1 08DEC9999:23:59:59.997009 23:59:59.997000000 86400.00
+2
source to share
1 answer
SAS date and time values have been stored as the number of seconds since Jan 1, 1960, so I'm pretty sure this is caused by base-10 binary floating point representation issues. See this article for an example .
I don't have access to SAS right now, so I can't check, but I for example Dec 31, 2504, 23: 59: 59: 997 will be saved as 17198611199.997. There is no loss of precision here, so an error will be thrown when SAS formats the number back to datetime representation.
Using Python to do the same calculation, I get
>>> from datetime import timedelta
>>> timedelta(seconds=17198611199.997)
datetime.timedelta(199057, 86399, 997002)
That is, the specified number of seconds is converted to 199,057 days, 86399 seconds, and 99702 microseconds.
+2
source to share