<?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: SAS decimal points unexpected result in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-decimal-points-unexpected-result/m-p/539655#M148734</link>
    <description>&lt;P&gt;Thank you for your reply and explanation. I tried to use round function and it works. So based on your explanation, whenever decimal number is produced, we should use round function, otherwise, the result might be unexpected.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;yangx&lt;/P&gt;</description>
    <pubDate>Fri, 01 Mar 2019 14:53:24 GMT</pubDate>
    <dc:creator>yangx</dc:creator>
    <dc:date>2019-03-01T14:53:24Z</dc:date>
    <item>
      <title>SAS decimal points unexpected result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-decimal-points-unexpected-result/m-p/539494#M148659</link>
      <description>&lt;P&gt;Dear Support,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a very simple program which used a number to divide group, but the result is unexpected. I cannot find the reason for this. Could you please help me with this? Please see the program below:&lt;/P&gt;&lt;P&gt;data t1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; input id a b;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; cards;&lt;/P&gt;&lt;P&gt;1 56 50.4&lt;/P&gt;&lt;P&gt;2 25 22.5&lt;/P&gt;&lt;P&gt;3 55 49.4&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;data t2;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; set t1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; c=((a-b)*100/a);&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; group=0;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; if 0&amp;lt;=c=10 then group=1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; if c&amp;gt;10 then group=0;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;For id 1 and 2, c has value of 10, so group should equal to 1, however, group has 0 and 1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is appreciated. thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Xiumei&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 21:32:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-decimal-points-unexpected-result/m-p/539494#M148659</guid>
      <dc:creator>yangx</dc:creator>
      <dc:date>2019-02-28T21:32:07Z</dc:date>
    </item>
    <item>
      <title>Re: SAS decimal points unexpected result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-decimal-points-unexpected-result/m-p/539497#M148662</link>
      <description>&lt;P&gt;did you intend&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;if 0&amp;lt;= c &amp;lt;=10 then group=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;instead of&lt;/P&gt;
&lt;P&gt;&amp;nbsp;if 0&amp;lt;=c=10 then group=1;&lt;/P&gt;
&lt;P&gt;in the latter if c is not equal to 10 then the c=10 bit is false and group is not set to 1&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 21:47:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-decimal-points-unexpected-result/m-p/539497#M148662</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-02-28T21:47:50Z</dc:date>
    </item>
    <item>
      <title>Re: SAS decimal points unexpected result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-decimal-points-unexpected-result/m-p/539499#M148664</link>
      <description>&lt;P&gt;Your IF statment is likely missing a &amp;lt; sign.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    if 0&amp;lt;=&lt;FONT size="4" color="#FF0000"&gt;&lt;STRONG&gt;c=10&lt;/STRONG&gt;&lt;/FONT&gt; then group=1;

    if c&amp;gt;10 then group=0;&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;Should likely be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    if 0 &amp;lt;= c &amp;lt;= 10 then group=1;
    else if c &amp;gt; 10 then group=0; * not needed since it was set already to 0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You also may want to round the number to avoid issues.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This may help:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data t1;
    input id a b;
    cards;
1 56 50.4
2 25 22.5
3 55 49.4
;

run;

data t2;

    set t1;

    c=((a-b)*100)/a;
	d = round(c, 0.001);

    group=0;

    if 0 &amp;lt;= c &amp;lt;= 10 then group=1;
    else if c &amp;gt; 10 then group=0; * not needed since it was set already to 0;

	if 0 &amp;lt;= d &amp;lt;= 10 then groupD=1;
    else if d &amp;gt; 10 then groupD=0; * not needed since it was set already to 0;
    format c d 8.3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/155882"&gt;@yangx&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Dear Support,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a very simple program which used a number to divide group, but the result is unexpected. I cannot find the reason for this. Could you please help me with this? Please see the program below:&lt;/P&gt;
&lt;P&gt;data t1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; input id a b;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; cards;&lt;/P&gt;
&lt;P&gt;1 56 50.4&lt;/P&gt;
&lt;P&gt;2 25 22.5&lt;/P&gt;
&lt;P&gt;3 55 49.4&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;data t2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; set t1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; c=((a-b)*100/a);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; group=0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; if 0&amp;lt;=c=10 then group=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; if c&amp;gt;10 then group=0;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;For id 1 and 2, c has value of 10, so group should equal to 1, however, group has 0 and 1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help is appreciated. thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Xiumei&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 21:49:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-decimal-points-unexpected-result/m-p/539499#M148664</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-28T21:49:59Z</dc:date>
    </item>
    <item>
      <title>Re: SAS decimal points unexpected result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-decimal-points-unexpected-result/m-p/539582#M148697</link>
      <description>&lt;P&gt;You obviously have an error in your program. The statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    if 0&amp;lt;=c=10 then group=1;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;resolves to&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if 0&amp;lt;=c and c=10;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So I assume that you meant&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if 0&amp;lt;=c&amp;lt;=10 then group=1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The problem you have, apart from that, is that SAS floating points are base 2, not base 10. This means that a decimal which is not a (negative) power of 2 is not represented exactly. So, for ID = 1 you have 50.4, which is not exactly representable in base 2. And, obviously, while the C value displays as 10, it is not exactly 10. If you subset your output data with "where c=10", you will see that your first observation is not included.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use ROUND to get around this problem, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;c=round((a-b)*100/a,0.0001);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Mar 2019 10:50:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-decimal-points-unexpected-result/m-p/539582#M148697</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-03-01T10:50:07Z</dc:date>
    </item>
    <item>
      <title>Re: SAS decimal points unexpected result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-decimal-points-unexpected-result/m-p/539637#M148727</link>
      <description>&lt;P&gt;Thanks for your reply, but&amp;nbsp; "&lt;SPAN&gt;if 0&amp;lt;= c &amp;lt;=10 then group=1" is doing the same thing, doesn't work.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 14:27:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-decimal-points-unexpected-result/m-p/539637#M148727</guid>
      <dc:creator>yangx</dc:creator>
      <dc:date>2019-03-01T14:27:17Z</dc:date>
    </item>
    <item>
      <title>Re: SAS decimal points unexpected result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-decimal-points-unexpected-result/m-p/539655#M148734</link>
      <description>&lt;P&gt;Thank you for your reply and explanation. I tried to use round function and it works. So based on your explanation, whenever decimal number is produced, we should use round function, otherwise, the result might be unexpected.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;yangx&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 14:53:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-decimal-points-unexpected-result/m-p/539655#M148734</guid>
      <dc:creator>yangx</dc:creator>
      <dc:date>2019-03-01T14:53:24Z</dc:date>
    </item>
  </channel>
</rss>

