Hello!

I have a long dataset with dates corresponding to visits (variable name "date"). Each person (variable name "ID") can have more than one visit. I am hoping to create a new variable that corresponds to the nth visit. For instance, if ID number 1 has a visit on January 1, 2020, another on February 2, 2020 and another on March 3, 2020 these would be coded in the new variable as visit number 1, 2, and 3 respectively.

I am sure there is a simple way to do this using egen but have not been able to figure it out so far.

Any help is much appreciated. I have included a dataex example below.

Thank you,

Sarah


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float studyid str9 date
 3 "09-MAR-10"
 3 "03-MAR-09"
 6 "13-JAN-09"
 6 "12-JAN-10"
 6 "05-OCT-10"
 6 "27-JUL-10"
 6 "09-JUN-09"
 6 "06-DEC-13"
 6 "11-OCT-11"
 6 "12-JUL-11"
 6 "27-APR-10"
 6 "22-SEP-09"
 6 "21-AUG-12"
 6 "09-AUG-13"
 6 "02-AUG-12"
 6 "27-NOV-12"
 6 "10-JAN-12"
 6 "14-APR-09"
 6 "01-FEB-11"
 6 "15-MAY-12"
 6 "19-MAR-13"
 6 "06-SEP-13"
 6 "15-MAR-11"
 6 "23-AUG-12"
 6 "10-DEC-13"
 6 "13-OCT-09"
 6 "30-JUN-09"
 7 "24-JUL-12"
 7 "08-JUL-11"
 7 "08-JAN-10"
 7 "09-MAR-12"
 7 "10-SEP-10"
 7 "20-NOV-12"
 7 "07-MAY-10"
 7 "21-JUL-09"
 7 "04-NOV-11"
 7 "14-JAN-11"
 7 "19-MAR-13"
 7 "26-MAR-13"
 7 "11-JAN-11"
 7 "20-MAR-09"
 8 "15-SEP-11"
 8 "09-SEP-10"
 8 "24-APR-12"
 8 "21-AUG-12"
 8 "15-JUN-10"
 8 "07-JUL-11"
 8 "07-JAN-11"
 8 "23-SEP-11"
 8 "08-APR-11"
 8 "08-NOV-12"
 8 "24-JAN-12"
 9 "20-JAN-09"
15 "18-MAR-10"
15 "26-MAY-11"
15 "28-JUL-09"
15 "27-JAN-09"
17 "10-APR-12"
17 "11-OCT-11"
17 "05-JUL-11"
17 "13-JAN-11"
17 "08-JUN-10"
17 "02-MAR-10"
17 "11-DEC-12"
17 "01-AUG-13"
17 "09-DEC-10"
17 "16-OCT-12"
17 "10-NOV-09"
17 "14-APR-11"
17 "13-AUG-13"
17 "06-OCT-09"
17 "02-SEP-10"
17 "14-MAY-13"
17 "12-FEB-13"
17 "28-APR-11"
17 "09-SEP-10"
18 "06-MAY-10"
18 "02-SEP-10"
19 "26-MAY-11"
19 "31-MAY-12"
19 "31-JAN-12"
19 "29-MAY-12"
19 "23-SEP-11"
20 "20-OCT-09"
20 "03-NOV-09"
20 "09-FEB-10"
20 "16-JUL-09"
21 "09-AUG-13"
21 "10-MAR-09"
21 "06-SEP-13"
21 "15-DEC-09"
21 "17-JAN-12"
21 "22-JUN-10"
21 "18-AUG-09"
21 "13-DEC-11"
21 "23-NOV-10"
21 "15-JUN-10"
21 "03-MAY-13"
21 "04-SEP-12"
21 "25-JAN-13"
end