<?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 rounding error in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/257865#M49559</link>
    <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the total number of pupils in the year 1992 &amp;nbsp;and I need &amp;nbsp;to calculate&amp;nbsp;the number of pupils &amp;nbsp;in each course in 1992. see below code .&amp;nbsp;The problem is &amp;nbsp;the numbers I obtained &amp;nbsp;have decimals points, when I round them they don't always add up to the total. &amp;nbsp;Does anyone know how to fix this issue so &amp;nbsp;when I sum&amp;nbsp;&lt;SPAN&gt;the number of pupils &amp;nbsp;in each course it would add up to 1145.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;data have;
input id :$4. pupils1990 fraction total1992;
datalines;
100	50	0.04784689	1145
101	5	0.004784689	1145
102	131	0.125358852	1145
104	21	0.020095694	1145
105	15	0.014354067	1145
106	3	0.002870813	1145
107	12	0.011483254	1145
108	5	0.004784689	1145
109	17	0.016267943	1145
110	29	0.027751196	1145
112	152	0.145454545	1145
113	23	0.022009569	1145
114	37	0.035406699	1145
115	40	0.038277512	1145
116	65	0.062200957	1145
117	52	0.049760766	1145
118	44	0.042105263	1145
119	7	0.006698565	1145
120	9	0.00861244	1145
121	4	0.003827751	1145
122	7	0.006698565	1145
123	8	0.007655502	1145
124	27	0.025837321	1145
125	234	0.223923445	1145
126	16	0.015311005	1145
127	16	0.015311005	1145
128	16	0.015311005	1145
;

data want;
set have;
pupils1992 = round(fraction*total1992);
run;
proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Any help would be greatly appreciated. Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 20 Mar 2016 20:02:57 GMT</pubDate>
    <dc:creator>archibald</dc:creator>
    <dc:date>2016-03-20T20:02:57Z</dc:date>
    <item>
      <title>rounding error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/257865#M49559</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the total number of pupils in the year 1992 &amp;nbsp;and I need &amp;nbsp;to calculate&amp;nbsp;the number of pupils &amp;nbsp;in each course in 1992. see below code .&amp;nbsp;The problem is &amp;nbsp;the numbers I obtained &amp;nbsp;have decimals points, when I round them they don't always add up to the total. &amp;nbsp;Does anyone know how to fix this issue so &amp;nbsp;when I sum&amp;nbsp;&lt;SPAN&gt;the number of pupils &amp;nbsp;in each course it would add up to 1145.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;data have;
input id :$4. pupils1990 fraction total1992;
datalines;
100	50	0.04784689	1145
101	5	0.004784689	1145
102	131	0.125358852	1145
104	21	0.020095694	1145
105	15	0.014354067	1145
106	3	0.002870813	1145
107	12	0.011483254	1145
108	5	0.004784689	1145
109	17	0.016267943	1145
110	29	0.027751196	1145
112	152	0.145454545	1145
113	23	0.022009569	1145
114	37	0.035406699	1145
115	40	0.038277512	1145
116	65	0.062200957	1145
117	52	0.049760766	1145
118	44	0.042105263	1145
119	7	0.006698565	1145
120	9	0.00861244	1145
121	4	0.003827751	1145
122	7	0.006698565	1145
123	8	0.007655502	1145
124	27	0.025837321	1145
125	234	0.223923445	1145
126	16	0.015311005	1145
127	16	0.015311005	1145
128	16	0.015311005	1145
;

data want;
set have;
pupils1992 = round(fraction*total1992);
run;
proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Any help would be greatly appreciated. Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Mar 2016 20:02:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/257865#M49559</guid>
      <dc:creator>archibald</dc:creator>
      <dc:date>2016-03-20T20:02:57Z</dc:date>
    </item>
    <item>
      <title>Re: rounding error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/257870#M49562</link>
      <description>&lt;P&gt;Go with cumulative fractions instead:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
