Hello,

This post is similar to what I previously posted here: https://www.statalist.org/forums/for...9-sorting-data
The suggestions provided in the link above do not appear to work for every group. Many of the groups do not sort in the correct order. I have made some adjustments in my post below:

I have the following data:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long ID double(group start end) long width
10 1 18080 18747 200
10 1 20702 21335 300
20 1 18748 23035 100
15 1 21336 23035 450
4 2 17148 19196 280
6 2 19263 21943 114
8 2 19199 21943 170
6 2 21924 21943 90
2 2 22267 23035 110
2 2 21944 23035 170
end
format %td start
format %td end
I want to put this data in chronological order based on the start and end dates by group. By group, the start date should be one day after the previous end date. I want the observations that fit this criteria to form a subgroup. If observations in the group do not fit this criteria, then they should form their own individual, separate subgroups. I also want to create every possible combination if there is a start date that can follow several end dates.

I want the final result to look like this:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long ID double(group start end subgroup) long width
10 1 18080 18747 1 200
20 1 18748 23035 1 100
10 1 20702 21335 2 300
15 1 21336 23035 2 450
4 2 17148 19196 1 280
6 2 19263 21943 2 114
2 2 21944 23035 2 170
8 2 19199 21943 3 170
2 2 21944 23035 3 170
6 2 21924 21943 4 90
2 2 21944 23035 4 170
2 2 22267 23035 5 110
end
format %td start
format %td end
I would appreciate any assistance with this!

Best,
Anoush K.