I'm doing survival analysis and trying to calculate the end of follow-up date for each patient. Follow-up is to be censored when an individual discontinues their initial exposure (defined as no prescription for at least 3 months), switches between exposures or adds in another exposure to their initiating treatment. To switch or add in another exposure is defined as 2 consecutive prescriptions for at least 90 days of an exposure different to the initial treatment.

Quite a complex method to determine end of follow-up/censorship. Is there anyone that could help? A small anonymised sample set below for you to have a go with:

Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input byte ID int(startdate prscdate) byte exposure
1 21531 21531 8
1 21531 21574 8
1 21531 21592 8
1 21531 21634 8
1 21531 21691 8
1 21531 21808 8
1 21531 21840 8
1 21531 21886 8
1 21531 21921 4
1 21531 21931 4
2 16764 16764 6
2 16764 16790 6
2 16764 16820 6
2 16764 16820 3
2 16764 16859 3
3 15077 15077 6
3 15077 15103 6
3 15077 15189 6
3 15077 15245 6
3 15077 15301 6
4 17303 17303 1
4 17303 17322 1
4 17303 17336 1
4 17303 17367 1
4 17303 17422 1
5 15508 15508 6
5 15508 15530 6
5 15508 15547 6
5 15508 15547 2
5 15508 15564 2
end
format %td startdate
format %td prscdate
label values exposure AHT_drugclass
label def AHT_drugclass 1 "ACEI", modify
label def AHT_drugclass 2 "ARB", modify
label def AHT_drugclass 3 "BB", modify
label def AHT_drugclass 4 "CCB", modify
label def AHT_drugclass 6 "Diuretics", modify
label def AHT_drugclass 8 "a1-blocker", modify
Thank you