<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Odd decimal behaviour - precission error? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Odd-decimal-behaviour-precission-error/m-p/744758#M233359</link>
    <description>&lt;P&gt;The problem is that the precision of numbers in SAS is not infinite. Furthermore, the numbers are base 2 floating points, not base 10.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A SAS datetime value is internally stored as the number of seconds since 01JAN1960. When you use a datetime value at 31DEC9999, that's an awful lot of seconds - actually, the number of seconds has 12 digits. The maximum precision of SAS numbers is about 15 decimals, so you cannot expect to get datetime values with 6 decimals of&amp;nbsp;precision when the number of seconds has 12 digits.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you add .999 instead of .999999 to your datetime value, you will get "&lt;SPAN&gt;31DEC9999:23:59:59.998993", so the last decimal value is not completely precise. You will get your 6 decimals right up to and after 31DEC2099, but not 8000 years later.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 31 May 2021 11:01:35 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2021-05-31T11:01:35Z</dc:date>
    <item>
      <title>Odd decimal behaviour - precission error?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Odd-decimal-behaviour-precission-error/m-p/744752#M233356</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;I want to create the timestamp "31DEC9999:23:59:59.999999" with this code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _NULL_;
without_decimals = dhms('31DEC9999'D,23,59,59); 
with_decimals    = sum(dhms('31DEC9999'D,23,59,59) , 0.999999  );

put  without_decimals=  datetime25.6;
put  with_decimals   =  datetime25.6;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, in the Log I get this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;without_decimals=31DEC9999:23:59:59.000000&lt;BR /&gt;with_decimals=01JAN****:00:00:00.000000&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.00 seconds&lt;BR /&gt;cpu time 0.00 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why do I not get the expected "31DEC9999:23:59:59.999999" ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 May 2021 09:46:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Odd-decimal-behaviour-precission-error/m-p/744752#M233356</guid>
      <dc:creator>FK1</dc:creator>
      <dc:date>2021-05-31T09:46:15Z</dc:date>
    </item>
    <item>
      <title>Re: Odd decimal behaviour - precission error?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Odd-decimal-behaviour-precission-error/m-p/744758#M233359</link>
      <description>&lt;P&gt;The problem is that the precision of numbers in SAS is not infinite. Furthermore, the numbers are base 2 floating points, not base 10.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A SAS datetime value is internally stored as the number of seconds since 01JAN1960. When you use a datetime value at 31DEC9999, that's an awful lot of seconds - actually, the number of seconds has 12 digits. The maximum precision of SAS numbers is about 15 decimals, so you cannot expect to get datetime values with 6 decimals of&amp;nbsp;precision when the number of seconds has 12 digits.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you add .999 instead of .999999 to your datetime value, you will get "&lt;SPAN&gt;31DEC9999:23:59:59.998993", so the last decimal value is not completely precise. You will get your 6 decimals right up to and after 31DEC2099, but not 8000 years later.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 31 May 2021 11:01:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Odd-decimal-behaviour-precission-error/m-p/744758#M233359</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2021-05-31T11:01:35Z</dc:date>
    </item>
    <item>
      <title>Re: Odd decimal behaviour - precission error?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Odd-decimal-behaviour-precission-error/m-p/744759#M233360</link>
      <description>&lt;P&gt;It looks like more than 3 decimal digits are rounded, plus none exact representation of decimal values in binary system:(&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lrcon/p0ji1unv6thm0dn1gp4t01a1u0g6.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lrcon/p0ji1unv6thm0dn1gp4t01a1u0g6.htm&lt;/A&gt; ) :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _NULL_;
  do _BY_ = 0.999999, 0.99999, 0.9999, 0.999, 0.99, 0.9;
    do without_decimals = dhms('31DEC9999'D,23,59,59) by _BY_ while(i&amp;lt;3); 
      put  _BY_= without_decimals=  datetime25.6 without_decimals=best32.12;
      i + 1;
    end;
    put;
    i=0;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log shows:&lt;/P&gt;
