Announcement

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

  • Compare observation first time event occurs with previous observation in panel data

    I need some help (code) finding a solution for the following problem:

    I have a longitudinal dataset with a variable number of visits for each patient (id). I want to compare different outcomes the first time an event occurs (kostvejl == 1) with the patient’s previous visit. How do I generate a variable that identifies (tags) the first observation where the event occurs (ie “after”) AND the previous observation (ie “before”) for each id - no matter if the event occurs again or not?

    Thanks in advance!


    id kostvejl visit
    2 0 1
    2 0 0
    2 0 2
    2 1 3
    5 0 1
    5 0 2
    5 0 3
    5 0 0
    5 1 4
    6 0 0
    6 0 1
    6 1 2
    8 0 3
    8 0 1
    8 0 0
    8 1 2


  • #2
    perhaps,
    Code:
    by id (visit), sort: gen wanted = kostvejl == 1 | kostvejl[_n+1] == 1

    Comment


    • #3
      Thanks for the quick reply. Almost what I wanted :-).

      Is it possible to assign different values to the new variable, eg "before" =1 and "after" = 2?

      Thanks!

      Comment


      • #4
        See the concurrent thread https://www.statalist.org/forums/for...-certain-value including resources linked there.

        Comment

        Working...
        X