retain pupils1992;
set have;
cumFrac + fraction;
pupils1992 = round(cumFrac*total1992 - cumPup);
cumPup + pupils1992;
drop cum:;
run;

proc print data=want noobs; 
var id pupils1990 fraction pupils1992; 
sum pupils1990 pupils1992 fraction;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 20 Mar 2016 20:28:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/257870#M49562</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-03-20T20:28:17Z</dc:date>
    </item>
    <item>
      <title>Re: rounding error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/257878#M49565</link>
      <description>&lt;P&gt;Thank you very much PGStats , I will try this code. &amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, excuse &amp;nbsp;my naivety, but can you explain what will &amp;nbsp;the following line of code achieve?&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;pupils1992 &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;round&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;cumFrac&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;total1992 &lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt; cumPup&lt;SPAN class="token punctuation"&gt;);&lt;/SPAN&gt;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Mar 2016 21:19:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/257878#M49565</guid>
      <dc:creator>archibald</dc:creator>
      <dc:date>2016-03-20T21:19:50Z</dc:date>
    </item>
    <item>
      <title>Re: rounding error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/257883#M49566</link>
      <description>&lt;PRE class="  language-sas"&gt;&lt;CODE class="  language-sas"&gt;pupils1992 &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;round&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;cumFrac&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;total1992 &lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt; cumPup&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;will calculate the difference between the cumulative fraction for the current observation and the cumulative number of pupils for the previous observation. That way, the accumulated difference between the number of pupils (integers) and fractions (real numbers) is compensated as soon as it gets greater than 0.5.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Mar 2016 22:20:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/257883#M49566</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-03-20T22:20:10Z</dc:date>
    </item>
    <item>
      <title>Re: rounding error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/257978#M49602</link>
      <description>&lt;P&gt;Thank you very much&amp;nbsp;PGStats, the code works great!&lt;/P&gt;&lt;P&gt;Now &amp;nbsp;I added 60 schools in my dataset&amp;nbsp; and I need to calculate the same&amp;nbsp; estimate.I applied the code provided and I realized that the code produce a cumulative of&amp;nbsp; the last observation of the current &amp;nbsp;school and the first observation of the next school. Is there a way to fix this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Mar 2016 15:07:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/257978#M49602</guid>
      <dc:creator>archibald</dc:creator>
      <dc:date>2016-03-21T15:07:00Z</dc:date>
    </item>
    <item>
      <title>Re: rounding error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/257981#M49605</link>
      <description>&lt;P&gt;I assumed you have a new variable called school&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id :$4. pupils1990 fraction school $ total1992;
datalines;
100	50	0.04784689	A 1145
101	5	0.004784689	A 1145
102	131	0.125358852	A 1145
104	21	0.020095694	A 1145
105	15	0.014354067	A 1145
106	3	0.002870813	A 1145
107	12	0.011483254	A 1145
108	5	0.004784689	A 1145
109	17	0.016267943	A 1145
110	29	0.027751196	A 1145
112	152	0.145454545	A 1145
113	23	0.022009569	A 1145
114	37	0.035406699	A 1145
115	40	0.038277512	A 1145
116	65	0.062200957	A 1145
117	52	0.049760766	A 1145
118	44	0.042105263	A 1145
119	7	0.006698565	A 1145
120	9	0.00861244	A 1145
121	4	0.003827751	A 1145
122	7	0.006698565	A 1145
123	8	0.007655502	A 1145
124	27	0.025837321	A 1145
125	234	0.223923445	A 1145
126	16	0.015311005	A 1145
127	16	0.015311005	A 1145
128	16	0.015311005	A 1145
100	50	0.04784689	B 1155
101	5	0.004784689	B 1155
102	131	0.125358852	B 1155
104	21	0.020095694	B 1155
105	15	0.014354067	B 1155
106	3	0.002870813	B 1155
107	12	0.011483254	B 1155
108	5	0.004784689	B 1155
109	17	0.016267943	B 1155
110	29	0.027751196	B 1155
112	152	0.145454545	B 1155
113	23	0.022009569	B 1155
114	37	0.035406699	B 1155
115	40	0.038277512	B 1155
116	65	0.062200957	B 1155
117	52	0.049760766	B 1155
118	44	0.042105263	B 1155
119	7	0.006698565	B 1155
120	9	0.00861244	B 1155
121	4	0.003827751	B 1155
122	7	0.006698565	B 1155
123	8	0.007655502	B 1155
124	27	0.025837321	B 1155
125	234	0.223923445	B 1155
126	16	0.015311005	B 1155
127	16	0.015311005	B 1155
128	16	0.015311005	B 1155
;

