<?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 code for Sample size estimations for Binomial vs Normal proportions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242488#M309018</link>
    <description>&lt;P&gt;Thanks again for all the help. I value your time and effort. I will try to grasp and replicate the code and process you have described and come back with my results.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will come back to your last question too. Have a wonderful weekend.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 08 Jan 2016 22:28:18 GMT</pubDate>
    <dc:creator>Aj</dc:creator>
    <dc:date>2016-01-08T22:28:18Z</dc:date>
    <item>
      <title>SAS code for Sample size estimations for Binomial vs Normal proportions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242108#M309009</link>
      <description>&lt;P&gt;Please help me with the code for estimating sample sizes for binomial proportions when we have the values for proportion, error and confidence levels.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Two medthods of interest are :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. Sample size using modified exact computer algorithm&lt;/P&gt;&lt;P&gt;2. Sample size using large sample normal approximation method&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2016 18:57:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242108#M309009</guid>
      <dc:creator>Aj</dc:creator>
      <dc:date>2016-01-06T18:57:15Z</dc:date>
    </item>
    <item>
      <title>Re: SAS code for Sample size estimations for Binomial vs Normal proportions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242115#M309010</link>
      <description>&lt;P&gt;Have you looked at the documentation for Proc Power? Or do you have the SAS Power and Sample Size wizard installed which is an option when installing SAS Stat?&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2016 20:22:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242115#M309010</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-01-06T20:22:18Z</dc:date>
    </item>
    <item>
      <title>Re: SAS code for Sample size estimations for Binomial vs Normal proportions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242119#M309011</link>
      <description>&lt;P&gt;For item 2, the approximate sample size &lt;EM&gt;n&lt;/EM&gt; needed to obtain a 100(1-&lt;EM&gt;alpha&lt;/EM&gt;)% confidence interval for a binomial proportion &lt;EM&gt;p&lt;/EM&gt; with length +/-&lt;EM&gt;d&lt;/EM&gt; (i.e. 2&lt;EM&gt;d&lt;/EM&gt;) using&amp;nbsp;a planning value &lt;EM&gt;p0&lt;/EM&gt; (to be set to 0.5 in order to achieve the most conservative &lt;EM&gt;n&lt;/EM&gt;), I suggest (with arbitrary values for &lt;EM&gt;p0&lt;/EM&gt;, &lt;EM&gt;d&lt;/EM&gt; and &lt;EM&gt;alpha&lt;/EM&gt; just as an example):&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;data _null_;
p0=0.2;
d=0.08;
alpha=0.1;
n=ceil(p0*(1-p0)*(probit(1-alpha/2)/d)**2);
put n=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(formula according to the&amp;nbsp;&lt;SPAN&gt;classic reference&amp;nbsp;&lt;/SPAN&gt;&lt;A href="http://eu.wiley.com/WileyCDA/WileyTitle/productCd-0471887692.html" target="_blank" rel="nofollow"&gt;Statistical Intervals: A Guide for Practitioners&lt;/A&gt;,&lt;SPAN&gt;&amp;nbsp;p. 145)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;As to item 1, I'm not sure what the "modified exact computer algorithm" is.&amp;nbsp;Do you refer to&amp;nbsp;&lt;A href="http://onlinelibrary.wiley.com/doi/10.1002/sim.2146/pdf" target="_blank"&gt;this article&lt;/A&gt;&amp;nbsp;by G.T. Fosgate? I don't have access to it.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2016 20:37:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242119#M309011</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-06T20:37:56Z</dc:date>
    </item>
    <item>
      <title>Re: SAS code for Sample size estimations for Binomial vs Normal proportions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242260#M309012</link>
      <description>&lt;P&gt;Thank you very much for the code. It worked for normal approximation.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes I was reffereing to the article by G.T Fosgate. I tried to find the minimum sample size using proc power and it doesnot work. I have access to the article and I am attaching it here. I am confued on what SAS pfunction deals with binomial proportions. Thanks a lot for all the help&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2016 18:51:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242260#M309012</guid>
      <dc:creator>Aj</dc:creator>
      <dc:date>2016-01-07T18:51:55Z</dc:date>
    </item>
    <item>
      <title>Re: SAS code for Sample size estimations for Binomial vs Normal proportions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242288#M309013</link>
      <description>&lt;P&gt;Many thanks for providing the article. I have read the relevant parts of it. Very interesting! Tomorrow (CET) I will start trying to implement that algorithm in SAS.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2016 22:36:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242288#M309013</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-07T22:36:24Z</dc:date>
    </item>
    <item>
      <title>Re: SAS code for Sample size estimations for Binomial vs Normal proportions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242399#M309014</link>
      <description>&lt;P&gt;Your online documentation for Proc Power should have an example named "The Sawtooth Power Function in Proportion Analysis"&lt;/P&gt;
