Announcement

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

  • Creating "previous history" variable based on a current year observation - multiple observations per person

    Hello,
    I have a dataset with multiple observations per person. There can be multiple incidences in a given year for up to 5 years. I want to create a binary variable that will update in a current year based on a previous year (different row) by unique identifier. The data is in long format. So far I've been able create dummy variables within each year for the injury of interest and a cumulative variable across all years but these don't address my issue.

    Essentially I need a binary medical history variable.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float newid int school_year_key float(concussion17 concussion18 concussion19 concussion20 any_concussion)
     4 2018 0 0 0 0 0
     5 2018 0 0 0 0 0
     6 2019 0 0 0 0 0
     6 2020 0 0 0 0 0
     7 2019 0 0 0 0 0
     8 2018 0 1 0 0 2
     8 2017 0 0 0 0 2
     8 2019 0 0 1 0 2
     9 2017 0 0 0 0 1
     9 2020 0 0 0 0 1
     9 2019 0 0 0 0 1
     9 2017 0 0 0 0 1
     9 2018 0 0 0 0 1
     9 2019 0 0 0 0 1
     9 2017 1 0 0 0 1
     9 2020 0 0 0 0 1
    10 2018 0 0 0 0 0
    10 2017 0 0 0 0 0
    10 2018 0 0 0 0 0
    10 2019 0 0 0 0 0
    end

  • #2
    Your question is unclear to me.

    that will update in a current year based on a previous year (different row) by unique identifier.
    Update in what way? You should manually create the variable for a few identifiers to illustrate what you want.

    Comment


    • #3
      When I said current year I meant row/observation.

      If we look at newid #8 for year 2019, we see that in year 2018 they had a concussion. I want to create a binary variable that indicates such for row/year 2019 prev_concussion18 = 1

      I tried:
      bysort newid: gen prev_concussion17 = any_concussion - (concussion18 + concussion19 + concussion20)

      The above code fails and simply recreates any_concussion because it is not recognizing the differing rows despite sharing the newid variable.

      Comment


      • #4
        Try this:

        Code:
        bysort newid (school_year_key) : gen prev_concussion=any_concussion[_n-1]==1

        hth,
        Jeph

        Comment


        • #5
          With the multiple observations per person per year, I would do it this way:

          Code:
          input float newid int school_year_key float(concussion17 concussion18 concussion19 concussion20 any_concussion)
           4 2018 0 0 0 0 0
           5 2018 0 0 0 0 0
           6 2019 0 0 0 0 0
           6 2020 0 0 0 0 0
           7 2019 0 0 0 0 0
           8 2018 0 1 0 0 2
           8 2017 0 0 0 0 2
           8 2019 0 0 1 0 2
           9 2017 0 0 0 0 1
           9 2020 0 0 0 0 1
           9 2019 0 0 0 0 1
           9 2017 0 0 0 0 1
           9 2018 0 0 0 0 1
           9 2019 0 0 0 0 1
           9 2017 1 0 0 0 1
           9 2020 0 0 0 0 1
          10 2018 0 0 0 0 0
          10 2017 0 0 0 0 0
          10 2018 0 0 0 0 0
          10 2019 0 0 0 0 0
          end
          
          egen concussion= rowtotal(concussion*)
          frame put newid school_year_key concussion, into(concussions)
          frame concussions{
              collapse (max) concussion, by(newid school_year_key)
              xtset newid school_year_key
              gen wanted=l.concussion
          }
          frlink m:1 newid school_year_key, frame(concussions)
          frget wanted, from(concussions)
          frame drop concussions
          Res.:

          Code:
          . sort newid school_year_key
          
          . l newid school_year_key concussion wanted, sepby(newid school_year)
          
               +--------------------------------------+
               | newid   school~y   concus~n   wanted |
               |--------------------------------------|
            1. |     4       2018          0        . |
               |--------------------------------------|
            2. |     5       2018          0        . |
               |--------------------------------------|
            3. |     6       2019          0        . |
               |--------------------------------------|
            4. |     6       2020          0        0 |
               |--------------------------------------|
            5. |     7       2019          0        . |
               |--------------------------------------|
            6. |     8       2017          0        . |
               |--------------------------------------|
            7. |     8       2018          1        0 |
               |--------------------------------------|
            8. |     8       2019          1        1 |
               |--------------------------------------|
            9. |     9       2017          1        . |
           10. |     9       2017          0        . |
           11. |     9       2017          0        . |
               |--------------------------------------|
           12. |     9       2018          0        1 |
               |--------------------------------------|
           13. |     9       2019          0        0 |
           14. |     9       2019          0        0 |
               |--------------------------------------|
           15. |     9       2020          0        0 |
           16. |     9       2020          0        0 |
               |--------------------------------------|
           17. |    10       2017          0        . |
               |--------------------------------------|
           18. |    10       2018          0        0 |
           19. |    10       2018          0        0 |
               |--------------------------------------|
           20. |    10       2019          0        0 |
               +--------------------------------------+
          
          .

          Comment

          Working...
          X