Thanks!
For anyone interested, I've figured it out.
The commands I used:
bysort pt_id result_day: gen visit1 = (_n==1)
bysort pt_id (result_day): replace visit1 = sum(visit1)
It worked out well, and below is the output.
+------------------------------------------+
| pt_id encntr_id result~y visit1 |
|------------------------------------------|
1. | 275661 275661_0559 1567 1 |
2. | 275661 275661_0559 1567 1 |
3. | 275661 275661_0571 1792 2 |
4. | 275661 275661_0571 1792 2 |
5. | 552313 552313_0184 798 1 |
|------------------------------------------|
6. | 552313 552313_0184 798 1 |
7. | 702337 702337_0120 2768 1 |
8. | 702337 702337_0120 2768 1 |
9. | 702337 702337_0120 2768 1 |
10. | 733170 733170_0034 -234 1 |
|------------------------------------------|
11. | 733170 733170_0034 -234 1 |
12. | 733170 733170_0249 846 2 |
13. | 733170 733170_0249 846 2 |
14. | 787800 787800_0178 1215 1 |
15. | 787800 787800_0178 1215 1