I was wondering if anyone had some ideas on how I can approach a problem with my dataset. I have a variable that identifies when medicines were dispensed both the first and last time for the unique medicine and for each drug class. I want to be able to classify each unique medicine as to whether it was discontinued or whether it was switched to another medicine in the same class. I've defined switching and discontinued as:
SWITCHING

Where a medicine will be identified as an initial supply, we will confirm if another medicine at the same ATC4th level (e.g N02AB) has previously been supplied. Where another medicine in the same class has previously been supplied, we will confirm if the final supply date for the original medicine preceded the first supply date for the newly initiated medicine. These medicines were classified as “switched” rather than newly initiated medicines. However, where the original medicine was dispensed at least once after the newly initiated medicine was dispensed, both medicines were considered as concurrent supply rather than switched.
DISCONTINUATION

Medicines were categorised as discontinued if it were the last known supply, and it had not been switched to another medicine within the same class. It was considered as discontinued once a period of 45 days had passed since the last supply date.

RESTARTED
Medicines were categorised as restarted if where they were discontinued, there was at least one calendar year with no supply and then it was supplied again.

I have tried a number of different options to create the three variables to identify medicines that are switched, discontinued and restarted. Time series options are not seeming to work as there seem to be some issues with uniquely identifying data, although I cannot identify where the duplicates are. Does anyone have any suggestions?



The dataset
I have multiple people on who have had medicines dispensed in at least once in at least one year. The data are long by year so there is an observation per participant (identified by pat_id) per unique medicine (identified by ATCcode).

A person who used a medicine regularly and was compliant would have the medicine dispensed once each month. Dispensed_q1-dispensed_q4 are variables the identify if the medicine was dispensed at least once in the first quarter (January, February or March), second quarter (April, May, June), third quarter (July, August, September) or fourth quarter (October, November, December).

The ATC code is a WHO categorisation that identifies medicines. There are five levels in the ATC code from level 1 that identifies body system (skin, antiinfectives) down to level 5 that identifies the unique individual medicine. I have the ATCcode that represents the unique medicine. There is also an atc4th variable that identifies the medicine at the ATC level one, which is the drug class. This is important as sometimes people stop a medicine to use another medicine in the same class instead, or sometimes they may use two medicines in the one class.


