I have distinct groups (case-control) but the matchedIDs seem to repeat itself. I want each distinct case-control match to have a unique number. To make it simple, I have made up an illustration of my situation and what I want to achieve. My situation is the "matchedID" varilist, and I want to achieve the "uniqueIDs" list.




I tried this command:

by matchedID: gen uniqueIDs= _n

however, the command does NOT go through as the matchedID are not sorted. When I sort matchedID, I get all the different groups with similar observation number (e.g. number 1) together.
How can I solve this issue?