Hello all,
This is my first time posting a question on Statalist, and I really need your help!

I have a data like below in which each 'id' repeats for every 'activity' (e.g. job search and readiness, employment) he or she participated.
The variables 'begin_dt' and 'end_dt' correspond to the dates when each activities began and ended.


Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input float(id begin_dt end_dt activity) str63 type
1 21171 21201 1 "Individual"
1 21564 21608 1 "Individual"
1 21082 21234 2 "Full Time" 
1 21171 21201 2 "Part Time" 
1 21200 21380 2 "Full Time" 
1 21581 21761 2 "Part Time" 
1 21586 21601 2 "Full Time" 
1 21602 21632 2 "Full Time" 
1 21634 21817 2 "Full Time" 
end
format %td begin_dt
format %td end_dt
label values activity componentlabel
label def componentlabel 1 "Job Search and readiness", modify
label def componentlabel 2 "Employment", modify

I need to calculate how many days a person participated in each activity, but I do not know how to account for the overlaps and gaps between the earliest begin date and the last end date.

In this example, the earliest begin date for 'employment' was on 20-sep-2017, and the last date, '25-sep-2019'.
However, he or she had gap of about 7 months between 15-jul-2018 and 01-feb-2019. In addition, some begin and end dates overlap.

For this reason, I can't simply subtract the difference between 20-sep-2017 and 25-sep-2019.
Could you please advise me on what to do?

Thanks in advance!