<?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: Decimals calculation problem in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Decimals-calculation-problem/m-p/963075#M375263</link>
    <description>Great! Thanks for your detailed explanation!</description>
    <pubDate>Tue, 01 Apr 2025 03:17:16 GMT</pubDate>
    <dc:creator>Maplefin</dc:creator>
    <dc:date>2025-04-01T03:17:16Z</dc:date>
    <item>
      <title>Decimals calculation problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Decimals-calculation-problem/m-p/962984#M375234</link>
      <description>&lt;P&gt;Hi, I recently find an issue that SAS may not get the correct answer for decimals calculation. My code is as below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data x;
a=5.7;
b=4.75;
c=(a-b)/b;
if c=0.2 then flag="Y";
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;As you can see, I want get the proportion of (a-b) to b. It should be 0.2, and it seems that SAS give the right answer. But to my surprise, the value of variabel Flag is equal to "Y", which means c is not strictly equal to 0.2. In fact, c is over 0.2. Can someone explain it to me? I really want to know the&amp;nbsp;mechanism behind it.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Mar 2025 06:09:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Decimals-calculation-problem/m-p/962984#M375234</guid>
      <dc:creator>Maplefin</dc:creator>
      <dc:date>2025-03-31T06:09:37Z</dc:date>
    </item>
    <item>
      <title>Re: Decimals calculation problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Decimals-calculation-problem/m-p/962985#M375235</link>
      <description>&lt;P&gt;Just have a look at &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p0dv87zb3bnse6n1mqo360be70qr.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p0dv87zb3bnse6n1mqo360be70qr.htm&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;- the sections stating with "Floating-Point" should explain the problem. &lt;/P&gt;</description>
      <pubDate>Mon, 31 Mar 2025 06:17:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Decimals-calculation-problem/m-p/962985#M375235</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2025-03-31T06:17:23Z</dc:date>
    </item>
    <item>
      <title>Re: Decimals calculation problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Decimals-calculation-problem/m-p/962987#M375236</link>
      <description>&lt;P&gt;That is because computer is unable to store 0.2 exactly , same to other languages Java, Python, R.........&lt;/P&gt;
&lt;P&gt;You need to round it before comparing it .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data x;
a=5.7;
b=4.75;
c=(a-b)/b;
/*if round(c,1e-12)=0.2 then flag="Y";*/
if &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;round(c,0.000000000001)&lt;/STRONG&gt;&lt;/FONT&gt;=0.2 then flag="Y";
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Mar 2025 06:29:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Decimals-calculation-problem/m-p/962987#M375236</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-03-31T06:29:15Z</dc:date>
    </item>
    <item>
      <title>Re: Decimals calculation problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Decimals-calculation-problem/m-p/962988#M375237</link>
      <description>&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/n0o6e1t72yad42n10y9wf8pgmat8.htm#p0lghy68tpnudtn1je83d9vt1yf2" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/n0o6e1t72yad42n10y9wf8pgmat8.htm#p0lghy68tpnudtn1je83d9vt1yf2&lt;/A&gt;</description>
      <pubDate>Mon, 31 Mar 2025 06:34:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Decimals-calculation-problem/m-p/962988#M375237</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-03-31T06:34:46Z</dc:date>
    </item>
    <item>
      <title>Re: Decimals calculation problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Decimals-calculation-problem/m-p/962989#M375238</link>
      <description>Thanks, to round the decimals may be the good solution.</description>
      <pubDate>Mon, 31 Mar 2025 07:01:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Decimals-calculation-problem/m-p/962989#M375238</guid>
      <dc:creator>Maplefin</dc:creator>
      <dc:date>2025-03-31T07:01:27Z</dc:date>
    </item>
    <item>
      <title>Re: Decimals calculation problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Decimals-calculation-problem/m-p/962998#M375243</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/329461"&gt;@Maplefin&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Glad to see you found the practical&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0tj6cmga7p8qln1ejh6ebevm0c9.htm" target="_blank" rel="noopener"&gt;ROUND&lt;/A&gt; solution to your issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So this is &lt;A href="https://communities.sas.com/t5/SAS-Programming/Rounding-to-multiples-of-a-value-lead-to-wrnog-values-a-small/m-p/961034#" target="_blank" rel="noopener"&gt;yet another&lt;/A&gt; example where numeric representation error and&amp;nbsp;&lt;SPAN&gt;arithmetic calculations with numbers of limited precision entail tiny differences between the computer's results (not SAS-specific) and the results expected from using the decimal system. It's a particularly nice example, so let's take a look at the internal workings:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;First, note that your starting values &lt;FONT face="courier new,courier"&gt;a&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;b&lt;/FONT&gt; look quite different in the binary system:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;a=101.1&lt;FONT color="#00DD00"&gt;&lt;STRONG&gt;0110&lt;/STRONG&gt;&lt;/FONT&gt;01100110...
