tags:

views:

377

answers:

1

Can someone explain where the highest precision numbers in variables d4 to d7 came from?

The 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;

The 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
+1  A: 

SAS datetime values are stored as the number of seconds since January 1, 1960, so I'm pretty sure this is caused by the problems of representing base-10 numbers with binary floating point numbers. See for example this article.

I don't have access to SAS right now so I can't check, but I for example December 31, 2504, 23:59:59:997 would be stored as 17198611199.997. There is no loss of accuracy here, so the error would be caused when SAS formats the number back to the 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 given number of seconds converts to 199057 days, 86399 seconds and 99702 microseconds.

Ville Koskinen
Thanks. The Python example was also helpful.
Michiel Borkent