<?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 Numeric field value different from actual value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Numeric-field-value-different-from-actual-value/m-p/973570#M377714</link>
    <description>&lt;P&gt;Hi Friends,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;See the following code&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;EM&gt;data tmp1;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;a = 0.8;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;b = 0.6;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;c = 0.4;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;d = 0.2;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;total = sum(a,b,c,d);&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;run;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;data tmp2;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;set tmp1;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;if total &amp;gt;= 2 then flag = 1;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;else flag = 0;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;diff = sum(-2,total);&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;format diff 32.25;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;run;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;print of tmp2:&lt;/DIV&gt;&lt;DIV&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ItsMeAG_0-1756413618933.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/109394i02DD889A7BFE35D6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ItsMeAG_0-1756413618933.png" alt="ItsMeAG_0-1756413618933.png" /&gt;&lt;/span&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;DIV&gt;Here I am getting &lt;EM&gt;total = 2&lt;/EM&gt; , but when it comes to&lt;EM&gt; if condition&lt;/EM&gt; it's not satisfying '&amp;gt;= 2' and getting flag values as 0.&lt;/DIV&gt;&lt;DIV&gt;I tried to see the difference of&amp;nbsp;&lt;EM&gt;total&lt;/EM&gt; with 2, and getting "&lt;SPAN&gt;-0.00000000000000022204".&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Can someone please help me to understand why the value of&amp;nbsp;&lt;EM&gt;total&amp;nbsp;&lt;/EM&gt; is not actually&amp;nbsp;&lt;STRONG&gt;2&lt;/STRONG&gt; and how to solve it.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Thanks in advance for your time and support!&lt;/SPAN&gt;&lt;/DIV&gt;</description>
    <pubDate>Thu, 28 Aug 2025 20:51:17 GMT</pubDate>
    <dc:creator>ItsMeAG</dc:creator>
    <dc:date>2025-08-28T20:51:17Z</dc:date>
    <item>
      <title>Numeric field value different from actual value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Numeric-field-value-different-from-actual-value/m-p/973570#M377714</link>
      <description>&lt;P&gt;Hi Friends,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;See the following code&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;EM&gt;data tmp1;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;a = 0.8;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;b = 0.6;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;c = 0.4;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;d = 0.2;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;total = sum(a,b,c,d);&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;run;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;data tmp2;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;set tmp1;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;if total &amp;gt;= 2 then flag = 1;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;else flag = 0;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;diff = sum(-2,total);&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;format diff 32.25;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;run;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;print of tmp2:&lt;/DIV&gt;&lt;DIV&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ItsMeAG_0-1756413618933.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/109394i02DD889A7BFE35D6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ItsMeAG_0-1756413618933.png" alt="ItsMeAG_0-1756413618933.png" /&gt;&lt;/span&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;DIV&gt;Here I am getting &lt;EM&gt;total = 2&lt;/EM&gt; , but when it comes to&lt;EM&gt; if condition&lt;/EM&gt; it's not satisfying '&amp;gt;= 2' and getting flag values as 0.&lt;/DIV&gt;&lt;DIV&gt;I tried to see the difference of&amp;nbsp;&lt;EM&gt;total&lt;/EM&gt; with 2, and getting "&lt;SPAN&gt;-0.00000000000000022204".&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Can someone please help me to understand why the value of&amp;nbsp;&lt;EM&gt;total&amp;nbsp;&lt;/EM&gt; is not actually&amp;nbsp;&lt;STRONG&gt;2&lt;/STRONG&gt; and how to solve it.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Thanks in advance for your time and support!&lt;/SPAN&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 28 Aug 2025 20:51:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Numeric-field-value-different-from-actual-value/m-p/973570#M377714</guid>
      <dc:creator>ItsMeAG</dc:creator>
      <dc:date>2025-08-28T20:51:17Z</dc:date>
    </item>
    <item>
      <title>Re: Numeric field value different from actual value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Numeric-field-value-different-from-actual-value/m-p/973582#M377721</link>
      <description>&lt;P&gt;Because SAS (and most computer languages) use BINARY (base 2) floating point numbers.&amp;nbsp; Not the decimal numbers (base 10) that humans like to use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;None of those values can be exactly represented as binary numbers.&amp;nbsp; And when you add them all up the difference is enough that the normal equality test considers the values to be unequal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not just the ROUND() function to reduce the number to a reasonable number of digits.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  input x1-x4;
  total = sum(of x1-x4);
  diff = total-2;
  if total=2 then put TOTAL= 'is exactly 2.';
  if round(total,0.0001)=2 then put TOTAl= 'is close enough to 2.'
    / 'since difference is ' diff:32.25 
  ;