&lt;P&gt;It demonstrates using the normal approximation to get an estimated sample size and then examining the power available for sample sizes around that estimate. Use those results to pick a sample and examine the exact power. If that value works for your project then okay. Else pick another, wash, rinse, repeat until you have a sample and power that you are satisfied with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The Power procedure doesn't do n for specific power for exact binomiial because of the behavior of the binomial&amp;nbsp;there will almost never be an exact sample size&amp;nbsp;match for a given power and proportion. So if you are looking for a power of .8 you may have to use a sample size that yeilds a power of .791 or .808 or some such.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Review generating exact&amp;nbsp;confidence intervals for small sample, &amp;lt; 20, binomial probabilities.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The example also demonstrates that the Power may actually decrease for minor increases in sample size&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jan 2016 16:26:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242399#M309014</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-01-08T16:26:13Z</dc:date>
    </item>
    <item>
      <title>Re: SAS code for Sample size estimations for Binomial vs Normal proportions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242455#M309015</link>
      <description>&lt;P&gt;Hi ballard,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the detailed example. I am confused what the null proportion is in the Proc Power. The other confusion would be the hypothesis for the power in this scenario.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What would be the hypothesis for this ? H0: Confidence Intervals using Mid P greater than Confidence Intervals using normal Approximation?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And what is Null proportion and i know that the proportion the the proc power is the Binomial proportion.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank agin for all the inputs. Appreciate your explaination. Sorry if these sound a bit dumb.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jan 2016 20:45:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242455#M309015</guid>
      <dc:creator>Aj</dc:creator>
      <dc:date>2016-01-08T20:45:28Z</dc:date>
    </item>
    <item>
      <title>Re: SAS code for Sample size estimations for Binomial vs Normal proportions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242456#M309016</link>
      <description>&lt;P&gt;Thanks again for all your effort. I was just looking for a similar equation like&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token function"&gt;n&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;ceil&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;p0&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;p0&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token procnames"&gt;probit&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;alpha&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;d&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;**&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;using Mid- P Confidence Interval for the binomial proportions. Your previous code was simple and effective. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jan 2016 20:47:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242456#M309016</guid>
      <dc:creator>Aj</dc:creator>
      <dc:date>2016-01-08T20:47:44Z</dc:date>
    </item>
    <item>
      <title>Re: SAS code for Sample size estimations for Binomial vs Normal proportions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242479#M309017</link>
      <description>&lt;P&gt;Hi Aj,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry for not responding earlier (was busy with other work). I have a draft version of a function &lt;EM&gt;f1&lt;/EM&gt; which I thought would be an implementation of G. T. Fosgate's algorithm. However, when I compared his Table I with my results, I got a lot of discrepancies for the "Exact" columns (whereas the "Approx" columns matched perfectly, which was of course not that surprising).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then I created a slightly modified version &lt;EM&gt;f2&lt;/EM&gt; of my function (introduced a "tolerance" parameter), which takes the "sawtooth" effect into account (cf.&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw﻿&lt;/a&gt;'s post). As a result, the number of discrepancies&amp;nbsp;decreased (and could be further decreased by modifying the tolerance parameter), but was still substantial, unfortunately.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have&amp;nbsp;scrutinized two of the discrepant cases and used computer algebra software to evaluate equation (1) of the article for one case, in order to make sure that no numerical accuracy issues were involved. Result: The probability value calculated by SAS was accurate to 13 significant decimal places! So, I'm fairly confident that the accuracy is sufficient (at least for this case). In fact, the calculation showed that for n=67, p0=0.55, d=0.1 (i.e., the confidence interval [0.45, 0.65]) and alpha=0.1 the LHS of&amp;nbsp;equation (1), using x=37 (because 0.55*67=36.85) results in 0.0976628..., which is apparently &amp;lt;alpha. According to p. 2860 of the article, the "appropriate sample size has been reached when the sum of the tail probabilities [calculated with eqn. (1)] is less than ... alpha ..." Hence, it should be 67 (since all smaller sample sizes led to values &amp;gt;0.1). Interestingly, the "sawtooth" effect does not interfere with this result (for this specific combination of parameters), i.e., when the sample size is increased, the calculated probability remains &amp;lt;0.1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, the question is, why G.T. Fosgate's algorithm obtained 68, not 67 (see Table I). It is true, however, that the exact tail probabilities (as calculated with the formulas on p. 2858) have a sum greater than 0.1, but they would drop below this value not before n=76, if I'm not mistaken.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is also another minor issue: On p. 2860 it says that the "value of x ... is always 1 at the first iteration ..." I'm not too sure about this: Wouldn't x be 99 for p0=0.99 and n=100 or did I misunderstand the author?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway, here is my draft code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Define sample size functions f1 and the modified version f2 */

