<?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 SAS issue with sum in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-issue-with-sum/m-p/911890#M359541</link>
    <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;I run the below code to get weight schedule.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The problem is that when I want to get (sum of element)=1, SAS do not return row value such as factor1=0, factor2=0 and factor3=1.&lt;/P&gt;
&lt;P&gt;I am not sure if it is my SAS issue or else, thus I keep the count in each part so you can can check.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you please help to review?&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do factor1 = 0 to 1 by 0.1;
    do factor2 = 0 to 1 by 0.1;
      do factor3 = 0 to 1 by 0.1;
		output;
      end;
    end;
  end;
run;
/*The data set WORK.HAVE has 1331 observations and 3 variables*/

data weight_schedule; set have;
if factor1 + factor2 + factor3 = 1;
run;
/*The data set WORK.WEIGHT_SCHEDULE has 53 observations*/&lt;BR /&gt;&lt;BR /&gt;/********this return CORRECT count*******/&lt;BR /&gt;&lt;BR /&gt;data CORRECT_COUNT; set have;&lt;BR /&gt;if 0.99&amp;lt;factor1 + factor2 + factor3&amp;lt;1.01;&lt;BR /&gt;run;&lt;BR /&gt;/* The data set WORK.W has 66 observations*/&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 18 Jan 2024 00:34:45 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2024-01-18T00:34:45Z</dc:date>
    <item>
      <title>SAS issue with sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-issue-with-sum/m-p/911890#M359541</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;I run the below code to get weight schedule.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The problem is that when I want to get (sum of element)=1, SAS do not return row value such as factor1=0, factor2=0 and factor3=1.&lt;/P&gt;
&lt;P&gt;I am not sure if it is my SAS issue or else, thus I keep the count in each part so you can can check.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you please help to review?&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do factor1 = 0 to 1 by 0.1;
    do factor2 = 0 to 1 by 0.1;
      do factor3 = 0 to 1 by 0.1;
		output;
      end;
    end;
  end;
run;
/*The data set WORK.HAVE has 1331 observations and 3 variables*/

data weight_schedule; set have;
if factor1 + factor2 + factor3 = 1;
run;
/*The data set WORK.WEIGHT_SCHEDULE has 53 observations*/&lt;BR /&gt;&lt;BR /&gt;/********this return CORRECT count*******/&lt;BR /&gt;&lt;BR /&gt;data CORRECT_COUNT; set have;&lt;BR /&gt;if 0.99&amp;lt;factor1 + factor2 + factor3&amp;lt;1.01;&lt;BR /&gt;run;&lt;BR /&gt;/* The data set WORK.W has 66 observations*/&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Jan 2024 00:34:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-issue-with-sum/m-p/911890#M359541</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2024-01-18T00:34:45Z</dc:date>
    </item>
    <item>
      <title>Re: SAS issue with sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-issue-with-sum/m-p/911895#M359544</link>
      <description>&lt;P&gt;There is documentation and long discussions around numeric precision and representation of floating point numbers. You'll find them if searching a bit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What was surprising to me is that there is such a precision issue with values 0, 0 and 1. This must be related to how SAS populates the iteration variable if you've got a BY 0.1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1705540137135.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92626iFF8AC4440BEA7381/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1705540137135.png" alt="Patrick_0-1705540137135.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;If there are no further answers that provide more explanation like that this is already widely known then it would be worth to also raise this directly with SAS Tech Support.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For your case: Round your sum to some non-significant decimal before the comparison.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do factor1 = 0 to 1 by 0.1;
    do factor2 = 0 to 1 by 0.1;
      do factor3 = 0 to 1 by 0.1;
        output;
      end;
    end;
  end;
run;

data test;
  set have;
  if round(factor1 + factor2 + factor3,.000001) = 1;
  precision_diff_flg= round(factor1 + factor2 + factor3,.000001) ne (factor1 + factor2 + factor3);
run;

proc print data=test;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jan 2024 01:19:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-issue-with-sum/m-p/911895#M359544</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-01-18T01:19:44Z</dc:date>
    </item>
    <item>
      <title>Re: SAS issue with sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-issue-with-sum/m-p/911896#M359545</link>
      <description>&lt;P&gt;Your are getting a numeric precision problem. It's not a software bug. Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if round(sum(factor1, factor2, factor3), 0.1) = 1;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Jan 2024 01:14:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-issue-with-sum/m-p/911896#M359545</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2024-01-18T01:14:34Z</dc:date>
    </item>
    <item>
      <title>Re: SAS issue with sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-issue-with-sum/m-p/911897#M359546</link>
      <description>&lt;P&gt;This is a numeric precision problem, not a SAS problem.&amp;nbsp; It will happen with all computing software using floating point arithmetic (which needs to be the case given that 0.1 is the do loop increment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For instance, the loop&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do factor1=0 to 1 by 0.1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will NOT produce factor1=1, because at least one of the values 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9 can not be exactly represented using floating point arithmetic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Therefore none of the factor combinations that should have two zeroes and a one will &lt;STRIKE&gt;not&lt;/STRIKE&gt; appear.&amp;nbsp; The same will happen with some of the other combinations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suggest you use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do factor=0 to 10 by 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The program below will then produce 66 observations:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do factor1 = 0 to 10 by 1;
    do factor2 = 0 to 10 by 1;
      do factor3 = 0 to 10 by 1;
		output;
      end;
    end;
  end;
run;
/*The data set WORK.HAVE has 1331 observations and 3 variables*/

data weight_schedule; set have;
  if factor1 + factor2 + factor3 = 10;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want, you can divide the factors, and their sums by 10 AFTER you do the filtering.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Editted addition:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS: It's more than just the representation of numbers, it's the sequence of adding.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;For instance, 0.1+0.1+0.8 (factor1+factor2+factor3) appears in WEIGHT_SCHEDULE,&amp;nbsp; but 0.1+0.8+0.1 and 0.8+0.1+0.1 do not.&amp;nbsp; &amp;nbsp; I.e.&amp;nbsp; 0.1+0.8 (in either order) does not generate the exact representation of 0.9.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jan 2024 01:31:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-issue-with-sum/m-p/911897#M359546</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-01-18T01:31:55Z</dc:date>
    </item>
  </channel>
</rss>

