## How to derive subgroup adjusted outcome averages and conduct pairwise stat-test

When you run a statistical interaction model (e.g., Y=TREATMENT + GENDER + TREATMENT*GENDER), you also want to run a mathematically equivalent model:
Y= T_MALE + C_MALE + T_FEMALE + C_FEMALE.

SUBGROUP defines the four groups below.

This is for linear model (HLM because I have the random statement).

proc glimmix data=asdf3 namelen=32;
class CAMPUSID  SUBGROUP;
model y=
x1 x2 x3
SUBGROUP
/solution ddfm=kr dist=normal link=identity s noint;
random int / subject = CAMPUSID;
ods output ModelInfo=x1var1 ParameterEstimates=x2var1 CovParms=x3var1
nobs=x4var1 Diffs=DIF_RESULT1;
run;

This is for logistic regression model.

proc glimmix data=asdf METHOD=RSPL;
class CAMPUS_14 subgroup;
model y=x1 x2 x3 subgroup
ods output  ModelInfo=x1var1 ParameterEstimates=x2var1 CovParms=x3var1
Diffs=DIF_RESULT1 LSMeans=LS1;
run;

## Odds ratio using PROC MEANS and a data step

proc means data=amy stackodsoutput mean min max n;
class A B;
var X ;
ods output summary=kaz_mean;
run;

proc transpose data=kaz_mean out=amyt;
by B;
var Mean;run;

data amyt2;
set amyt;
/*http://en.wikipedia.org/wiki/Odds_ratio
test using the set values. This should return odds ratio of 36
col1=0.9;
col2=0.2;
*/

odds_ratio= ( col2/(1-col2)) /(col1/(1-col1)) ;

run;

## T-test for proportions of multiple groups using SAS procedures and datasteps

/*In this example, there is only two groups, but you can run it with multiple groups.*/

data kaz;set sashelp.class;
if age < 12 then THIS_IS_OUTCOME=0; if age > 13 then THIS_IS_OUTCOME=1;

run;

%let group=sex;
%let outcome=THIS_IS_OUTCOME;
%let dataname=kaz;

ods listing;
ods trace on;

proc means data=&dataname;
where &outcome ne .;
class &group;
var &outcome;
ods output summary=kaz_mean;
run;

proc glimmix data=&dataname;
class &group;
model &outcome=&group ;
lsmeans &group /diff ;
ods output Diffs=kaz_t;
run;

data kaz_t2;
set kaz_t;
keep &group _&group estimate;
run;
proc sort;
by &group;run;

data kaz_mean1;
set kaz_mean;
prop1=&outcome._mean;
n1=&outcome._n;
keep &group prop1 n1;
run;
proc sort;by &group;run;

data kaz_mean2;
set kaz_mean;
prop2=&outcome._mean;
n2=&outcome._n;
_&group=&group;
keep _&group prop2 n2;
run;
proc sort;by _&group;run;

data mix1;
merge kaz_t2 kaz_mean1;
by &group;
run;
proc sort;
by _&group;run;

data mix2;
merge mix1 kaz_mean2;
by _&group;
if estimate ne .;
DEG_FD=N1+N2-2;
/*QC’ed
tValue=2.228;
DEG_FD=10;
*/
tValue=(prop1-prop2)/(SQRT((prop1*(1-prop1)/n1 )+prop2*(1-prop2)/ n2));
/*2 tail test*/
P=(1-probt(abs(tValue),DEG_FD))*2;

length _2TAIL_STAT_TEST \$ 2;

if P < .05 then _2TAIL_TEST = "*"; group1=&group; group2=_&group; classvar="&group"; dif=estimate; outcome="&outcome"; run; data mix3; retain outcome classvar group1 group2 n1 prop1 n2 prop2 dif p _2TAIL_TEST ; set mix2; keep outcome classvar group1 group2 n1 prop1 n2 prop2 dif p _2TAIL_TEST ; ; run;

## Appling t-test for comparison of proportion in a data step:

%let var1=female;

%let var2=male;

%let key=ENROLL_RATE;

Z=(&var1._&key-&var2._&key)/(SQRT((&var1._&key*(1-&var1._&key)/&var1._SAMPLE_N )+&var2._&key*(1-&var2._&key)/ &var2._SAMPLE_N ));

data &schoollevel.&var1;
retain group;
merge ueka1b ueka2b;
by group;

P1=M2_MET_pre_Mean;
P2=M2_MET_post1_Mean;

N1=M2_MET_pre_N;
N2=M2_MET_post1_N;

A=(P1*(1-P1))/N1;
B=(P2*(1-P2))/N2;
STDERR=sqrt(A+B);

Z=abs((P1-P2)/STDERR);
/*two tail 5%*/

P=(1-probnorm(Z))*2;

if P < 0.05 then SIG=”*”;

drop A B P1 P2 N1 N2;
run;