input double pat_id int year str7 ATCcode str5 atc4th int(yearsupply_initiated yearsupply_ended yeargroup_initiated yeargroup_ended) byte(dispensed_q1 dispensed_q2 dispensed_q3 dispensed_q4)
17629 2012 "c09ca04" "c09ca" 19097 19279 19097 19279 0 1 1 1
17629 2013 "c09ca04" "c09ca" 19307 19687 19307 19687 1 1 1 1
17629 2014 "c09ca04" "c09ca" 19720 20048 19720 20048 1 1 1 1
17629 2015 "c09ca04" "c09ca" 20080 20444 20080 20444 1 1 1 1
17629 2016 "c09ca04" "c09ca" 20234 20803 20234 20803 1 1 1 1
17629 2017 "c09ca04" "c09ca" 20833 21132 20833 21132 1 1 1 1
17629 2018 "c09ca04" "c09ca" 21166 21523 21166 21523 1 1 1 1
17629 2019 "c09ca04" "c09ca" 21556 21820 21556 21820 1 1 1 0
17629 2020 "c09ca04" "c09ca" 21944 22212 21944 22212 1 1 1 1
17629 2021 "c09ca04" "c09ca" 22270 22576 22270 22576 1 1 1 1
17629 2012 "h03aa01" "h03aa" 19240 19240 19240 19240 0 0 1 0
17629 2013 "h03aa01" "h03aa" 19438 19438 19438 19438 1 0 0 0
17629 2014 "h03aa01" "h03aa" 20045 20045 20045 20045 0 0 0 1
17629 2015 "h03aa01" "h03aa" 20275 20275 20275 20275 0 0 1 0
17629 2016 "h03aa01" "h03aa" 20511 20730 20511 20730 1 0 0 1
17629 2017 "h03aa01" "h03aa" 20928 21120 20928 21120 0 1 0 1
17629 2018 "h03aa01" "h03aa" 21327 21523 21327 21523 0 1 0 1
17629 2020 "h03aa01" "h03aa" 21920 22113 21920 22113 1 0 1 0
17629 2021 "h03aa01" "h03aa" 22305 22484 22305 22484 1 0 1 0
17629 2013 "m01ac06" "m01ac" 19614 19614 19614 19614 0 0 1 0
17629 2016 "m05ba07" "m05ba" 20499 20499 20499 20499 1 0 0 0
26225 2012 "a01ab11" "a01ab" 19283 19283 19283 19283 0 0 0 1
26225 2013 "a01ab11" "a01ab" 19292 19417 19292 19417 1 0 0 1
26225 2014 "a03fa01" "a03fa" 19475 19475 19475 19475 0 1 0 0
26225 2010 "a10ba02" "a10ba" 18280 18547 18280 18547 1 1 1 1
26225 2011 "a10ba02" "a10ba" 18586 18929 18586 18929 1 1 1 1
26225 2012 "a10ba02" "a10ba" 18939 19283 18939 19283 1 1 1 1
26225 2013 "a10ba02" "a10ba" 19292 19489 19292 19489 1 1 0 1
26225 2014 "a10ba02" "a10ba" 19535 19745 19535 19745 1 1 1 1
26225 2010 "a10bb09" "a10bb" 18265 18523 18265 18523 1 1 1 0
26225 2011 "a10bb09" "a10bb" 18576 18929 18576 18929 1 1 1 1
26225 2012 "a10bb09" "a10bb" 18954 19283 18954 19283 1 1 1 1
26225 2013 "a10bb09" "a10bb" 19292 19462 19292 19462 1 1 0 1
26225 2014 "a10bb09" "a10bb" 19493 19751 19493 19751 1 1 1 1
26225 2012 "b01ac04" "b01ac" 19283 19283 18942 19283 0 1 1 1
26225 2013 "b01ac04" "b01ac" 19292 19437 19292 19437 1 0 0 1
26225 2014 "b01ac04" "b01ac" 19466 19745 19466 19745 1 1 1 1
26225 2012 "b01ac06" "b01ac" 18942 19187 18942 19283 0 1 1 1
26225 2014 "c03ca01" "c03ca" 19511 19704 19511 19704 0 1 1 1
26225 2010 "c08ca02" "c08ca" 18280 18547 18280 18547 1 1 1 1
26225 2011 "c08ca02" "c08ca" 18576 18926 18576 18926 1 1 1 1
26225 2012 "c08ca02" "c08ca" 18954 19283 18954 19283 1 1 1 1
26225 2013 "c08ca02" "c08ca" 19292 19489 19292 19489 1 1 0 1
26225 2014 "c08ca02" "c08ca" 19523 19724 19523 19724 1 1 1 1
26225 2010 "c09aa05" "c09aa" 18265 18562 18265 18562 1 1 1 1
26225 2011 "c09aa05" "c09aa" 18586 18912 18586 18912 1 1 1 1
26225 2012 "c09aa05" "c09aa" 18939 19283 18939 19283 1 1 1 1
26225 2013 "c09aa05" "c09aa" 19292 19504 19292 19504 1 1 0 1
26225 2014 "c09aa05" "c09aa" 19531 19733 19531 19733 1 1 1 1
26225 2010 "c10aa05" "c10aa" 18265 18520 18265 18520 1 1 1 0
26225 2011 "c10aa05" "c10aa" 18586 18926 18586 18926 1 1 1 1
26225 2012 "c10aa05" "c10aa" 19052 19283 19052 19283 1 1 1 1
26225 2013 "c10aa05" "c10aa" 19292 19410 19292 19410 1 0 0 1
26225 2014 "c10aa05" "c10aa" 19431 19733 19431 19733 1 1 1 1
26225 2011 "j01cr02" "j01cr" 18612 18612 18612 18612 0 0 0 1
26225 2014 "j01cr02" "j01cr" 19751 19751 19751 19751 1 0 0 0
26225 2013 "j01db01" "j01db" 19308 19395 19308 19395 1 0 0 1
26225 2014 "j01db01" "j01db" 19674 19709 19674 19709 0 0 0 1
26225 2014 "j01xd01" "j01xd" 19751 19751 19751 19751 1 0 0 0
26225 2013 "n02aa05" "n02aa" 19421 19421 19421 19421 1 0 0 0
26225 2013 "n02ae01" "n02ae" 19421 19431 19421 19431 1 0 0 0
26225 2014 "n02ae01" "n02ae" 19450 19753 19450 19753 1 1 1 1
26225 2012 "n05cd07" "n05cd" 18947 18947 18947 18947 0 0 0 1
26225 2013 "n05cd07" "n05cd" 19305 19415 19305 19415 1 0 0 1
26225 2014 "n05cd07" "n05cd" 19437 19745 19437 19745 1 1 1 1
26225 2012 "s01aa01" "s01aa" 18947 19031 18947 19031 1 0 0 1
26225 2012 "s01bb02" "s01bb" 19031 19031 19031 19031 1 0 0 0
end
format %td yearsupply_initiated
format %td yearsupply_ended
format %td yeargroup_initiated
format %td yeargroup_ended
label var pat_id "Patient Id"
label var ATCcode "ATC5_Code"
label var yearsupply_initiated "Last year of supply"
label var yeargroup_initiated "First year of supply"
label var yeargroup_ended "Last year of supply"