counting number of visits

counting number of visits

by Amanda -
Number of replies: 2

I am currently working with an 8-year dataset and I am having difficulty figuring out how to create a 'count' of the number of visits a patient has had over that span of time
They have several observations (rows) for 1 encounter (variable name: result_day)
I've sorted by pt_id & result_day
 
Now what I would like to do is have a "1" designate the first result_day, "2" the follow-up day, "3" an additional follow-up, and so on
I've named this variable "visit"
 
Here is what I have:

     |  pt_id   result~y   visit |
     |---------------------------|
  1. | 275661       1567       0 |
  2. | 275661       1567       0 |
  3. | 275661       1792       0 |
  4. | 275661       1792       0 |
  5. | 552313        798       0 |
     |---------------------------|
  6. | 552313        798       0 |
  7. | 702337       2768       0 |
  8. | 702337       2768       0 |
  9. | 702337       2768       0 |
 10. | 733170       -234       0 |
     |---------------------------|
 11. | 733170       -234       0 |
 12. | 733170        846       0 |
 13. | 733170        846       0 |
 14. | 787800       1215       0 |
 15. | 787800       1215       0 |
     +---------------------------+
Here is what I want:

     |  pt_id   result~y   visit |
     |---------------------------|
  1. | 275661       1567       1 |
  2. | 275661       1567       1 |
  3. | 275661       1792       2 |
  4. | 275661       1792       2 |
  5. | 552313        798       1 |
     |---------------------------|
  6. | 552313        798       1 |
  7. | 702337       2768       1 |
  8. | 702337       2768       1 |
  9. | 702337       2768       1 |
 10. | 733170       -234       1 |
     |---------------------------|
 11. | 733170       -234       1 |
 12. | 733170        846       2 |
 13. | 733170        846       2 |
 14. | 787800       1215       1 |
 15. | 787800       1215       1 |
     +---------------------------+
 
How can I go about doing so?

Thank you for your help!


In reply to Amanda

Re: counting number of visits

by Mark Pletcher -

Hi Amanda - this is a complex one.  You might look at the "How to use egen" handout posted in Resources this week - you may be able to figure it out from that.  Then would suggest that you talk about it with a TA or me during Lab, or with Jen during a Friday AM session.

In reply to Mark Pletcher

Re: counting number of visits

by Amanda -

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