&lt;PRE&gt;_BY_=0.999999 without_decimals=31DEC9999:23:59:59.000000 without_decimals=253717747199
_BY_=0.999999 without_decimals=01JAN****:00:00:00.000000 without_decimals=253717747200
_BY_=0.999999 without_decimals=01JAN****:00:00:01.000000 without_decimals=253717747201

_BY_=0.99999 without_decimals=31DEC9999:23:59:59.000000 without_decimals=253717747199
_BY_=0.99999 without_decimals=01JAN****:00:00:00.000000 without_decimals=253717747200
_BY_=0.99999 without_decimals=01JAN****:00:00:01.000000 without_decimals=253717747201

_BY_=0.9999 without_decimals=31DEC9999:23:59:59.000000 without_decimals=253717747199
_BY_=0.9999 without_decimals=31DEC9999:23:59:59.999908 without_decimals=253717747199.999
_BY_=0.9999 without_decimals=01JAN****:00:00:00.999817 without_decimals=253717747200.999

_BY_=0.999 without_decimals=31DEC9999:23:59:59.000000 without_decimals=253717747199
_BY_=0.999 without_decimals=31DEC9999:23:59:59.998993 without_decimals=253717747199.999
_BY_=0.999 without_decimals=01JAN****:00:00:00.997986 without_decimals=253717747200.998

_BY_=0.99 without_decimals=31DEC9999:23:59:59.000000 without_decimals=253717747199
_BY_=0.99 without_decimals=31DEC9999:23:59:59.989990 without_decimals=253717747199.99
_BY_=0.99 without_decimals=01JAN****:00:00:00.979980 without_decimals=253717747200.98

_BY_=0.9 without_decimals=31DEC9999:23:59:59.000000 without_decimals=253717747199
_BY_=0.9 without_decimals=31DEC9999:23:59:59.899994 without_decimals=253717747199.9
_BY_=0.9 without_decimals=01JAN****:00:00:00.799988 without_decimals=253717747200.8
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 31 May 2021 11:04:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Odd-decimal-behaviour-precission-error/m-p/744759#M233360</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-05-31T11:04:29Z</dc:date>
    </item>
    <item>
      <title>Re: Odd decimal behaviour - precission error?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Odd-decimal-behaviour-precission-error/m-p/744766#M233362</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114220"&gt;@FK1&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In short: The reason is a rounding error. Assuming that your operating system is Windows or Unix, here is what happens in detail:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The integer&lt;/P&gt;