cards;
.8 .6 .4 .2
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Aug 2025 01:46:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Numeric-field-value-different-from-actual-value/m-p/973582#M377721</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-08-29T01:46:03Z</dc:date>
    </item>
    <item>
      <title>Re: Numeric field value different from actual value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Numeric-field-value-different-from-actual-value/m-p/973583#M377722</link>
      <description>&lt;P&gt;In the SAS docu under&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p0dv87zb3bnse6n1mqo360be70qr.htm" target="_self"&gt;Numeric Precision&lt;/A&gt;&amp;nbsp;some more detail for what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;already explained.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Aug 2025 01:54:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Numeric-field-value-different-from-actual-value/m-p/973583#M377722</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2025-08-29T01:54:48Z</dc:date>
    </item>
    <item>
      <title>Re: Numeric field value different from actual value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Numeric-field-value-different-from-actual-value/m-p/973919#M377766</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/446777"&gt;@ItsMeAG&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/446777"&gt;@ItsMeAG&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;DIV&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ItsMeAG_0-1756413618933.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/109394i02DD889A7BFE35D6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ItsMeAG_0-1756413618933.png" alt="ItsMeAG_0-1756413618933.png" /&gt;&lt;/span&gt;&lt;/DIV&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This is one of the more interesting examples of the usual numeric precision issues, as it sheds light on how the calculation is done internally.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159" target="_blank" rel="noopener"&gt;Tom&lt;/A&gt; has pointed out, the internal 64-bit binary floating-point representations of the four numbers 0.8, 0.6, 0.4 and 0.2 are inexact. They contain unavoidable rounding errors because those numbers are repeating fractions in the binary number system (with 4-bit sequences repeating infinitely)&lt;FONT face="arial,helvetica,sans-serif"&gt;:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT face="courier new,courier"&gt;decimal   binary representations&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;value     exact           rounded to 53 signif. bits (as in 8-byte SAS variables)     rounding error&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;-------   -------------------------------------------------------------------------   ---------------&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;0.8       0.&lt;STRONG&gt;1100&lt;/STRONG&gt;1100...   0.&lt;STRONG&gt;1100&lt;/STRONG&gt;11001100110011001100110011001100110011001100110&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;10&lt;/STRONG&gt;&lt;/FONT&gt;     -0.8*2**-54&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;0.6       0.&lt;STRONG&gt;1001&lt;/STRONG&gt;1001...   0.&lt;STRONG&gt;1001&lt;/STRONG&gt;1001100110011001100110011001100110011001100110011      0.4*2**-54&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;0.4       0.&lt;STRONG&gt;0110&lt;/STRONG&gt;0110...   0.&lt;STRONG&gt;0110&lt;/STRONG&gt;011001100110011001100110011001100110011001100110&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;10&lt;/STRONG&gt;&lt;/FONT&gt;    -0.4*2**-54&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;0.2       0.&lt;STRONG&gt;0011&lt;/STRONG&gt;0011...   0.&lt;STRONG&gt;0011&lt;/STRONG&gt;0011001100110011001100110011001100110011001100110&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;10&lt;/STRONG&gt;&lt;/FONT&gt;   -0.2*2**-54&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;Yet, adding the above rounded binary numbers &lt;EM&gt;in one go&lt;/EM&gt; would yield &lt;FONT face="courier new,courier"&gt;2 + 2**-54&lt;/FONT&gt;&amp;nbsp;(as you can see by adding the last few bits manually), which would be rounded to the exact value &lt;FONT face="courier new,courier"&gt;2&lt;/FONT&gt;. So, SAS must do something else. There is strong evidence (from millions of other examples) that the sum is calculated &lt;EM&gt;in three steps&lt;/EM&gt; as &lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;((a+b)+c)+d&lt;/FONT&gt;&lt;/STRONG&gt;, i.e., the result of &lt;FONT face="courier new,courier"&gt;a+b=0.8+0.6&lt;/FONT&gt; is rounded to 53 significant bits before &lt;FONT face="courier new,courier"&gt;0.4&lt;/FONT&gt; is added. Finally, &lt;FONT face="courier new,courier"&gt;0.2&lt;/FONT&gt; is added to the rounded result of the previous calculation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It turns out that the second of the three steps mentioned above introduces an excessive rounding error (&lt;FONT face="courier new,courier"&gt;0.8*2**-52&lt;/FONT&gt;) -- causing the intermediate result &lt;FONT face="courier new,courier"&gt;1.4+0.4 &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;&amp;lt;&lt;/FONT&gt;&lt;/STRONG&gt; 1.8&lt;/FONT&gt;&amp;nbsp;*. In the third step the rounding error is further increased to &lt;FONT face="courier new,courier"&gt;2**-52&lt;/FONT&gt;, the negative value of which is what you obtained in your variable DIFF.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;* see this log:&lt;/P&gt;
