Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Generate visit number in a longitudinal dataset

    I have a longitudinal dataset and would like to generate a visit number for each patient. For example patient ID#1 could have visit 1, 2, 3; patient ID#2 could have visit 1, 2,3, 4.

    Is there a way to do this? I show the patient id variable (ptid) and date variables below:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str3 ptid float(datebirth dateadmit)
    "49"  13599 20702
    "114" 13853 20760
    "150" 13853 20789
    "114" 13853 20758
    "150" 13853 20784
    ""    13853 20770
    ""    13853 20773
    "114" 13853 20760
    "150" 13853 20795
    ""    13853 20771
    end
    format %td datebirth
    format %td dateadmit

    Thank you,
    Al Bothwell

  • #2
    Code:
    bys ptid (dateadmit): gen visit_no=_n if !missing(ptid)
    You appear to have duplicates, at least in your data example.

    Comment


    • #3
      Building on Andrew's observation of duplicate admission dates, this example eliminates double-counting.
      Code:
      bysort ptid dateadmit: generate admit = _n==1 if !missing(ptid)
      by ptid (dateadmit): generate visit = sum(admit) if !missing(ptid)
      list, noobs sepby(ptid)
      Code:
      . list, noobs sepby(ptid)
      
        +----------------------------------------------+
        | ptid   datebirth   dateadmit   admit   visit |
        |----------------------------------------------|
        |        05dec1997   12nov2016       .       . |
        |        05dec1997   13nov2016       .       . |
        |        05dec1997   15nov2016       .       . |
        |----------------------------------------------|
        |  114   05dec1997   31oct2016       1       1 |
        |  114   05dec1997   02nov2016       1       2 |
        |  114   05dec1997   02nov2016       0       2 |
        |----------------------------------------------|
        |  150   05dec1997   26nov2016       1       1 |
        |  150   05dec1997   01dec2016       1       2 |
        |  150   05dec1997   07dec2016       1       3 |
        |----------------------------------------------|
        |   49   26mar1997   05sep2016       1       1 |
        +----------------------------------------------+

      Comment


      • #4
        Thank you this William and Andrew. I used William's code and it worked perfectly.

        Comment

        Working...
        X