Hi Reeza I spoke too soon. With a larger database it appears that all subsequent readmits are linked to visit #1 whereas the readmit difference is still connected to the various visits. I added a visit for chartno 111111 below and you'll note that even though the 3rd visit is connected to the 2nd visit, it shows the account number of the first visit, which I don't want. I want the 3rd visit to have the account number of the 2nd visit because that is the one that is within 30 days of it. Thanks. data test_grp; input @1 chartno $6. @7 acctno $4. @11 admdate yymmdd8. @19 sepdate yymmdd8. @27 cmg $3.; format admdate yymmdd10. sepdate yymmdd10.; cards; 111111A0012012032920120331001 222222A0022012050120120515003 333333A0032012060120120607321 111111A0042012040220120410320 444444A0052012060320120615109 555555A0062012053120120603149 222222A0072012070120120713001 222222A0082012072020120725026 444444A0092012081320120817027 333333A0102012061520120621028 111111A0112012110120121130004 111111A0122012050320120517001 run; proc sort data=test_grp; by chartno admdate;run; Data readmits; set test_grp; by chartno admdate; length index_acctno $4.; length index_cmg $3.; dif=ifn(first.chartno,.,admdate-lag(sepdate)); if first.chartno then index_acctno=""; else index_acctno=lag(acctno); if first.chartno then index_cmg=""; else index_cmg=lag(cmg); format admdate ddmmyy10. sepdate ddmmyy10.; run; Data readmits; set test_grp; by chartno; dif=ifn(first.chartno,.,admdate-lag(sepdate)); format admdate ddmmyy10. sepdate ddmmyy10.; *create an episode; if first.chartno then episode=1; if dif > 30 then episode+1; *count visits within episodes; if first.chartno the visit=1; else if dif <=30 then visit+1; else visit=1; *flag a readmit; if dif<=30 and dif ne . then readmit=1; run; *merge in fields, you can add the other fields in the same manner as acctno; proc sql; create table readmits_with_index as select r.*, i.acctno as index_visit from readmits as r left join readmits as i on r.chartno=i.chartno and r.episode=i.episode and i.visit=1 and r.readmit=1 order by chartno, episode, visit; quit;
... View more