proc fcmp outlib=work.funcs.test;
function f1(pL, pU, conf); /* pL, pU: lower/upper confidence limit, conf: confidence level */
  alpha=round(1-conf, 1e-10);
  p0=(pL+pU)/2;
  do n0=1 to 10000 until(n0*p0-int(n0*p0)&amp;lt;1e-10);
  end;
  do n=1 to 10000 until(p&amp;lt;alpha);
    x=round(n*p0);
    p=round((pdf('binom',x,pL,n)+pdf('binom',x,pU,n))/2+1-cdf('binom',x,pL,n)+cdf('binom',x-1,pU,n),1e-10);
  end;
  return(n);
endsub;

function f2(pL, pU, conf, tol); /* tol: tolerance parameter */
  alpha=round(1-conf, 1e-10);
  p0=(pL+pU)/2;
  do n0=1 to 10000 until(n0*p0-int(n0*p0)&amp;lt;1e-10);
  end;
  do n=n0 to 10000;
    x=round(n*p0);
    p=round((pdf('binom',x,pL,n)+pdf('binom',x,pU,n))/2+1-cdf('binom',x,pL,n)+cdf('binom',x-1,pU,n),1e-10);
    if p&amp;lt;alpha &amp;amp; n1=. then n1=n;
    else if p&amp;gt;=alpha+tol then n1=.;
  end;
  return(n1);
endsub;
quit;

options cmplib=work.funcs;

/* Try to replicate the values corresponding to Table I of the article */

data test;
do i=50 to 90 by 5;
  p0=i/100;
  do conf=0.9, 0.95, 0.99;
    do d=0.1, 0.05;
      e1=f1(p0-d, p0+d, conf);
      e2=f2(p0-d, p0+d, conf, 0);
      e2t=f2(p0-d, p0+d, conf, 0.0005);
      a=ceil(p0*(1-p0)*(probit(1-(1-conf)/2)/d)**2);
      output;
    end;
  end;
end;
drop i;
run;

proc sort data=test;
by conf descending d p0;
run;

/* Read Table I of the article, "Exact" and "Approx" columns stacked side by side */

data orig;
input e a;
p0=(50+5*mod(_n_-1,9))/100;
if mod(_n_-1,18)&amp;lt;9 then d=0.1;
else d=0.05;
if _n_&amp;lt;=18 then conf=0.9;
else if _n_&amp;lt;=36 then conf=0.95;
else conf=0.99;
cards;
68 68
68 67
65 65
...  /* please copy from the PDF yourself */
353 339
260 239
;

/* Compare the results */

proc compare data=test c=orig;
id p0 conf d notsorted;
var e:;
with e e e;
run;

/* Investigate a particular discrepancy */

data ttt;
pL=0.45; pU=0.65; conf=0.9;
alpha=round(1-conf, 1e-10);
put alpha= best20.;
put alpha= hex16.;
p0=round((pL+pU)/2, 1e-10);
put p0= best20.;
put p0= hex16.;
do n=1 to 120;
  x=round(n*p0);
  p=(pdf('binom',x,pL,n)+pdf('binom',x,pU,n))/2+1-cdf('binom',x,pL,n)+cdf('binom',x-1,pU,n);
  output;
end;
proc print width=min;
format p best16.;
run;

/* Calculate some exact tail probabilities */

data _null_;
x1=1-cdf('binom',36,0.45,67);
x2=cdf('binom',37,0.65,67);
x=x1+x2;
put (x:)(=);
run; /* 0.122179 */

data _null_;
x1=1-cdf('binom',36,0.45,68);
x2=cdf('binom',37,0.65,68);
x=x1+x2;
put (x:)(=);
run; /* 0.121597 */

data _null_;
x1=1-cdf('binom',40,0.45,75);
x2=cdf('binom',41,0.65,75);
x=x1+x2;
put (x:)(=);
run; /* 0.100311 */

data _null_;
x1=1-cdf('binom',41,0.45,76);
x2=cdf('binom',42,0.65,76);
x=x1+x2;
put (x:)(=);
run; /* 0.096772 */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I hope this helps. Please don't hesitate to ask if you have further&amp;nbsp;questions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In any case, as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw﻿&lt;/a&gt;&amp;nbsp;has suggested, you can calculate tail probabilities fairly easily, both exactly and approxmately using equation (1), see my code above. So, for a given&amp;nbsp;combination of parameters you will be able to obtain the optimal sample size with only little effort and without complicated general algorithms.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jan 2016 21:59:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242479#M309017</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-08T21:59:53Z</dc:date>
    </item>
    <item>
      <title>Re: SAS code for Sample size estimations for Binomial vs Normal proportions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242488#M309018</link>
      <description>&lt;P&gt;Thanks again for all the help. I value your time and effort. I will try to grasp and replicate the code and process you have described and come back with my results.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will come back to your last question too. Have a wonderful weekend.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jan 2016 22:28:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-code-for-Sample-size-estimations-for-Binomial-vs-Normal/m-p/242488#M309018</guid>
      <dc:creator>Aj</dc:creator>
      <dc:date>2016-01-08T22:28:18Z</dc:date>
    </item>
  </channel>
</rss>

