Ok so first off you want to take only those cases who meet your first criteria which is the first visit was after April 1, 2012. For reference the dataset you put out doesn't have anyone who meets that criteria, but you can run this code to take out all of the other people (I specify in the below code that the date is April 1, 2012. proc sql; create table um2 as select distinct PID, date, docid, specialty, dx, fyear, (max(date)) format=mmddyy10. as last_dt /*You specify what the most recent date was so you can compare to it in the next step*/ from dat group by PID having (min(date)>'01Apr2012'd); /*Here you specify that the first visit for the PID has to have occurred after this date (April 1, 2012) and remove all PID who do not meet this criteria*/ create table um3 as select distinct PID, docid, dx, specialty /*Since you now know what the most recent visit date is, you can specify that the visit that you are counting had to have occurred within a specific time frame, in this case 1 year or 365 days. I wrote it that the most recent date is first so you have to specify datdif to be negative, but you could reverse it. Also I included 0, so the last visit is counted*/ from um2 where datdif(last_dt, date, 'act/act') between -365 and 0; create table um4 as /*since you have eliminated all of the visits which didn't occur within 1 year you now can reduce your visits based on dx and specialty, and count the number of DocID's that are in the table for each PID*/ select distinct PID, dx, specialty, (count(docid)) as count from um3 group by pid, dx, specialty; create table cohort as select distinct pid, dx, specialty from um4 where count >=3; /*This line could have been specified in um4 as having count >=3 or it can be written as another line. This specification will give you only those PID who had at least 3 visits to different docids in the year preceding the most recent visit for the same dx and in the same specialty*/ quit; This code should create the cohort that you have so far specified.
... View more