Hi,

I have a dataset with two different procedures (proc1 & proc 2). Some id's have multiple procedures performed on either the same date or different dates. I am trying to identify two things: 1)those patients who had proc1 and then switched to proc2 (performed on a different date); 2) the number of times an id received proc1 before moving to proc2.

I created the variable seq based on the date of the procedure (bysort PATIENT_ID (date_claim) : gen seq=_n) but cannot figure out how to code this. Examples are id 4, 29, and 46.

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float id str10 claimdt double(proc1 proc2) float seq
 1 "2004-07-21" 1 0  1
 1 "2004-07-30" 1 1  2
 4 "2009-12-21" 1 0  1
 4 "2010-04-06" 1 0  2
 4 "2011-04-09" 0 1  3
11 "2009-02-09" 1 0  1
11 "2009-02-13" 1 0  2
11 "2009-04-14" 1 0  3
12 "2010-10-06" 1 0  1
12 "2011-10-14" 1 0  2
15 "2003-10-24" 1 1  1
22 "2010-10-06" 1 0  1
22 "2010-11-12" 1 0  2
22 "2011-01-11" 1 0  3
23 "2010-10-25" 1 0  1
23 "2010-12-03" 1 0  2
23 "2010-12-26" 1 0  3
23 "2011-02-18" 1 0  4
29 "2005-03-04" 1 0  1
29 "2005-03-24" 0 1  2
32 "2003-06-09" 1 0  1
32 "2003-06-10" 1 0  2
35 "2004-01-26" 1 0  1
35 "2004-04-01" 1 0  2
35 "2004-07-14" 1 0  3
35 "2004-11-19" 1 0  4
36 "2005-02-18" 1 0  1
36 "2005-03-04" 1 0  2
36 "2005-04-20" 1 1  3
36 "2005-06-06" 1 0  4
36 "2005-06-07" 0 1  5
38 "2003-01-27" 1 0  1
38 "2003-12-04" 0 1  2
38 "2003-12-12" 1 0  3
38 "2004-01-05" 0 1  4
39 "2005-06-07" 1 0  1
39 "2005-07-28" 1 0  2
40 "2010-09-16" 1 0  1
40 "2010-09-17" 0 1  2
40 "2010-09-23" 1 0  3
42 "2004-11-05" 1 0  1
42 "2004-11-07" 0 1  2
42 "2004-11-18" 0 1  3
42 "2004-11-29" 0 1  4
42 "2004-12-02" 0 1  5
42 "2004-12-06" 0 1  6
42 "2005-02-25" 0 1  7
46 "2003-04-29" 1 0  1
46 "2003-05-30" 0 1  2
48 "2004-06-30" 1 0  1
48 "2004-07-01" 0 1  2
48 "2004-09-13" 0 1  3
48 "2004-09-16" 0 1  4
50 "2010-03-08" 1 1  1
50 "2010-04-23" 0 1  2
52 "2005-05-13" 1 0  1
52 "2005-08-05" 1 0  2
53 "2009-01-22" 1 0  1
53 "2009-01-23" 1 1  2
53 "2009-05-04" 0 1  3
53 "2009-05-05" 0 1  4
53 "2009-05-07" 0 1  5
57 "2005-12-30" 1 0  1
57 "2006-01-19" 1 0  2
58 "2010-08-05" 1 0  1
58 "2011-08-01" 1 0  2
60 "2010-08-20" 1 1  1
60 "2010-09-21" 0 1  2
60 "2010-12-06" 0 1  3
60 "2010-12-17" 0 1  4
60 "2011-01-11" 0 1  5
60 "2011-04-01" 0 1  6
60 "2011-07-26" 0 1  7
60 "2011-09-09" 0 1  8
60 "2011-11-17" 0 1  9
60 "2012-02-27" 0 1 10
60 "2012-04-05" 0 1 11
61 "2005-03-07" 1 0  1
61 "2005-05-27" 1 0  2
62 "2010-09-02" 1 0  1
62 "2010-09-09" 0 1  2
62 "2010-09-23" 0 1  3
62 "2010-10-27" 0 1  4
62 "2010-10-29" 0 1  5
62 "2010-11-11" 0 1  6
62 "2010-11-26" 0 1  7
62 "2010-12-15" 0 1  8
63 "2004-05-10" 1 0  1
63 "2004-08-06" 1 0  2
63 "2004-09-23" 1 0  3
63 "2005-05-17" 1 0  4
67 "2004-07-21" 1 0  1
67 "2005-07-15" 1 0  2
67 "2005-08-25" 1 0  3
67 "2006-06-01" 1 0  4
68 "2008-10-30" 0 1  1
68 "2008-12-02" 0 1  2
71 "2003-01-06" 1 0  1
71 "2003-01-07" 0 1  2
71 "2003-01-15" 0 1  3
end
Thank you in advance for your help.