/* This program was supplied by a SAS customer as an example of something */
/* that is DIFFICULT to read.  Do you agree with that assessment? */
/* See the reformatted version to compare. */

RSUBMITlibname project '/project'libname mysas '/project/smith'
data totsubset; 
set project.total_consolidated(keep=vaers_id onstdays symc1-symc20 vax1-vax7 age_yrs state recvdate);

data shutoff; length vaers_id $8; vaers_id='999999-9';
data vax_totals (keep=vax) symptom_totals (keep=c1) two_symptom_combos (keep=c1 c2)
three_symptom_combos (
keep=c1-c3) four_symptom_combos (keep=c1-c4) 
vax_symptom_combos (
keep=vax c1) vax_two_symptom_combos (keep=vax c1 c2) 
vax_three_symptom_combos (
keep=vax c1-c3) vax_four_symptom_combos (keep=vax c1-c4);
merge totsubset shutoff (in=thats_all_folks); by vaers_id;

array syms $21 symc1-symc20; length c1-c4 symhold onset_symp $21 stage $7 vax $6;
array vaxs $6 vax1-vax7; length pair $30retain totrep 0;
if thats_all_folks=1 then docall symput('total', totrep); stopend; totrep=totrep+1

onset=onstdays; 
if onset ne . then doif onset<2 then onset_symp='EARLY ONSET'; count=1;
do while (count<21); if syms(count)=' ' then do; syms(count)=onset_symp; count=20end; count=count+1endend;combo=0;
do j=1 to 20if syms(j)=' '  then syms(j)='ZZZZZZ'else do;combo=combo+1; syms(j)=trim(left(syms(j))); endend;
do j=1 to 19do k=j+1 to 20if syms(k)<syms(j) then do;symhold=syms(k); syms(k)=syms(j); syms(j)=symhold; endend;end;first=1;

do m=1 to 7if vaxs(m) ne ' ' then do; vax=vaxs(m); output vax_totals;
do i=1 to combo; c1=syms(i); if first=1 then output symptom_totals; output vax_symptom_combos;
do j=i+1 to combo; c2=syms(j); if first=1 then output two_symptom_combos; output vax_two_symptom_combos;
do k=j+1 to combo; c3=syms(k); if first=1 then output three_symptom_combos; output vax_three_symptom_combos;
do l=k+1 to combo; c4=syms(l); if first=1 then output four_symptom_combos; output vax_four_symptom_combos;
endendendend; first=2endendproc sort data=vax_four_symptom_combos; by c1 c2 c3 c4 vax;
data mysas.vax_four_symptom_combos; set vax_four_symptom_combos; by c1 c2 c3 c4 vax; retain sum;
if first.c1=1 or first.c2=1 or first.c3=1 or first.c4=1 or first.vax=1 then sum=0; sum=sum+1
if last.c1=1 or last.c2=1 or last.c3=1 or last.c4=1 or last.vax=1 then doif sum>4 then outputend;
proc sort data=vax_three_symptom_combos; by c1 c2 c3 vax;
data mysas.vax_three_symptom_combos; set vax_three_symptom_combos; by c1 c2 c3 vax; retain sum;
if first.c1=1 or first.c2=1 or first.c3=1  or first.vax=1 then sum=0; sum=sum+1
if last.c1=1 or last.c2=1 or last.c3=1  or last.vax=1 then doif sum>4 then outputend;
proc sort data=vax_two_symptom_combos; by c1 c2  vax;
data mysas.vax_two_symptom_combos; set vax_two_symptom_combos; by c1 c2 vax; retain sum;
if first.c1=1 or first.c2=1  or first.vax=1 then sum=0; sum=sum+1
if last.c1=1 or last.c2=1  or last.vax=1 then doif sum>4 then outputend;
proc sort data=vax_symptom_combos; by c1 vax;
data mysas.vax_symptom_combos; set vax_symptom_combos; by c1 vax; retain sum;if first.c1=1  or first.vax=1 then sum=0; sum=sum+1;
if last.c1=1  or last.vax=1 then doif sum>4 then outputend;proc sort data=vax_totals; by vax;
data mysas.vax_totals; set vax_totals; by vax; retain vsum;
if first.vax=1 then vsum=0; vsum=vsum+1if last.vax=1 then doif vsum>4 then outputend;
proc sort data=four_symptom_combos; by c1 c2 c3 c4;
data mysas.four_symptom_combos; set four_symptom_combos; by c1 c2 c3 c4; retain csum;
if first.c1=1 or first.c2=1 or first.c3=1 or first.c4=1 then csum=0; csum=csum+1
if last.c1=1 or last.c2=1 or last.c3=1 or last.c4=1 then doif csum>4 then outputend;
proc sort data=three_symptom_combos; by c1 c2 c3;
data mysas.three_symptom_combos; set three_symptom_combos; by c1 c2 c3; retain csum;
if first.c1=1 or first.c2=1 or first.c3=1 then csum=0; csum=csum+1
if last.c1=1 or last.c2=1 or last.c3=1 then doif csum>4 then outputend;
proc sort data=two_symptom_combos; by c1 c2;
data mysas.two_symptom_combos; set two_symptom_combos; by c1 c2; retain csum;if first.c1=1 or first.c2=1 then csum=0; csum=csum+1;
if last.c1=1 or last.c2=1 then doif csum>4 then outputend;proc sort data=symptom_totals; by c1;
data mysas.symptom_totals; set symptom_totals; by c1; retain csum;
if first.c1=1 then csum=0; csum=csum+1if last.c1=1 then doif csum>4 then outputend;  
proc sort data=mysas.vax_four_symptom_combos out=combosort; by vax;
data v_margin; merge mysas.vax_totals combosort; by vax; lift=log(sum/vsum);proc sort data=v_margin; by c1 c2 c3 c4;

