Announcement

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

  • In longitudinal data, coding with binary variable of 0/1 whenever person ID changes (but keeping 0 for within person ID)

    I'm using longitudinal data, that includes up to four year-month (YEAR_MONTH) observations for each person (PERSON_ID). So each PERSON_ID has up to four records. Assuming the data are sorted by PERSON_ID and YEAR_MONTH, I want to generate a binary variable that flags with a "1" whenever PERSON_ID changes. The variable would take a value of "0" whenever current PERSON_ID and the prior record's PERSON_ID are the same. So I would like data that looks something like the following. Would appreciate any help with this. I'm sure it's more straightforward than I realize. Thank you very much.
    PERSON_ID YEAR_MONTH NEW_PERSON
    1 2020_1 1
    1 2020_2 0
    1 2020_3 0
    1 2020_4 0
    2 2020_1 1
    2 2020_2 0
    2 2020_3 0
    2 2020_4 0
    3 2020_1 1
    3 2020_2 0
    3 2020_3 0
    3 2020_4 0
    4 2020_1 1
    5 2020_1 1
    6 2020_1 1
    7 2020_1 1
    8 2020_1 1

  • #2
    Seems like you just want to tag the first observation of each person, in which case, you can

    Code:
    bys PERSON_ID (YEAR_MONTH): g wanted=_n==1

    Comment


    • #3
      Thanks very much. That seems to work. But now I'm having trouble with my grouping function (how I generate PERSON_ID). I've concatenated a couple ID variables, and I'm unsure if the resulting string variable is causing Stata to be unable to distinguish between unique IDs. Does the "group" function work on string variables?

      Comment


      • #4
        I assume you mean the group function to the -egen- command; here is what it says in the help file: "varlist may contain numeric variables, string variables, or a combination of the two."

        Comment


        • #5
          I'm all set now, thank you, Rich

          Comment

          Working...
          X