&lt;PRE&gt;109   data _null_;
110   if 1.4+0.4 &amp;lt; 1.8 then put 'Surprised?';
111   run;

Surprised?&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/446777"&gt;@ItsMeAG&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;DIV&gt;
&lt;P&gt;(...)&lt;/P&gt;
&lt;/DIV&gt;
&lt;DIV&gt;Here I am getting &lt;EM&gt;total = 2&lt;/EM&gt; , ...&lt;/DIV&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No, you are getting a value that is &lt;EM&gt;displayed as 2&lt;/EM&gt;&amp;nbsp; in the default numeric format. Exact formats such as BINARY64. or HEX16. would show (like variable DIFF) that the value of TOTAL slightly differs from 2.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Sep 2025 17:53:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Numeric-field-value-different-from-actual-value/m-p/973919#M377766</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2025-09-02T17:53:53Z</dc:date>
    </item>
    <item>
      <title>Re: Numeric field value different from actual value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Numeric-field-value-different-from-actual-value/m-p/973923#M377767</link>
      <description>&lt;P&gt;In addition to the good advice you have already &lt;EM&gt;received,&lt;/EM&gt; I would add this. Look at the options in PROC COMPARE so that you can see a variety of methods for comparing computed numbers that work for a variety of situations.&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/v_066/proc/p0bbu58eqgufwzn16zafm1hvzfw2.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/v_066/proc/p0bbu58eqgufwzn16zafm1hvzfw2.htm&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Sep 2025 18:18:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Numeric-field-value-different-from-actual-value/m-p/973923#M377767</guid>
      <dc:creator>WarrenKuhfeld</dc:creator>
      <dc:date>2025-09-02T18:18:27Z</dc:date>
    </item>
    <item>
      <title>Re: Numeric field value different from actual value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Numeric-field-value-different-from-actual-value/m-p/973928#M377768</link>
      <description>&lt;P&gt;It's perfectly logical that&lt;/P&gt;
&lt;PRE&gt;a+b+c+d&lt;/PRE&gt;
&lt;P&gt;is done as&lt;/P&gt;
&lt;PRE&gt;((a+b)+c)+d&lt;/PRE&gt;
&lt;P&gt;The ALU (Arithmetic-Logical Unit) in a processor can only do operations with two numbers/values at a time, and as long as there's no other precedence, the calculation is done left to right.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Sep 2025 19:27:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Numeric-field-value-different-from-actual-value/m-p/973928#M377768</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-09-02T19:27:55Z</dc:date>
    </item>
    <item>
      <title>Re: Numeric field value different from actual value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Numeric-field-value-different-from-actual-value/m-p/973986#M377775</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;wrote:
&lt;P&gt;The ALU (Arithmetic-Logical Unit) in a processor can only do operations with two numbers/values at a time, and as long as there's no other precedence, the calculation is done left to right.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Thanks for contributing this great argument, which is also consistent with the "left to right" order of evaluation in "Group 3" of the table "&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p1n8bsqqd03xppn17pgvjpjlbhhs.htm#p0rdul29zn7i2pn1e7d6fkmc28it" target="_blank" rel="noopener"&gt;Order of Operation in Compound Expressions&lt;/A&gt;."&lt;/P&gt;</description>
      <pubDate>Tue, 02 Sep 2025 21:04:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Numeric-field-value-different-from-actual-value/m-p/973986#M377775</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2025-09-02T21:04:59Z</dc:date>
    </item>
  </channel>
</rss>

