proc sql;
create table adef1 as
select a.*,b.AVAL as AVAL1
from adef(where=(GROUPN=2)) a
left join adef(where=(GROUPN=1)) b
on a.USUBJID=b.USUBJID and a.TRTPN=b.TRTPN and a.AVISITN=b.AVISITN
order by TRTPN,AVISITN
;
quit;
data adef2;
set adef1;
if AVAL=1 and AVAL1=1 then type=1;
else if AVAL=1 and AVAL1=2 then type=2;
else if AVAL=2 and AVAL1=1 then type=3;
else if AVAL=2 and AVAL1=2 then type=4;
run;
data table1;
do TRTPN=1 to 5;
do type=1 to 4;
AVAL=choosen(type,1,1,2,2);
AVAL1=choosen(type,1,2,1,2);
output;
end;
end;
run;
proc sql noprint;
create table ds_ana1 as
select a.*,coalesce(b.COUNT,0) as COUNT
from table1 a
left join (select count(USUBJID) as COUNT,TRTPN,type from adef2 Group by TRTPN,type) b on a.TRTPN=b.TRTPN and a.type=b.type
;
quit;
ods graphics off;
proc freq data=ds_ana1;
by TRTPN;
tables AVAL*AVAL1 / nopercent norow nocol agree chisq warn=(nolog output);
weight COUNT/zeros;
exact McNem;
ods output McNemarsTest=McNemarsTest;output out=fp1 chisq fisher;
run;
data final;
set McNemarsTest;
format _all_;
__StatValue=ChiSquare;
__PValue=Prob;
run;