<?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: addition of variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/addition-of-variables/m-p/578631#M164140</link>
    <description>&lt;P&gt;SAS stores all numbers as 8-byte real (binary).&lt;/P&gt;
&lt;P&gt;Most decimal fractions cannot be exactly represented in binary, and calculations cause additional aberrations like the one you experienced. Rounding gets the original imprecision back, so the compare for equal works again.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 02 Aug 2019 08:03:54 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-08-02T08:03:54Z</dc:date>
    <item>
      <title>addition of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/addition-of-variables/m-p/578298#M163987</link>
      <description>&lt;P class="x_MsoNormal"&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%let&amp;nbsp;z=-.4;

&amp;nbsp;

data&amp;nbsp;new;

x=2.05000;

x1=2.45;

a=0&amp;nbsp;&amp;lt; x &amp;lt; &amp;amp;z.+x1;

run;&lt;/PRE&gt;&lt;P class="x_MsoNormal"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="x_MsoNormal"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="x_MsoNormal"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="x_MsoNormal"&gt;&lt;SPAN&gt;when I am running this code the value of&amp;nbsp; variable a in dataset new is coming 1 why ?&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="x_MsoNormal"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="x_MsoNormal"&gt;&lt;SPAN&gt;I think it should come as 0&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Aug 2019 08:59:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/addition-of-variables/m-p/578298#M163987</guid>
      <dc:creator>shubham1</dc:creator>
      <dc:date>2019-08-01T08:59:37Z</dc:date>
    </item>
    <item>
      <title>Re: addition of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/addition-of-variables/m-p/578303#M163988</link>
      <description>&lt;P&gt;&lt;SPAN&gt;. The result of a comparison operation is 1 if the comparison is true and 0 if it is false.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Aug 2019 09:15:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/addition-of-variables/m-p/578303#M163988</guid>
      <dc:creator>singhsahab</dc:creator>
      <dc:date>2019-08-01T09:15:38Z</dc:date>
    </item>
    <item>
      <title>Re: addition of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/addition-of-variables/m-p/578304#M163989</link>
      <description>&lt;P&gt;Let's go through this step by step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let z=-.4;

data new;
x = 2.05000;
x1 = 2.45;
a = 0 &amp;lt; x &amp;lt; &amp;amp;z. + x1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;First, the macro variable is resolved:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
x = 2.05000;
x1 = 2.45;
a = 0 &amp;lt; x &amp;lt; -.4 + x1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Next, the calculation is done:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
x = 2.05000;
x1 = 2.45;
a = 0 &amp;lt; x &amp;lt; -.4 + 2.45;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The calculation involves a decimal fraction, which is often a big thing with SAS, because of numeric precision:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
x1 = -.4 + 2.45;
x2 = 2.05;
x3 = x1 - x2;
format x3 best32.;
run;

proc print data=test noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt; x1      x2                                   x3

2.05    2.05                 4.4408920985006E-16
&lt;/PRE&gt;
&lt;P&gt;So let's get back to your original code, and prudently apply the round() function to the calculation:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let z=-.4;

data new;
x = 2.05000;
x1 = 2.45;
a = 0 &amp;lt; x &amp;lt; round(&amp;amp;z. + x1,.001);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You will see that now you get your intended result.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Aug 2019 09:16:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/addition-of-variables/m-p/578304#M163989</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-08-01T09:16:28Z</dc:date>
    </item>
    <item>
      <title>Re: addition of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/addition-of-variables/m-p/578619#M164131</link>
      <description>&lt;P&gt;thanks for your answer .it works correctly&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I have a question why decimal fraction is behaving&amp;nbsp; like this and what does round function has done in this case&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 05:52:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/addition-of-variables/m-p/578619#M164131</guid>
      <dc:creator>shubham1</dc:creator>
      <dc:date>2019-08-02T05:52:10Z</dc:date>
    </item>
    <item>
      <title>Re: addition of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/addition-of-variables/m-p/578629#M164138</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/127952"&gt;@shubham1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;But I have a question why decimal fraction is behaving&amp;nbsp; like this and what does round function has done in this case&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/127952"&gt;@shubham1&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A short answer is: Internally, the computer doesn't use the decimal system, but the binary system. The vast majority of "short" decimal fractions (such as 0.1, 1.234, etc.) have &lt;EM&gt;infinitely&lt;/EM&gt; many &lt;EM&gt;binary&lt;/EM&gt; digits (quite similar to 1/3=0.3333333... in the decimal system), but need to be stored in a &lt;EM&gt;finite&lt;/EM&gt; number of bits in the computer's memory. The unavoidable rounding errors due to this limitation can add up to larger errors in calculations. The ROUND function removes the additional error because this error is still much smaller than the rounding unit in the second argument, here: .001.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For more details and examples, please see the documentation &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; and/or other discussions on here about this topic (where I've contributed detailed explanations), e.g.:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/ceil-function-bug/m-p/239414#M44044" target="_blank" rel="noopener"&gt;ceil function bug?&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="https://communities.sas.com/t5/New-SAS-User/Why-is-my-PROC-SQL-WHERE-statement-not-recognising-all-variable/m-p/570882#M12047" target="_blank" rel="noopener"&gt;Why is my PROC SQL WHERE statement not recognising all variable values?&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Bizarre-Issue-with-subsetting-where-var-macro-var/m-p/490605#M128391" target="_blank" rel="noopener"&gt;Bizarre Issue with subsetting where var= macro var&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="https://communities.sas.com/t5/New-SAS-User/decimal-precision-of-a-variable/m-p/539430#M7065" target="_blank" rel="noopener"&gt;decimal precision of a variable&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Fri, 02 Aug 2019 07:47:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/addition-of-variables/m-p/578629#M164138</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-08-02T07:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: addition of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/addition-of-variables/m-p/578631#M164140</link>
      <description>&lt;P&gt;SAS stores all numbers as 8-byte real (binary).&lt;/P&gt;
&lt;P&gt;Most decimal fractions cannot be exactly represented in binary, and calculations cause additional aberrations like the one you experienced. Rounding gets the original imprecision back, so the compare for equal works again.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 08:03:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/addition-of-variables/m-p/578631#M164140</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-08-02T08:03:54Z</dc:date>
    </item>
  </channel>
</rss>

