<?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 PROC SQL Greater Than (&amp;gt;) in WHERE clause returns wrong rows, i.e., it includes equal values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Greater-Than-gt-in-WHERE-clause-returns-wrong-rows-i-e/m-p/766991#M243090</link>
    <description>&lt;P&gt;Neither of the following codes filters the rows properly. The discount column was initially formatted as a percentage, I changed it to Comma32.28 to see all decimals but there is nothing but zeros in the decimals. So, if I write WHERE Discount &amp;gt; 0.3 it shouldn't return columns where the discount value is 0.3, am I wrong?&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ikoyuncu_0-1631235552849.png" style="width: 1005px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63492i0A03D8A8F5F2D74B/image-dimensions/1005x294?v=v2" width="1005" height="294" role="button" title="ikoyuncu_0-1631235552849.png" alt="ikoyuncu_0-1631235552849.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;What is wrong with this program? Why it keeps including equality when I specifically say &amp;gt; or GT. How is 0.300000000000000000000 greater than 0.3? Am I missing something?&lt;/P&gt;&lt;P&gt;Has anyone else encountered a similar problem? Is it because of lack of ORDER BY or something? Couldn't think of anything else...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 10 Sep 2021 01:00:24 GMT</pubDate>
    <dc:creator>ikoyuncu</dc:creator>
    <dc:date>2021-09-10T01:00:24Z</dc:date>
    <item>
      <title>PROC SQL Greater Than (&gt;) in WHERE clause returns wrong rows, i.e., it includes equal values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Greater-Than-gt-in-WHERE-clause-returns-wrong-rows-i-e/m-p/766991#M243090</link>
      <description>&lt;P&gt;Neither of the following codes filters the rows properly. The discount column was initially formatted as a percentage, I changed it to Comma32.28 to see all decimals but there is nothing but zeros in the decimals. So, if I write WHERE Discount &amp;gt; 0.3 it shouldn't return columns where the discount value is 0.3, am I wrong?&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ikoyuncu_0-1631235552849.png" style="width: 1005px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63492i0A03D8A8F5F2D74B/image-dimensions/1005x294?v=v2" width="1005" height="294" role="button" title="ikoyuncu_0-1631235552849.png" alt="ikoyuncu_0-1631235552849.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;What is wrong with this program? Why it keeps including equality when I specifically say &amp;gt; or GT. How is 0.300000000000000000000 greater than 0.3? Am I missing something?&lt;/P&gt;&lt;P&gt;Has anyone else encountered a similar problem? Is it because of lack of ORDER BY or something? Couldn't think of anything else...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 01:00:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Greater-Than-gt-in-WHERE-clause-returns-wrong-rows-i-e/m-p/766991#M243090</guid>
      <dc:creator>ikoyuncu</dc:creator>
      <dc:date>2021-09-10T01:00:24Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Greater Than (&gt;) in WHERE clause returns wrong rows, i.e., it includes equal values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Greater-Than-gt-in-WHERE-clause-returns-wrong-rows-i-e/m-p/766996#M243094</link>
      <description>&lt;P&gt;SAS&amp;nbsp; (and pretty much all computers) store numbers using BINARY representation. There is no way to represent 3/10ths exactly using binary numbers.&amp;nbsp; The value that your query is saying is larger then 0.3 is larger than 0.3 by such a small amount that it does not display when printed as a decimal fraction.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Round the value and then test.&lt;/P&gt;
