<?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: comparing two values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/comparing-two-values/m-p/761126#M240793</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The &lt;A href="https://documentation.sas.com/doc/en/pgmmvacdc/9.4/lefunctionsref/p18pa7u3zufu9nn1ivv67uknw3i7.htm" target="_self"&gt;FUZZ()&lt;/A&gt; function can help here, detecting when two values are within roundoff error, and then treating them as equal.&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, this can be a handy function in many such cases. However, it is less flexible than Kurt Bremser's approaches in that it uses the fixed constant 1E-12 as the maximum tolerated absolute difference. The rounding error in sums of moderately large numbers (or sufficiently many numbers) can exceed this threshold.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example (where the difference is about &lt;FONT face="courier new,courier"&gt;-1.8E-12&lt;/FONT&gt;)&lt;FONT face="helvetica"&gt;:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;1797  data _null_;
1798  if fuzz(4101.4+4101.7 - 8203.1) then put 'Surprised?';
1799  run;

Surprised?&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 12 Aug 2021 12:36:19 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2021-08-12T12:36:19Z</dc:date>
    <item>
      <title>comparing two values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-two-values/m-p/761113#M240787</link>
      <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Although this was supposed to be very simple code and to function without any issues, I have the outcome I am not able to solve or see where the problem, either with my proc sql code or SAS data download:&lt;/P&gt;&lt;P&gt;So it's kind of simple, I need to compare if the sum of var2 values is equal to the value of of var3 (the reason for that is that users are entering var3 manually by summing up the values of var2, so the mistakes are present).&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code is:&lt;/P&gt;&lt;P&gt;create&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;table&amp;nbsp;newTable&amp;nbsp;as&lt;/P&gt;&lt;P&gt;select&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;var1,&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;sum(var2)&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;as&amp;nbsp;newVar2, var3&lt;/P&gt;&lt;P&gt;case when sum(var2)=var3 then "OK"&amp;nbsp;&lt;/P&gt;&lt;P&gt;else "CHECK!" end as Var4&lt;/P&gt;&lt;P&gt;from&amp;nbsp;table1&lt;/P&gt;&lt;P&gt;group&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;by&amp;nbsp;var1;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem is that there are records where sum(var2)=var3 but the var4 has the output "CHECK"&lt;/P&gt;&lt;P&gt;for example&amp;nbsp; sum(var2) = 1.9 and var 3 =1.9 but the var4 will be CHECK (out of over 600 records, this happens with 20 records), for all other records works well.&lt;/P&gt;&lt;P&gt;Maybe I should mention that I've checked the format of this sum(var2) and var3: sum(var2) is BEST12 numeric, var3 is 5.1 numeric. Even if I bring them to the same format (BEST12), it would be still the same (though that some format discrepancy may cause that...)&lt;/P&gt;&lt;P&gt;So no matter what I do, I would get something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;var1&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;newVar2&lt;/TD&gt;&lt;TD&gt;var3&lt;/TD&gt;&lt;TD&gt;var4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;rec1&lt;/TD&gt;&lt;TD&gt;3.1&lt;/TD&gt;&lt;TD&gt;3.1&lt;/TD&gt;&lt;TD&gt;CHECK!&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;rec2&lt;/TD&gt;&lt;TD&gt;18.2&lt;/TD&gt;&lt;TD&gt;18.2&lt;/TD&gt;&lt;TD&gt;OK&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;rec3&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;OK&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;rec4&lt;/TD&gt;&lt;TD&gt;12.4&lt;/TD&gt;&lt;TD&gt;12.4&lt;/TD&gt;&lt;TD&gt;CHECK!&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;rec5&lt;/TD&gt;&lt;TD&gt;17.8&lt;/TD&gt;&lt;TD&gt;17.8&lt;/TD&gt;&lt;TD&gt;OK&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am not able to see the problem in terms of my code, data format... is there anything&amp;nbsp;elsewhere&amp;nbsp;I should look for he?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very&amp;nbsp;much&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Aug 2021 10:36:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-two-values/m-p/761113#M240787</guid>
      <dc:creator>Batta</dc:creator>
      <dc:date>2021-08-12T10:36:18Z</dc:date>
    </item>
    <item>
      <title>Re: comparing two values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-two-values/m-p/761115#M240788</link>
      <description>&lt;P&gt;You run into the usual problem with numeric precision. SAS stores numbers in 8-byte real, which means that there are 52 bits available for the mantissa (the rest is sign and exponent), which translates to a maximum of 15 decimal digits that can be reliably stored.&lt;/P&gt;