data want;
retain pupils1992;
set have;
by school notsorted;
if first.school then do;
    cumFrac = 0;
    cumPup = 0;
    end;
cumFrac + fraction;
pupils1992 = round(cumFrac*total1992 - cumPup);
cumPup + pupils1992;
drop cum:;
run;

proc print data=want noobs; 
by school notsorted;
var school id pupils1990 fraction pupils1992; 
sum pupils1990 pupils1992 fraction;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Mar 2016 15:20:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/257981#M49605</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-03-21T15:20:23Z</dc:date>
    </item>
    <item>
      <title>Re: rounding error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/258032#M49624</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/72261"&gt;@archibald﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note that the advantage of getting integers adding up to the given total comes at a price: The numbers obtained contain a "random component" in the sense that they depend (in general) on the order of courses.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example: The "exact" estimate for course id 123 is 8.765... Currently, the integer estimate is 8, but it could very well be 9 if the courses were numbered (or ordered) differently.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Whether this is an issue depends on what you plan to do with the results.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Mar 2016 17:34:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/258032#M49624</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-21T17:34:18Z</dc:date>
    </item>
    <item>
      <title>Re: rounding error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/258040#M49627</link>
      <description>&lt;P&gt;That is perfectly true&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh﻿&lt;/a&gt;. The end result depends on the order of allocation. I have met this problem in the context of sample allocation where I found that it is generally preferable to do the allocation from the lowest to the largest fraction,&amp;nbsp;especially when a minimum stratum sample size (=1) had to be enforced.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Mar 2016 18:14:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/258040#M49627</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-03-21T18:14:28Z</dc:date>
    </item>
    <item>
      <title>Re: rounding error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/258083#M49646</link>
      <description>greatly appreciate your input, FreelanceReinhard . I understand this the downside of the method.&lt;BR /&gt;so would it be advisable to sort by id course to get the true integers, since 8.756 should really be 9 and not 8?</description>
      <pubDate>Mon, 21 Mar 2016 20:54:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/258083#M49646</guid>
      <dc:creator>archibald</dc:creator>
      <dc:date>2016-03-21T20:54:57Z</dc:date>
    </item>
    <item>
      <title>Re: rounding error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/258086#M49647</link>
      <description>&lt;P&gt;you nailed this perfectly. thank you , thank you, thank you&lt;/P&gt;</description>
      <pubDate>Mon, 21 Mar 2016 20:56:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/258086#M49647</guid>
      <dc:creator>archibald</dc:creator>
      <dc:date>2016-03-21T20:56:51Z</dc:date>
    </item>
    <item>
      <title>Re: rounding error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/258088#M49648</link>
      <description>&lt;P&gt;The data &lt;EM&gt;are&lt;/EM&gt; currently sorted by ID (within school). Unfortunately, in general you can't have "correctly" rounded integers and at the same time hit the prespecified sum exactly.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Mar 2016 21:02:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/258088#M49648</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-21T21:02:50Z</dc:date>
    </item>
    <item>
      <title>Re: rounding error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/258104#M49654</link>
      <description>&lt;P&gt;Got it ! thank you for your valuable input!&lt;/P&gt;</description>
      <pubDate>Mon, 21 Mar 2016 22:26:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rounding-error/m-p/258104#M49654</guid>
      <dc:creator>archibald</dc:creator>
      <dc:date>2016-03-21T22:26:15Z</dc:date>
    </item>
  </channel>
</rss>