&lt;P&gt;How many digits of precision do those numbers really have?&amp;nbsp; Let's say it is to the thousandths place.&amp;nbsp; &amp;nbsp;So round to the thousandths place or the ten-thousands place and then compare.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;WHERE round(Discount,0.0001) &amp;gt; 0.3&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Sep 2021 04:01:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Greater-Than-gt-in-WHERE-clause-returns-wrong-rows-i-e/m-p/766996#M243094</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-10T04:01:55Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Greater Than (&gt;) in WHERE clause returns wrong rows, i.e., it includes equal values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Greater-Than-gt-in-WHERE-clause-returns-wrong-rows-i-e/m-p/767000#M243098</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;answered.&lt;/P&gt;
&lt;P&gt;0.3 cannot be represented exactly as a power of 2, just like 1/3 cannot be represented in the decimal base.&amp;nbsp;Look up &lt;EM&gt;numerical precision&lt;/EM&gt;&amp;nbsp;if you want to know more.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&amp;gt; How is 0.300000000000000000000&amp;nbsp;greater than 0.3?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You can add as many zeros as you want in the code, it makes no difference as the computer cannot use them when storing the decimal number in 8 bytes.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 06:54:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Greater-Than-gt-in-WHERE-clause-returns-wrong-rows-i-e/m-p/767000#M243098</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-09-10T06:54:46Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Greater Than (&gt;) in WHERE clause returns wrong rows, i.e., it includes equal values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Greater-Than-gt-in-WHERE-clause-returns-wrong-rows-i-e/m-p/767031#M243116</link>
      <description>&lt;P&gt;For more about the nuances of floating point math and the apparent inequality of certain numbers that "look" the same, see &lt;A href="https://blogs.sas.com/content/sasdummy/2012/03/01/precision-in-sas-numbers/" target="_self"&gt;Numerical precision in SAS&lt;/A&gt;. But as others said, this isn't unique to SAS -- it's an IEEE standard for how floating point numbers are represented in systems/programming languages in general.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 11:38:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Greater-Than-gt-in-WHERE-clause-returns-wrong-rows-i-e/m-p/767031#M243116</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2021-09-10T11:38:56Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Greater Than (&gt;) in WHERE clause returns wrong rows, i.e., it includes equal values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Greater-Than-gt-in-WHERE-clause-returns-wrong-rows-i-e/m-p/767033#M243117</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/393082"&gt;@ikoyuncu&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's true that SAS cannot store 0.3 exactly in its &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;64-bit binary floating-point representation&lt;/A&gt; -- indeed, in the binary system 0.3 is a &lt;EM&gt;periodic&lt;/EM&gt; fraction: &lt;FONT face="courier new,courier"&gt;0.0&lt;EM&gt;1001&lt;/EM&gt;10011001...&lt;/FONT&gt;, so the internal representation is &lt;EM&gt;rounded&lt;/EM&gt; and under Windows it really corresponds to &lt;FONT face="courier new,courier"&gt;0.299999999999999988897769753748434595763683319091796875&lt;/FONT&gt; (&lt;FONT face="courier new,courier"&gt;= 0.3 - 2**-54 / 5 = 0.3 - 1.1102...E-17&lt;/FONT&gt;) -- but I wouldn't say that the numeric representation error (&lt;FONT face="courier new,courier"&gt;-1.11...E-17&lt;/FONT&gt;) of this particular number is the main reason for the &lt;EM&gt;rounding&lt;/EM&gt; error in your variable &lt;FONT face="courier new,courier"&gt;Discount&lt;/FONT&gt;&amp;nbsp;(whose value in the cases in question must differ from 0.3 by &lt;EM&gt;more&lt;/EM&gt; than the unavoidable&amp;nbsp;numeric representation error).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Virtually &lt;EM&gt;all&lt;/EM&gt; numbers, including (what should be) integers, can be "contaminated" with similar rounding errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Examples (log from SAS 9.4M5 under Windows):&lt;/P&gt;
&lt;PRE&gt;409  data _null_;
410  if 1.4-0.4 &amp;lt; &lt;STRONG&gt;1&lt;/STRONG&gt; &amp;lt; 2.2-1.2
411   &amp;amp; 1.005*1000 &amp;lt; &lt;STRONG&gt;1005&lt;/STRONG&gt; &amp;lt; 10.05*100
412   &amp;amp; 0.6/0.2 &amp;lt; &lt;STRONG&gt;3&lt;/STRONG&gt; &amp;lt; 9.9/3.3
413  then put 'Surprised?';
414  run;

