data step1;

** This program is used to calculate the p-value for testing noninferiority;

***Warning***;

** The pvalue and the asymptotic test procedure should not be used if cell frequencies were small;

** If the OR of drug B versus drug A is our interest;

** n1 represents the total number patients with discordant response in group I receiving A-B sequence;

** x1 represents the number of patients among n1 with a positive in period 2 and a negative in period 1;

** n2 represents the total number of patients with discordant response in group II receiving B-A sequence;

** x2 represents the number of patients among n2 with a positive in period 2 and a negative in period 1;

** If the OR of drug A versus drug B is our interest;

** n1 represents the total number patients with discordant response in group II receiving B-A sequence;

** x1 represents the number of patients among n1 with a positive in period 2 and a negative in period 1;

** n2 represents the total number of patients with discordant response in group I receiving A-B sequence;

** x2 represents the number of patients among n2 with a positive in period 2 and a negative in period 1;

alpha=0.05;

lowlimit=0.80; ** the non-inferior limit;

n1=48;

x1=32;

n2=56;

x2=15;

xt=x1+x2;

a=max(xt-n2, 0);

b=min(xt, n1);

if (n1-x1) gt 0 and x1 gt 0 and (n2-x2) gt 0 and x2 gt 0 then do;

mle=sqrt((x1*(n2-x2))/((n1-x1)*x2));

put mle 1-10 3 @20 'MLE estimator for the OR';

if mle gt 0 then z=(log(mle)-log(lowlimit))/(sqrt(1/x1+1/(n1-x1)+1/x2+1/(n2-x2))/2);

pz=1-probnorm(z);

put 'z-value' z 11-15 2 @20 'p-value=' pz 30-37 3 @40 'test without adding 0.5 to each cell for sparse data';

end;

adjmle=sqrt((x1+0.5)*(n2-x2+0.5)/((n1-x1+0.5)*(x2+0.5))); ** adding 0.5 before calculating the MLE;

put adjmle 1-10 3 @20 'MLE for the OR with an adjustment of adding 0.5 for sparse data';

adjz=(log(adjmle)-log(lowlimit))/(sqrt(1/(x1+0.5)+1/(n1-x1+0.5)+1/(x2+0.5)+1/(n2-x2+0.5))/2);

adjpz=1-probnorm(adjz);

put 'z-value' adjz 11-15 2 @20 'p-value=' adjpz 30-37 3 @40 'test with adding 0.5 to each cell for sparse data';

snum=0;

sden=0;

p=lowlimit**2;

do x=a to b;

prob=exp(lgamma(n1+1)-lgamma(x+1)-lgamma(n1-x+1))*

exp(lgamma(n2+1)-lgamma(xt-x+1)-lgamma(n2-xt+x+1))*p**x;

if x ge x1 then snum+prob;

sden+prob;

end; ** end of do x=1 to b;

pvalue=snum/sden;

put 'exact p-value for testing non-inferiority' pvalue 45-55 5;