<?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: Compare numeric data type with different format in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Compare-numeric-data-type-with-different-format/m-p/767589#M243375</link>
    <description>&lt;P&gt;Note that dealing with floating point numbers is not unique to SAS.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have;
  similar = abs(rate1-rate2)&amp;lt;0.0005;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 14 Sep 2021 00:52:00 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-09-14T00:52:00Z</dc:date>
    <item>
      <title>Compare numeric data type with different format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-numeric-data-type-with-different-format/m-p/767573#M243363</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am little lost comparing two fields of numeric data type&amp;nbsp; which has different format. As in I have two columns&lt;/P&gt;&lt;P&gt;Rate1 as numeric with Length 8, Format 6.3 and Informat as Best10.&lt;/P&gt;&lt;P&gt;Rate2 as numeric with&amp;nbsp;Length 8, Format Best11, and Informat Best11.&amp;nbsp;&lt;/P&gt;&lt;P&gt;For some compares, even if the two fields are similar in value it displaying as 'Yes' from my case statement.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I see for Rate2 format there is leading white spaces for all the values when I look at sas enterprise guide output window? Any suggestions, how to convert both fields in one similar format so it gives me proper results ? Thanks&amp;nbsp;&lt;/P&gt;&lt;P&gt;Rate1&amp;nbsp; &amp;nbsp; &amp;nbsp;Rate2&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;11.174&amp;nbsp; &amp;nbsp;11.174&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select ID, rate1, rate2, 
case when rate1=rate2 then 'No' 
else 'Yes' 
END as Status 
From temp; 
Quit;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;/CODE&gt;&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>Mon, 13 Sep 2021 22:15:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-numeric-data-type-with-different-format/m-p/767573#M243363</guid>
      <dc:creator>sasnew_484</dc:creator>
      <dc:date>2021-09-13T22:15:19Z</dc:date>
    </item>
    <item>
      <title>Re: Compare numeric data type with different format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-numeric-data-type-with-different-format/m-p/767577#M243366</link>
      <description>&lt;P&gt;Lesson: FORMAT has NO effect on value comparisons.&lt;/P&gt;
&lt;P&gt;What it may do is make a person think two values are different, or the same, when looking at the value when the format is applied. A 6.3 format rounds the &lt;STRONG&gt;displayed&lt;/STRONG&gt; value to 3 decimals. So if the actual value is 11.17399999 or 11.17400003 it will display as 11.174. A Best format will find the "best" way to fit the value into the specified number of character positions. Run this and look at the result in the log:&lt;/P&gt;
&lt;PRE&gt;data _null_;
   x=12345678;
   put "Best 11 format" x=best11.;
   put "Best  6 format" x=best6.;
   put "Best  3 format" x=best3.;
run;&lt;/PRE&gt;
&lt;P&gt;All three values written are "equal" but displayed differently because of the number of characters allowed to display them.&lt;/P&gt;
&lt;P&gt;"Leading white spaces" just means that the column sized picked by EG to display something is wider than the number of digits that being displayed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Print the data set with a longer format assigned to both variables and see that the values are not equal perhaps:&lt;/P&gt;
&lt;PRE&gt;proc print data=temp;
   where rate1 ne rate2;
   var rate1 rate2;
   format rate1 rate2 16.13;
run;&lt;/PRE&gt;
&lt;P&gt;Rule: If you need a specific level of precision of your variables it is up to you to round or truncate to that level of precision.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also have included the difference in your result:&lt;/P&gt;
&lt;PRE&gt;proc sql;
   select ID, rate1, rate2, (rate1-rate2) as dif format=best12. , 
   case when rate1=rate2 then 'No' 
   else 'Yes' 
   END as Status 