Surprised?
NOTE: DATA statement used (Total process time):&lt;/PRE&gt;
&lt;P&gt;&lt;EM&gt;Calculations&lt;/EM&gt; with numbers (like 0.3 and many others) affected by numeric representation error can easily produce rounding errors making the results different from what they should be mathematically, as shown in the examples above and below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the question is rather how those&amp;nbsp;&lt;FONT face="courier new,courier"&gt;Discount&lt;/FONT&gt; values only looking like &lt;FONT face="courier new,courier"&gt;0.3&lt;/FONT&gt;&amp;nbsp;or &lt;FONT face="courier new,courier"&gt;0.3000...&lt;/FONT&gt; (in standard formats) were created. I suspect that they were calculated from amounts of money with their typical (up to) two decimal places. (Note that, in a sense, &lt;EM&gt;96 percent&lt;/EM&gt; of the numbers with up to 2 decimal places are affected by&amp;nbsp;numeric representation error.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Examples:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input x y;
Discount=x/y;
length c $12;
h=put(Discount, hex16.);
diff=Discount-0.3;
     if Discount&amp;lt;0.3 then c='Discount&amp;lt;0.3';
else if Discount=0.3 then c='Discount=0.3';
else if Discount&amp;gt;0.3 then c='Discount&amp;gt;0.3';
cards;
   2.7     9
 240.03  800.1
2100.99 7003.3
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs          x         y    Discount         c                 h                diff

 1        2.70       9.0       0.3      Discount&amp;gt;0.3    3FD333333333333&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;4&lt;/FONT&gt;&lt;/STRONG&gt;    5.55112E-17
 2      240.03     800.1       0.3      Discount=0.3    3FD333333333333&lt;FONT color="#008000"&gt;&lt;STRONG&gt;3&lt;/STRONG&gt;&lt;/FONT&gt;              0
 3     2100.99    7003.3       0.3      Discount&amp;lt;0.3    3FD333333333333&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;    -5.5511E-17&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Mathematically, all three &lt;FONT face="courier new,courier"&gt;Discount&lt;/FONT&gt; values should be 0.3, but only the result in obs. 2 happens to match the internal representation of 0.3. The other two results contain rounding errors in the least significant bit (place value: 2**-54 = 5.551...E-17) which make them different from 0.3 in the IF conditions. This is obvious in formats such as HEX16. revealing the internal representation, but often not in common formats like the standard &lt;EM&gt;w.d&lt;/EM&gt; format or the COMMA&lt;EM&gt;w&lt;/EM&gt;.&lt;EM&gt;d&lt;/EM&gt; format &lt;EM&gt;because they are rounding&lt;/EM&gt;. For instance,&amp;nbsp;format 32.30 reveals a rounding error in obs. 3, but not in obs. 1, unless you look at the difference &lt;FONT face="courier new,courier"&gt;diff&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A definition of &lt;FONT face="courier new,courier"&gt;Discount&lt;/FONT&gt;&amp;nbsp;using the ROUND function such as&lt;/P&gt;
&lt;PRE&gt;Discount=&lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;round(&lt;/FONT&gt;&lt;/STRONG&gt;x/y&lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;, 1e-9)&lt;/FONT&gt;&lt;/STRONG&gt;;&lt;/PRE&gt;
&lt;P&gt;would have avoided the issues in the examples and in similar cases. Alternatively, the ROUND function can be applied in the comparison (as &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159" target="_blank" rel="noopener"&gt;Tom&lt;/A&gt; has suggested) or in a preliminary "data cleaning" step, e.g., if the &lt;FONT face="courier new,courier"&gt;Discount&lt;/FONT&gt; values were imported from another database.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 11:50:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Greater-Than-gt-in-WHERE-clause-returns-wrong-rows-i-e/m-p/767033#M243117</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-09-10T11:50:32Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Greater Than (&gt;) in WHERE clause returns wrong rows, i.e., it includes equal values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Greater-Than-gt-in-WHERE-clause-returns-wrong-rows-i-e/m-p/767079#M243139</link>
      <description>Thanks, I actually didn't add all those zeros to use. I only formatted the column to see if there are any decimals &amp;gt;0 that I am missing. As others suggested, a preliminary 'data cleaning' solved my problem.</description>
      <pubDate>Fri, 10 Sep 2021 17:21:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Greater-Than-gt-in-WHERE-clause-returns-wrong-rows-i-e/m-p/767079#M243139</guid>
      <dc:creator>ikoyuncu</dc:creator>
      <dc:date>2021-09-10T17:21:29Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL Greater Than (&gt;) in WHERE clause returns wrong rows, i.e., it includes equal values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Greater-Than-gt-in-WHERE-clause-returns-wrong-rows-i-e/m-p/767083#M243144</link>
      <description>Thanks, this definitely works. I didn't create the Discount column. There has to be a formula involved when it is first created, the values look like 0.3 but they're not precise.</description>
      <pubDate>Fri, 10 Sep 2021 17:53:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Greater-Than-gt-in-WHERE-clause-returns-wrong-rows-i-e/m-p/767083#M243144</guid>
      <dc:creator>ikoyuncu</dc:creator>
      <dc:date>2021-09-10T17:53:07Z</dc:date>
    </item>
  </channel>
</rss>