b=100.11&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;While b=4.75=19/4 has an exact, finite binary representation using only five binary digits, a=5.7 is a repeating fraction in the binary system: The four-bit pattern "0110" highlighted in green is repeated forever.&amp;nbsp; As a consequence, an unavoidable numeric representation error (here: 1.776E-16) occurs when 5.7 is rounded to fit into the 64-bit space (actually: 52 mantissa bits + 1 implied bit) available internally:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;101.1&lt;FONT color="#00DD00"&gt;&lt;STRONG&gt;0110&lt;/STRONG&gt;&lt;/FONT&gt;01100110011001100110011001100110011001100110&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;The last, 53rd bit (highlighted in red) has been rounded up and the decimal equivalent of this number is this:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;5.70000000000000017763568394002504646778106689453125&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thanks to the simplicity of &lt;FONT face="courier new,courier"&gt;b&lt;/FONT&gt; in the binary system, the subtraction &lt;FONT face="courier new,courier"&gt;a-b&lt;/FONT&gt; is easy and results in the binary fraction&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;0.1111001100110011001100110011001100110011001100110&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/FONT&gt;000&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;The rounded bit from above (red) is now the 50th bit, hence more significant than it was before. Indeed, the former numeric representation error has turned into a noticeable rounding error, as the number is slightly larger than the "expected" 0.95:&lt;/P&gt;
&lt;PRE&gt;122   data _null_;
123   if 5.7 - 4.75 &amp;gt; 0.95 then put 'Surprised?';
124   run;

Surprised?
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now you will not be surprised that the "red" bit also causes trouble in the division &lt;FONT face="courier new,courier"&gt;(a-b)/b&lt;/FONT&gt;. This calculation is a bit tedious (if done by hand), yet relatively simple due to the shortness of the binary representation of &lt;FONT face="courier new,courier"&gt;b&lt;/FONT&gt;. Its result is (after rounding up the 53rd bit)&lt;FONT face="courier new,courier"&gt;:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;0.001100110011001100110011001100110011001100110011001101&lt;FONT color="#993366"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;which differs (in the least significant bit) from the correctly rounded repeating fraction being the binary equivalent of the decimal 0.2&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;0.001100110011001100110011001100110011001100110011001101&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here comes the punchline: Ironically, even putting the "exact" numeric literal 0.95 (rather than the deviating &lt;FONT face="courier new,courier"&gt;a-b&lt;/FONT&gt;) into the division does &lt;EM&gt;not&lt;/EM&gt; yield the perfect result. Instead, the numeric representation error of 0.95 (making this number internally a bit smaller than it should be: &lt;FONT face="courier new,courier"&gt;0.9499999999999999555910790149937383830547332763671875&lt;/FONT&gt;) causes a&amp;nbsp;&lt;EM&gt;too small&lt;/EM&gt; result:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;0.00110011001100110011001100110011001100110011001100110&lt;STRONG&gt;&lt;FONT color="#993366"&gt;01&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;so that, again, an exact equality check in a DATA step would fail:&lt;/P&gt;
&lt;PRE&gt;134   data _null_;
135   if 0.95/4.75 &amp;lt; 0.2  then put 'Surprised again?';
136   run;

Surprised again?&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Mar 2025 12:15:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Decimals-calculation-problem/m-p/962998#M375243</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2025-03-31T12:15:43Z</dc:date>
    </item>
    <item>
      <title>Re: Decimals calculation problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Decimals-calculation-problem/m-p/963030#M375245</link>
      <description>&lt;P&gt;Great explanation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Everything would be much easier if we humans had evolved with only four fingers.&lt;/P&gt;</description>
      <pubDate>Mon, 31 Mar 2025 16:34:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Decimals-calculation-problem/m-p/963030#M375245</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-03-31T16:34:28Z</dc:date>
    </item>
    <item>
      <title>Re: Decimals calculation problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Decimals-calculation-problem/m-p/963075#M375263</link>
      <description>Great! Thanks for your detailed explanation!</description>
      <pubDate>Tue, 01 Apr 2025 03:17:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Decimals-calculation-problem/m-p/963075#M375263</guid>
      <dc:creator>Maplefin</dc:creator>
      <dc:date>2025-04-01T03:17:16Z</dc:date>
    </item>
  </channel>
</rss>

