Announcement

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

  • panel data identify last visit

    Hi all,

    I am working on a database where each subject is identified by -id- and each visit by -visit-. The data are long. There are several visits for each id.

    I am struggling finding a code to specifically identify the last visit for each subject. Of course, they don't have the same numbers of visits. I think very easy but ii can't find how to do that.

    Thank you very much in advance,

    CLY

  • #2
    Without an example of your data it is difficult to give a precise answer. Assuming that the value of visit for a given id is numbered in an increasing order by time of visit, so the last visit for the id is the observation with the largest value of visit for that id, then
    Code:
    bysort id (visit): egen lastvisit = max(visit)
    will create the variable lastvisit with the largest value of visit for each id.

    Comment


    • #3
      Hi William,

      Thanks so much for your reply and sorry I haven't been clear enough.

      My data are actually like this:

      id visit status
      1 1 0
      1 2 1
      1 3 1
      1 4 2
      2 1 0
      2 1 1
      2 1 2
      2 1 0


      I would like to creat a variable that takes 1 for the last visit of each subject (they don't have the same number of visits) because I am interested in identifying a specific status at the last visit of each patient.

      Thank you so so much for your help,

      CLY

      Comment


      • #4
        I don't understand the role of status. Does it matter?

        Ignoring it for now, here is code that builds on the code I suggested in #2 and applies it to your example data.
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input byte(id visit status)
        1 1 0
        1 2 1
        1 3 1
        1 4 2
        2 1 0
        2 1 1
        2 1 2
        2 1 0
        end
        
        // create sequence number to return it to the original order
        generate int seq = _n
        // create variable with last visit number
        bysort id (visit): egen lastvisit = max(visit)
        // create variable with 1 for observations of last visit
        generate last = visit==lastvisit
        // return data to original order
        sort seq
        drop seq
        // how did we do?
        list, noobs sepby(id) abbreviate(12)
        Code:
        . list, noobs sepby(id) abbreviate(12)
        
          +----------------------------------------+
          | id   visit   status   lastvisit   last |
          |----------------------------------------|
          |  1       1        0           4      0 |
          |  1       2        1           4      0 |
          |  1       3        1           4      0 |
          |  1       4        2           4      1 |
          |----------------------------------------|
          |  2       1        0           1      1 |
          |  2       1        1           1      1 |
          |  2       1        2           1      1 |
          |  2       1        0           1      1 |
          +----------------------------------------+

        Comment


        • #5
          Hi William,

          Sorry for the late reply!
          Thank you so much, I do appreciate your help!

          Best,
          CLY

          Comment


          • #6
            I think a compact alternative would be:
            Code:
             bysort id (visit): last = (_n==_N)
            for one of so many must-read from Nick Cox, , see:
            https://journals.sagepub.com/doi/pdf...867X0200200106

            you may need to include some provision for missings, since they are 'highest' values.

            pay attention to stata journal
            https://www.stata.com/bookstore/stata-journal/

            :
            Last edited by George Hoffman; 18 Apr 2020, 06:59.

            Comment

            Working...
            X