Hello,
I received programming notes to update my code using "min" , "max" function. Here is the request :
my code (not sure if i did it correctly, because I have 95CI% greater than 100%)
data tbl;
set tbl;
array xcol_ {3} col1-col3;
array xn_ {3} xn1-xn3;
array xcount_ {3} xcount1-xcount3;
array xp_ {3} xp1-xp3;
array xlci_ {3} xlci1-xlci3;
array xuci_ {3} xuci1-xuci3;
retain xn1-xn3 xcount1-xcount3 0;
if anbr=4 and row_text='N' then delete;
else if anbr=4 then do;
if row_text='n' and row_type='N' then do;
do j=1 to 3;
xn_{j} = xcol_{j};
end;
end;
if row_text='Success' and row_type='COUNT' then do;
do j=1 to 3;
xcount_{j} = xcol_{j};
end;
end;
if compress(row_text)='(95%CI)' then do; /* calcualte binomial CI to replace CP CI */
indent=1;
row_text='Two-Sided 95% Confidence Interval for Percentage (Normal Approximation to Binomial)';
xz=quantile('NORMAL', 0.975);
do j=1 to 3;
xp_{j} = xcount_{j} / xn_{j};
xlci_{j}=max(xp_{j} - xz*sqrt(xp_{j}*(1-xp_{j})/xn_{j}),0)*100;
xuci_{j}=min(xp_{j} + xz*sqrt(xp_{j}*(1-xp_{j})/xn_{j}),100)*100;
xcol_{j}='('||compress(put(xlci_{j},8.2))||'%,'||compress(put(xuci_{j},8.2))||'%)';
end;
end;
end;
run;
Can you post example input data (as a DATA step with the CARDS statement)?
I didn't go through the code, but if the problem is you are getting upper confidence limit of 10000%, then I think the problem may be this line:
xuci_{j}=min(xp_{j} + xz*sqrt(xp_{j}*(1-xp_{j})/xn_{j}),100)*100;
Should be:
xuci_{j}=min(xp_{j} + xz*sqrt(xp_{j}*(1-xp_{j})/xn_{j}),1)*100;
Your value xp_{j} is a proportion (with range 0-1), not a percentage (with range 0-100).
Can you post example input data (as a DATA step with the CARDS statement)?
I didn't go through the code, but if the problem is you are getting upper confidence limit of 10000%, then I think the problem may be this line:
xuci_{j}=min(xp_{j} + xz*sqrt(xp_{j}*(1-xp_{j})/xn_{j}),100)*100;
Should be:
xuci_{j}=min(xp_{j} + xz*sqrt(xp_{j}*(1-xp_{j})/xn_{j}),1)*100;
Your value xp_{j} is a proportion (with range 0-1), not a percentage (with range 0-100).
Whatever that first picture might be is unreadable as the text is too small.
Respectfully, you do not need to write your own formula for the confidence interval. PROC FREQ can produce many different CIs for the binomial proportion. The one you are using is called the "Wald" confidence interval and is the default CI when you specify the BINOMIAL option in the TABLES statement. PROC FREQ automatically handles capping the CI so that it does not include proportions less than 0 or greater than 1.
For an example, see the article, "Compute and visualize binomial proportions in SAS."
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.