Hello, I've a dataset sorted by patient ID (PID), diagnosis code (dx), doctor specialty code (specialty), and service date (date) in descending order, and doctor ID (docID). I'd like to identify patients seeking more than second opinion of doctor for the same diagnosis and from doctors with the same specialty -- that is, seeing a third doctor of the same specialty -- prior to the the most doctor visit. A patient might go back and forth to different doctors. The data is collected between January 1, 2011 and June 30, 2012. There is no date restriction except that the most recent visit within the same dx and specialty (or first record) should take place April 1, 2012. Here are the data and SAS code to read in the data (a fiscal year variable fyear is calculated (going by April 1 as the start of a fiscal year): data dat; input PiD 1 Date $ 3-10 DocID 12-14 Fee_Code $ 16-19 Specialty 21-22 Dx 24-26 Amount 28-30 Notes $ 32-76; datalines; 1 20120501 101 A585 28 289 60 1st doc w/n same PID, specialty code, dx code 1 20120322 101 A585 28 289 60 1st doc 1 20120313 102 A585 28 289 60 2nd doc 1 20120306 101 A585 28 289 60 1st doc 1 20110830 111 A585 28 300 60 3rd doc w/n same PID, specialty code, dx code 1 20110524 101 A585 28 289 60 1 20110420 101 A585 28 289 60 1 20110411 101 A585 28 289 60 1 20110101 101 A585 28 289 60 1 20120515 104 A401 40 110 100 1st doc w/n same PID, specialty code, dx code 1 20120303 105 A401 40 110 100 2nd doc 1 20120101 104 A401 40 110 100 1st doc 2 20120101 101 A303 15 305 30 1st doc but 1st record date < April 1, 2012 2 20111130 105 A303 15 305 50 2nd doc 2 20111009 108 A303 15 305 30 3rd doc 2 20110707 103 A303 15 305 30 3 20120415 201 A220 30 211 116 1st doc w/n same PID, specialty code, dx code 3 20120401 205 A220 30 211 116 2nd doc 3 20120303 201 A220 30 211 116 1st doc 3 20111231 205 A220 30 211 116 2nd doc ; data dat (rename=(Date2=Date)); format PID Date2 Fyear; set dat; format Date2 yymmdd10.; Date2=input(date,yymmdd10.); fyear = year(intnx("YEAR.4",Date2,0)); drop date; run; (The dataset "dat" is attached as well). Based on the above mentioned criteria, only PID=1 and specialty=28 and DX=289 should be outputted because there is a third visit to DocID=111 on Aug 30, 2011. PID=1, speciatlty=40, DX=110: there are 3 visits but only to 2 different doctors PID=2: there are 4 visits to 3 different doctors but the most recent visit took place in January 2010, which is fiscal year 2011 (< April 1, 2012) PID=3: there are 4 visits but to 2 different doctors So thte data output should look like: PiD Specialty Dx 1 28 289 I assume I should carry docID to the next record to compare and see if they are identical, but I need to compare not just the preceding one but all the preceding ones. I could transpose and compare docID1, docID2, docID3....., but I could have many records due to many visit entries (not manageable, just not preferable if I had other solutions). Thanks in advance for any help you could provide.
... View more