I have multiple types of patient-reported data collected across a variable number of patient encounters. The dates are included in this data but there is no variable linking the data collected on the same day. I would like to count and label the number of visits that patients attended, using a combination of patient study ID and date. The "visit" column is the variable I am seeking to create.
I have tried this using the egen group command but this just counts the number of distinct patient ID+date combinations across the entire dataset, rather than by each patient. I have similar results using the _n or _N functions.
| Patient ID | Date | Questionnaire | Visit |
| 1001 | 1/1/2021 | A | 1 |
| 1001 | 1/1/2021 | B | 1 |
| 1001 | 1/4/2021 | B | 2 |
| 1002 | 2/3/2021 | A | 1 |
| 1002 | 2/3/2021 | B | 1 |
| 1003 | 3/5/2021 | A | 1 |
| 1003 | 3/7/2021 | A | 2 |

Comment