&lt;PRE&gt;without_decimals=253717747199
&lt;/PRE&gt;
&lt;P&gt;has a 64-bit binary floating-point representation (as can be seen with the BINARY64. format) of&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;&lt;STRONG&gt;&lt;FONT color="#008000"&gt;010000100100&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;1101100010010110000011001010111111111000000000000000&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;This corresponds to writing the integer as &lt;STRONG&gt;&lt;FONT face="courier new,courier" color="#3366FF"&gt;&lt;FONT color="#000000"&gt;1.&lt;/FONT&gt;110110001... &lt;FONT color="#008000"&gt;* 2**37&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt; in the binary system.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Similarly, the decimals 0.999999 are internally represented as&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;001111111110&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;1111111111111111110111100111001000010000101111101001&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;which corresponds to &lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;1.&lt;FONT color="#3366FF"&gt;111111111...&lt;/FONT&gt;&lt;/STRONG&gt;* 2**-1&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the addition of these two numbers the smaller number, 0.999999, must use the exponent (37) of the larger number. Therefore, the mantissa must be shifted to the right by 37−(−1)=38 digits (and the "implied bit" appears):&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;&lt;FONT color="#008000"&gt;&lt;STRONG&gt;010000100100&lt;/STRONG&gt;&lt;/FONT&gt;&lt;STRONG&gt;0000000000000000000000000000000000000&lt;FONT color="#3366FF"&gt;111111111111111111101111001&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT color="#99CCFF"&gt;&lt;EM&gt;11001000010000101111101001&lt;/EM&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;The light blue bits in italics are then outside the processor's 80-bit precision, but the relevant rounding error occurs when the sum is rounded to 52 mantissa bits:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;&lt;FONT color="#008000"&gt;&lt;STRONG&gt;010000100100&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;1101100010010110000011001010111111111000000000000000&lt;/STRONG&gt;&lt;/FONT&gt;
&lt;U&gt;&lt;FONT color="#008000"&gt;&lt;STRONG&gt;010000100100&lt;/STRONG&gt;&lt;/FONT&gt;&lt;STRONG&gt;0000000000000000000000000000000000000&lt;FONT color="#3366FF"&gt;11111111111111111&lt;/FONT&gt;&lt;/STRONG&gt;...&lt;/U&gt;
&lt;FONT color="#008000"&gt;&lt;STRONG&gt;010000100100&lt;/STRONG&gt;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;1101100010010110000011001010111111111111111111111111&lt;FONT color="#FF0000"&gt;11...&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;Obviously, the 52nd mantissa bit must be rounded &lt;EM&gt;up&lt;/EM&gt; because of the red 1s that follow. The resulting internal representation&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;&lt;FONT color="#008080"&gt;&lt;STRONG&gt;010000100100&lt;/STRONG&gt;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;1101100010010110000011001011000000000000000000000000&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;is that of the integer&amp;nbsp;253717747200, i.e., the datetime value corresponding to 01 Jan 10000 00:00:00.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, in other words, due to the magnitude of datetime values from years like 9999 the precision of the 64-bit internal representation is insufficient to reflect tiny differences like a millionth of a second. In fact, the least significant bit of such datetime values has a place value of 2**(37−52) = 2**−15 = 3.05...E−5, i.e., about &lt;EM&gt;30&lt;/EM&gt; millionths of a second. For practical applications this should be sufficient.&lt;/P&gt;</description>
      <pubDate>Mon, 31 May 2021 12:01:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Odd-decimal-behaviour-precission-error/m-p/744766#M233362</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-05-31T12:01:52Z</dc:date>
    </item>
    <item>
      <title>Re: Odd decimal behaviour - precission error?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Odd-decimal-behaviour-precission-error/m-p/744768#M233364</link>
      <description>As s_lassen said ,it is precision problem in computer.&lt;BR /&gt;&lt;BR /&gt;data _NULL_;&lt;BR /&gt;without_decimals = dhms('31DEC9999'D,23,59,59); &lt;BR /&gt;with_decimals    = sum(dhms('31DEC9999'D,23,59,59) , 0.999999  );&lt;BR /&gt;&lt;BR /&gt;x=253717747199.999999;&lt;BR /&gt;put  without_decimals=  f32.6;&lt;BR /&gt;put  with_decimals   =  f32.6;&lt;BR /&gt;put  x   =  f32.6;&lt;BR /&gt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;without_decimals=253717747199.000000&lt;BR /&gt;with_decimals=253717747200.000000&lt;BR /&gt;x=253717747200.000000&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 31 May 2021 12:23:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Odd-decimal-behaviour-precission-error/m-p/744768#M233364</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-05-31T12:23:18Z</dc:date>
    </item>
    <item>
      <title>Re: Odd decimal behaviour - precission error?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Odd-decimal-behaviour-precission-error/m-p/744820#M233391</link>
      <description>And the formatted appearance, using the rounded value is attempting to display the year 10,000. Currently none of the SAS date or datetime formats will display a year value greater than 9999 substituting **** for the year portion if not the entire format.&lt;BR /&gt;&lt;BR /&gt;Formats also will round values on top of precision issues.</description>
      <pubDate>Mon, 31 May 2021 19:38:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Odd-decimal-behaviour-precission-error/m-p/744820#M233391</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-31T19:38:56Z</dc:date>
    </item>
    <item>
      <title>Re: Odd decimal behaviour - precission error?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Odd-decimal-behaviour-precission-error/m-p/744903#M233426</link>
      <description>&lt;P&gt;Thank you all for your very helpful explanations. Special Thanks to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;for this indepth explanation!&lt;/P&gt;</description>
      <pubDate>Tue, 01 Jun 2021 10:42:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Odd-decimal-behaviour-precission-error/m-p/744903#M233426</guid>
      <dc:creator>FK1</dc:creator>
      <dc:date>2021-06-01T10:42:05Z</dc:date>
    </item>
  </channel>
</rss>