*** 4; 
data mysas.four_lift_list; merge v_margin mysas.four_symptom_combos; by c1 c2 c3 c4;
keep vax symptom1-symptom4 loglift logPRR logOR sum a b c d apct cpct acpct; 
symptom1=c1; symptom2=c2; symptom3=c3; symptom4=c4; loglift=lift+log(&total/csum); 
logPRR=log((&total-vsum)*sum/(vsum*(csum-sum)));logOR=log(sum*(&total-vsum-csum+sum)/((csum-sum)*(vsum-sum))); 
a=sum; b=vsum-a; c=csum-a; ab=vsum; ac=a+c; cd=&total-vsum; d=cd-c; apct=a*
100/ab; cpct=c*100/cd; acpct=ac*100/&total; apct=round(apct,0.01); cpct=round(cpct,0.01); acpct=round(acpct,0.01);
if loglift NE . then output;proc sort data=mysas.four_lift_list; by descending loglift;
proc sort data=mysas.vax_three_symptom_combos out=combosort; by vax;
data v_margin; merge mysas.vax_totals combosort; by vax; lift=log(sum/vsum);proc sort data=v_margin; by c1 c2 c3;

*** 3;
data mysas.three_lift_list; merge v_margin mysas.three_symptom_combos; by c1 c2 c3;
keep vax symptom1-symptom4 loglift logPRR logOR sum a b c d apct cpct acpct; 
symptom1=c1; symptom2=c2; symptom3=c3; symptom4=
''; loglift=lift+log(&total/csum); 
logPRR=log((&total-vsum)*sum/(vsum*(csum-sum)));logOR=log(sum*(&total-vsum-csum+sum)/((csum-sum)*(vsum-sum)));
a=sum; b=vsum-a; c=csum-a; ab=vsum; ac=a+c; cd=&total-vsum; d=cd-c; apct=a*100/ab; cpct=c*100/cd; acpct=ac*100/&total; apct=round(apct,0.01); cpct=round(cpct,0.01); acpct=round(acpct,0.01);
if loglift NE . then output;proc sort data=mysas.three_lift_list; by descending loglift;
proc sort data=mysas.vax_two_symptom_combos out=combosort; by vax;
data v_margin; merge mysas.vax_totals combosort; by vax; lift=log(sum/vsum);proc sort data=v_margin; by c1 c2;