&lt;P&gt;And most &lt;EM&gt;decimal fractions&lt;/EM&gt; turn into periodic numbers when translated to binary notation.&lt;/P&gt;
&lt;P&gt;All this leads to imprecisions during calculations, where you get differences in the 1E-14 range or so.&lt;/P&gt;
&lt;P&gt;Use the ROUND function or check for a maximum difference:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;case when round(sum(var2),.0001)=round(var3,.0001)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;case when abs(sum(var2) - var3) lt 1E-5 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do a google search for "sas numeric precision", and you will find lots of stuff to read about this.&lt;/P&gt;
&lt;P&gt;Also see&amp;nbsp;&lt;A href="https://en.wikipedia.org/wiki/Double-precision_floating-point_format" target="_blank" rel="noopener"&gt;https://en.wikipedia.org/wiki/Double-precision_floating-point_format&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Aug 2021 11:05:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-two-values/m-p/761115#M240788</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-08-12T11:05:05Z</dc:date>
    </item>
    <item>
      <title>Re: comparing two values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-two-values/m-p/761117#M240789</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/361111"&gt;@Batta&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example (using SAS 9.4 under Windows) illustrating Kurt Bremser's points (see &lt;A href="https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=p0ji1unv6thm0dn1gp4t01a1u0g6.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank" rel="noopener"&gt;Numerical Accuracy in SAS Software&lt;/A&gt; for details)&lt;FONT face="helvetica"&gt;:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;1505  data _null_;
1506  if 1.4+1.7 ne 3.1 then put 'Surprised?';
1507  run;

Surprised?
NOTE: DATA statement used (Total process time):&lt;/PRE&gt;
&lt;P&gt;Default formats and most other numeric formats don't reveal the tiny difference (of about 4.44E-16) between the internal values (i.e., binary representations) of 1.4+1.7 and 3.1. Special formats like &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/leforinforref/n0ueabv26pr2fwn19uxk2f4bf10y.htm" target="_blank" rel="noopener"&gt;HEX16.&lt;/A&gt; do so:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
var3=3.1;
var2=1.4; output;
var2=1.7; output;
run;

proc sql;
select sum(var2) as s format=hex16., var3 format=hex16.,
       calculated s-var3 as d, round(calculated s, 1e-9) as r format=hex16.
from test
group by var3;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;               s              var3         d                 r
--------------------------------------------------------------
4008CCCCCCCCCCCC  4008CCCCCCCCCCCD  -444E-18  4008CCCCCCCCCCCD&lt;/PRE&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>Thu, 12 Aug 2021 11:46:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-two-values/m-p/761117#M240789</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-08-12T11:46:37Z</dc:date>
    </item>
    <item>
      <title>Re: comparing two values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-two-values/m-p/761118#M240790</link>
      <description>&lt;P&gt;The &lt;A href="https://documentation.sas.com/doc/en/pgmmvacdc/9.4/lefunctionsref/p18pa7u3zufu9nn1ivv67uknw3i7.htm" target="_self"&gt;FUZZ()&lt;/A&gt; function can help here, detecting when two values are within roundoff error, and then treating them as equal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
    if 1.4+1.7 ne 3.1 then put 'Surprised?';
run;

data _null_;
     if fuzz(1.4+1.7 - 3.1) then put 'Surprised?';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Aug 2021 11:53:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-two-values/m-p/761118#M240790</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-08-12T11:53:31Z</dc:date>
    </item>
    <item>
      <title>Re: comparing two values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-two-values/m-p/761126#M240793</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The &lt;A href="https://documentation.sas.com/doc/en/pgmmvacdc/9.4/lefunctionsref/p18pa7u3zufu9nn1ivv67uknw3i7.htm" target="_self"&gt;FUZZ()&lt;/A&gt; function can help here, detecting when two values are within roundoff error, and then treating them as equal.&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, this can be a handy function in many such cases. However, it is less flexible than Kurt Bremser's approaches in that it uses the fixed constant 1E-12 as the maximum tolerated absolute difference. The rounding error in sums of moderately large numbers (or sufficiently many numbers) can exceed this threshold.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example (where the difference is about &lt;FONT face="courier new,courier"&gt;-1.8E-12&lt;/FONT&gt;)&lt;FONT face="helvetica"&gt;:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;1797  data _null_;
1798  if fuzz(4101.4+4101.7 - 8203.1) then put 'Surprised?';
1799  run;

Surprised?&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Aug 2021 12:36:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-two-values/m-p/761126#M240793</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-08-12T12:36:19Z</dc:date>
    </item>
    <item>
      <title>Re: comparing two values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-two-values/m-p/761393#M240930</link>
      <description>&lt;P&gt;Thank you very much for this explanation.&lt;/P&gt;&lt;P&gt;Very useful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sincerely,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Batta&lt;/P&gt;</description>
      <pubDate>Fri, 13 Aug 2021 11:55:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-two-values/m-p/761393#M240930</guid>
      <dc:creator>Batta</dc:creator>
      <dc:date>2021-08-13T11:55:41Z</dc:date>
    </item>
    <item>
      <title>Re: comparing two values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-two-values/m-p/761394#M240931</link>
      <description>&lt;P&gt;Thank you very much for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sincerely,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Batta&lt;/P&gt;</description>
      <pubDate>Fri, 13 Aug 2021 12:03:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-two-values/m-p/761394#M240931</guid>
      <dc:creator>Batta</dc:creator>
      <dc:date>2021-08-13T12:03:19Z</dc:date>
    </item>
  </channel>
</rss>