From temp; 
Quit; &lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Sep 2021 22:54:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-numeric-data-type-with-different-format/m-p/767577#M243366</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-09-13T22:54:49Z</dc:date>
    </item>
    <item>
      <title>Re: Compare numeric data type with different format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-numeric-data-type-with-different-format/m-p/767579#M243368</link>
      <description>&lt;P&gt;Thank you so much for such clear explanation. As you suggested I ran the difference between rate 1 and rate 2 and it seems like for some the diff is in 0.0000959, 0.000829 etc. So, my case statement is displaying as 'Yes' as internally SAS compiler thinks they are different. Even after I truncate rate1 and rate2 to 3 decimal places and I still seeing the difference even if it is 12.189 values for both rate1 and rate2. How can I resolve this ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used below for truncation, I have two user defined functions&amp;nbsp;&lt;/P&gt;&lt;P&gt;Libname FD Base "/path"&lt;/P&gt;&lt;P&gt;proc fcmp outlib = FD.functions.truncations;&lt;/P&gt;&lt;P&gt;function truncn(x,d);&lt;/P&gt;&lt;P&gt;p = 10**d;&lt;/P&gt;&lt;P&gt;y = int(x*p)/p;&lt;/P&gt;&lt;P&gt;return(y);&lt;/P&gt;&lt;P&gt;endsub;&lt;/P&gt;&lt;P&gt;function truncc(x,d);&lt;/P&gt;&lt;P&gt;y = put(x,best.);&lt;/P&gt;&lt;P&gt;y= substr(y,1, find(y, '.')+d);&lt;/P&gt;&lt;P&gt;return(y);&lt;/P&gt;&lt;P&gt;endsub;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;options cmplib=FD.functions;&lt;/P&gt;&lt;P&gt;data A&lt;/P&gt;&lt;P&gt;length x n 8 c $9;&lt;/P&gt;&lt;P&gt;input x;&lt;/P&gt;&lt;P&gt;n =truncn(x,3);&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;3.14159&lt;/P&gt;&lt;P&gt;557.89763&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Sep 2021 23:25:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-numeric-data-type-with-different-format/m-p/767579#M243368</guid>
      <dc:creator>sasnew_484</dc:creator>
      <dc:date>2021-09-13T23:25:09Z</dc:date>
    </item>
    <item>
      <title>Re: Compare numeric data type with different format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-numeric-data-type-with-different-format/m-p/767587#M243373</link>
      <description>&lt;P&gt;Thanks for that tip, I can get the results I desired. What I did was calculating the diff value if it is greater than 0.0005 then the case statement result would be 'yes' if it is less than 0.0005 then 'No'.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Man, I am so new to Sas, so much to learn.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Sep 2021 23:57:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-numeric-data-type-with-different-format/m-p/767587#M243373</guid>
      <dc:creator>sasnew_484</dc:creator>
      <dc:date>2021-09-13T23:57:37Z</dc:date>
    </item>
    <item>
      <title>Re: Compare numeric data type with different format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-numeric-data-type-with-different-format/m-p/767589#M243375</link>
      <description>&lt;P&gt;Note that dealing with floating point numbers is not unique to SAS.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have;
  similar = abs(rate1-rate2)&amp;lt;0.0005;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Sep 2021 00:52:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-numeric-data-type-with-different-format/m-p/767589#M243375</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-14T00:52:00Z</dc:date>
    </item>
    <item>
      <title>Re: Compare numeric data type with different format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-numeric-data-type-with-different-format/m-p/767800#M243457</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/394062"&gt;@sasnew_484&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for that tip, I can get the results I desired. What I did was calculating the diff value if it is greater than 0.0005 then the case statement result would be 'yes' if it is less than 0.0005 then 'No'.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Man, I am so new to Sas, so much to learn.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You might want to look in the documentation for the COMPFUZZ function as well. It allows you to set a fuzz value of how close you want things to be for treating as equal, i.e one of the parameters could be 0.00005 and values within that range of each other are considered "equal".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I didn't work through your trunc functions but suspect you have a similar numeric precision issue as decimal values and internal binary representations often run into some problem with the more decimal positions the more likely for a precision issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Sep 2021 21:36:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-numeric-data-type-with-different-format/m-p/767800#M243457</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-09-14T21:36:39Z</dc:date>
    </item>
    <item>
      <title>Re: Compare numeric data type with different format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-numeric-data-type-with-different-format/m-p/768436#M243746</link>
      <description>&lt;P&gt;thank you for this information. Will keep this in my mind.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Sep 2021 17:48:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-numeric-data-type-with-different-format/m-p/768436#M243746</guid>
      <dc:creator>sasnew_484</dc:creator>
      <dc:date>2021-09-18T17:48:31Z</dc:date>
    </item>
  </channel>
</rss>

