<?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: Retain variable output issue in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain-variable-output-issue/m-p/771056#M244622</link>
    <description>&lt;P&gt;Or you could round each sum as it is calculated.&amp;nbsp; &amp;nbsp;If the highest precision of the original data is in the hundredths, then round to nearest .01, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
format val 23.19;
input key val;
cards;
1 -0.16
1 0.15
1 0.01
run;

data Final;
   set test;
   retain TOT;
   by key ;
   if FIRST.key then do; TOT = 0;end;

   tot=round(sum(tot,val),.01);
   output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 29 Sep 2021 02:46:44 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2021-09-29T02:46:44Z</dc:date>
    <item>
      <title>Retain variable output issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-variable-output-issue/m-p/771028#M244611</link>
      <description>&lt;P&gt;The last value of "TOT" in the table "Final" should have been 0, but some -ve number updated:&lt;/P&gt;&lt;P&gt;Any idea what is causing the issue?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;format val 23.19;&lt;BR /&gt;input key val;&lt;BR /&gt;cards;&lt;BR /&gt;1 -0.16&lt;BR /&gt;1 0.15&lt;BR /&gt;1 0.01&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data Final;&lt;BR /&gt;set test;&lt;BR /&gt;retain TOT;&lt;BR /&gt;by key ;&lt;BR /&gt;if FIRST.key then do; TOT = 0;&lt;BR /&gt;end;&lt;BR /&gt;TOT = TOT+val;&lt;BR /&gt;output;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Sep 2021 21:49:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-variable-output-issue/m-p/771028#M244611</guid>
      <dc:creator>Mushy</dc:creator>
      <dc:date>2021-09-28T21:49:15Z</dc:date>
    </item>
    <item>
      <title>Re: Retain variable output issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-variable-output-issue/m-p/771031#M244612</link>
      <description>&lt;P&gt;Machine precision (or machine epsilon) is the issue. Most non-integer numbers cannot be represented exactly in computer binary arithmetic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://en.wikipedia.org/wiki/Machine_epsilon" target="_blank"&gt;https://en.wikipedia.org/wiki/Machine_epsilon&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/v_014/lepg/p0dv87zb3bnse6n1mqo360be70qr.htm" target="_blank"&gt;https://go.documentation.sas.com/doc/en/pgmsascdc/v_014/lepg/p0dv87zb3bnse6n1mqo360be70qr.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Sep 2021 21:59:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-variable-output-issue/m-p/771031#M244612</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-28T21:59:26Z</dc:date>
    </item>
    <item>
      <title>Re: Retain variable output issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-variable-output-issue/m-p/771036#M244616</link>
      <description>&lt;P&gt;Thank you Miller for the information.&lt;/P&gt;&lt;P&gt;Do we have an alternate/solution for this issue for my example?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Sep 2021 22:26:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-variable-output-issue/m-p/771036#M244616</guid>
      <dc:creator>Mushy</dc:creator>
      <dc:date>2021-09-28T22:26:09Z</dc:date>
    </item>
    <item>
      <title>Re: Retain variable output issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-variable-output-issue/m-p/771044#M244619</link>
      <description>&lt;P&gt;One way that may be a bit fragile:&lt;/P&gt;
&lt;PRE&gt;data Final;
   set test;
   retain TOT;
   by key ;
   if FIRST.key then do; TOT = 0;
   end;
   tempval = val*100;
   TOT = TOT+tempval;
   if last.key then tot=tot/100;
   output;
run;&lt;/PRE&gt;
&lt;P&gt;This is attempting to use integer arithmetic as much as practical. The fragile part comes partially in knowing the minimum multiplier. There is also some behind the scenes rounding going on. Works for this small example.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have enough decimal places this still likely run into precision issues though.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Sep 2021 22:53:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-variable-output-issue/m-p/771044#M244619</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-09-28T22:53:07Z</dc:date>
    </item>
    <item>
      <title>Re: Retain variable output issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-variable-output-issue/m-p/771056#M244622</link>
      <description>&lt;P&gt;Or you could round each sum as it is calculated.&amp;nbsp; &amp;nbsp;If the highest precision of the original data is in the hundredths, then round to nearest .01, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
format val 23.19;
input key val;
cards;
1 -0.16
1 0.15
1 0.01
run;

data Final;
   set test;
   retain TOT;
   by key ;
   if FIRST.key then do; TOT = 0;end;

   tot=round(sum(tot,val),.01);
   output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Sep 2021 02:46:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-variable-output-issue/m-p/771056#M244622</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-09-29T02:46:44Z</dc:date>
    </item>
    <item>
      <title>Re: Retain variable output issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-variable-output-issue/m-p/771125#M244661</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/393424"&gt;@Mushy&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I mostly use the ROUND function (as &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461" target="_blank" rel="noopener"&gt;mkeintz&lt;/A&gt; suggested) in situations like this, often with a very small rounding unit (e.g., &lt;FONT face="courier new,courier"&gt;1E-9&lt;/FONT&gt;) -- small enough not to influence results, but large enough to remove the tiny rounding error (!) that would occur otherwise. In your example the absolute rounding error is &lt;FONT face="courier new,courier"&gt;&amp;lt;1E-17&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Even if you want to switch to integer calculations (as &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884" target="_blank" rel="noopener"&gt;ballardw&lt;/A&gt; suggested), you should guard against such tiny rounding errors while performing the conversion to integers, so that you need to round anyway:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data check;
do n=0 to 99;
  val=n/100;
  tempval = val*100; /* no rounding --&amp;gt; non-integers can occur */
  r = round(val*100, 1e-9);
  clean  = (tempval=n);
  cleanr = (r=n);
  output;
end;
run;

proc freq data=check;
tables clean cleanr;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result (with SAS 9.4M5 under Windows)&lt;FONT face="helvetica"&gt;:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;                                  Cumulative    Cumulative
clean    Frequency     Percent     Frequency      Percent
----------------------------------------------------------
 &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;   0           8        8.00             8         8.00&lt;/FONT&gt;&lt;/STRONG&gt;
    1          92       92.00           100       100.00


                                   Cumulative    Cumulative
cleanr    Frequency     Percent     Frequency      Percent
-----------------------------------------------------------
    &lt;STRONG&gt;&lt;FONT color="#008000"&gt; 1         100      100.00           100       100.00&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Sep 2021 12:43:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-variable-output-issue/m-p/771125#M244661</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-09-29T12:43:45Z</dc:date>
    </item>
  </channel>
</rss>

