# delimit ; Use //path/datafile; gen recordord = _n; */record order variable for when I resort data to original order*/; Sort PTVID surgdte; */sort by patient then case surgery date*/; gen caseord = _n, by (PTVID surgdte); */create variable for surgical proc case order*/; gen casenum = _N, by (PTVID surgdte); */create variable for number of cases patient has*/; tabulate anycompl casenum; */ anycompl is the variable case has any complication*/; tabulate anycompl caseord; Save //path/datafile; */save the data with new caseord and casenum variables*/; gen lagpre=.; */patients with single case have no overlap*/; replace lagpre = surgdte[_n]-surgdte[_n-1] if caseord > 1; */only cases after first have pre time*/; gen lagprecat = 1 if lagpre < 5 and caseord>1; replace lagprecat =2 if 5 LE lagpre LT 10; replace lagprecat = 3 if 10 LE lagpre LT 15; replace lagprecat = 4 if 15 LE lagpre LT 30; replace lagprecat = 5 if 30 LE lagpre LT 45; replace lagprecat = 6 if 45 LE lagpre LE 60; replace lagprecat = 7 if lagpre GT 60; gen overlap =0; replace overlap = 1 if lagpre < 61 and caseord>1; */ over view of lag and overlap */; tabulate caseord anycompl overlap, row col; tabulate caseord anycompl lagprecat, row col; gen clust2 = 0; replace clust2 = 1 if overlap == 1 and casenum == 2; */patients with 2 surgical cases, only 2, that overlap*/; gen clust2grp=.; replace clust2grp=0 if anycompl[_n-1]==0 and anycompl[_n]==0 and clust2==1; replace clust2grp=1 if anycompl[_n-1]==1 and anycompl[_n]==0 and clust2==1; replace clust2grp=2 if anycompl[_n-1]==0 and anycompl[_n]==1 and clust2==1; replace clust2grp=3 if anycompl[_n-1]==1 and anycompl[_n]==1 and clust2==1; tabulate clust2 clust2grp, row col; tabulate clust2grp lagprecat, row col; summarize lagpre if clust2==1; summarize lagpre if clust2grp==0; summarize lagpre if clust2grp==1; summarize lagpre if clust2grp==2; summarize lagpre if clust2grp==3; gen clust3 = 0; */anlysis on patients with 3 surgical cases*/; gen lagtot3 = lagpre[_n-1]+lagpre[_n] if caseord==3 and casenum==3; replace lagtot3=lagtot3[_n+2] if caseord==1 and casenum==3; replace lagtot3=lagtot3[_n+1] if caseord==2 and casenum==3; gen overlap3=0 ; replace overlap3=1 if lagtot3 < 121 and casenum==3; gen clust3 = 1 if overlap3==1 and casenum==3; */patients with 3 surgical cases, only 3,that overlap*/; gen clust3grp=.; gen clust3Agrp=0 and casenum==3; gen clust3Bgrp=0 and casenum==3; gen clust3Cgrp=0 and casenum==3; replace clust3Agrp=1 if overlap[_n+1]==1 and overlap[_n+2]==1 and caseord==1; replace clust3Bgrp=1 if overlap[_n+1]==1 and overlap[_n+2]==0 and caseord==1; replace clust3Cgrp=1 if overlap[_n+1]==0 and overlap[_n+2]==1 and caseord==1; replace clust3grp=0 if anycompl==0 and anycompl[_n+1]==0 and anycompl[_n+2]==0 and caseord==1; replace clust3grp=1 if anycompl==0 and anycompl[_n+1]==0 and anycompl[_n+2]==1 and caseord==1; replace clust3grp=2 if anycompl==0 and anycompl[_n+1]==1 and anycompl[_n+2]==0 and caseord==1; replace clust3grp=3 if anycompl==0 and anycompl[_n+1]==1 and anycompl[_n+2]==1 and caseord==1; replace clust3grp=4 if anycompl==1 and anycompl[_n+1]==0 and anycompl[_n+2]==0 and caseord==1; replace clust3grp=5 if anycompl==1 and anycompl[_n+1]==0 and anycompl[_n+2]==1 and caseord==1; replace clust3grp=6 if anycompl==1 and anycompl[_n+1]==1 and anycompl[_n+2]==0 and caseord==1; replace clust3grp=7 if anycompl==1 and anycompl[_n+1]==1 and anycompl[_n+2]==1 and caseord==1; tabulate clust3 clust3grp; tabulate clust3grp clust3Agrp; tabulate clust3grp clust3Bgrp; tabulate clust3grp clust3Cgrp; summarize lagtot3 if clust3==1; summarize lagtot3 if clust3Agrp==1; summarize lagtot3 if clust3Bgrp==1; summarize lagtot3 if clust3Cgrp==1; */ patients with more than 4 are deferred pending results*/; Save //path//new file name//; Collapse (sum) lagpre (count)overlap cases=caseord compls=anycompl, by(Ptvid); tabulate overlap cases compls, row col; summarize lagpre; # delimit cr