*** 2;
data mysas.two_lift_list; merge v_margin mysas.two_symptom_combos; by c1 c2;
keep vax symptom1-symptom4 loglift logPRR logOR sum a b c d apct cpct acpct; 
symptom1=c1; symptom2=c2; symptom3=
''; symptom4=''; loglift=lift+log(&total/csum); 
logPRR=log((&total-vsum)*sum/(vsum*(csum-sum)));logOR=log(sum*(&total-vsum-csum+sum)/((csum-sum)*(vsum-sum)));
a=sum; b=vsum-a; c=csum-a; ab=vsum; ac=a+c; cd=&total-vsum; d=cd-c; apct=a*100/ab; cpct=c*100/cd; acpct=ac*100/&total; apct=round(apct,0.01); cpct=round(cpct,0.01); acpct=round(acpct,0.01);
if loglift NE . then outputproc sort data=mysas.two_lift_list; by descending loglift;
proc sort data=mysas.vax_symptom_combos out=combosort; by vax;
data v_margin; merge mysas.vax_totals combosort; by vax; lift=log(sum/vsum); proc sort data=v_margin; by c1;

*** 1;
data mysas.single_lift_list; merge v_margin mysas.symptom_totals; by c1;
keep vax symptom1-symptom4 loglift logPRR logOR sum a b c d apct cpct acpct;  
symptom1=c1; symptom2=
''; symptom3=''; symptom4=''; loglift=lift+log(&total/csum); 
logPRR=log((&total-vsum)*sum/(vsum*(csum-sum)));logOR=log(sum*(&total-vsum-csum+sum)/((csum-sum)*(vsum-sum))); 
a=sum; b=vsum-a; c=csum-a; ab=vsum; ac=a+c; cd=&total-vsum; d=cd-c; apct=a*
100/ab; cpct=c*100/cd; acpct=ac*100/&total; apct=round(apct,0.01); cpct=round(cpct,0.01); acpct=round(acpct,0.01);
if loglift NE . then outputproc sort data=mysas.single_lift_list; by descending loglift;

data mysas.tmp; merge mysas.four_lift_list mysas.three_lift_list mysas.two_lift_list mysas.single_lift_list; by descending loglift; drop sum; support=sum;
Proc datasets library=mysas; delete four_lift_list three_lift_list two_lift_list single_lift_list four_symptom_combos 
three_symptom_combos two_symptom_combos symptom_totals vax_four_symptom_combos vax_three_symptom_combos vax_two_symptom_combos vax_symptom_combos vax_totals;
run;

RSUBMIT* a acpct apct b c cpct d logOR logPRR loglift support symptom1-symptom4 vax;
proc contents data=mysas.tmp; run;
data t(drop=symptom1-symptom4); set mysas.tmp(keep=a b c d logPRR vax symptom1-symptom4 vax);
PRR=exp(logPRR); PRR=round(PRR,0.1); 
sympc=trim(symptom1)||
' '||trim(symptom2)||' '||trim(symptom3)||' '||trim(symptom4); 
vsymps=trim(vax)||
'=>'||sympc; 

*%let sy="CONJUNCTIVITIS"; *%let sy="EDEMA FACE";
proc print data=t; where vax="FLU" and (prr gt 5 or prr = .and a ge 20;

*where vax="FLU" and (prr1>=2 or prr2>=2 or prr3>=2 or prr4>=2 or prr5>=2)
and (symptom1=&sy or symptom2=&sy or symptom3=&sy or symptom4=&sy);


*where vax="PPV" and (prr>=2) and a ge 15; 
var PRR a vsymps; 
run;