Announcement

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

  • How to generate dummy related to first value of individual observations over time

    Hello everybody!
    I have the following dataset (reported in the table below) where an Accident can occur (when it occurs, "Accident"=1 / otherwise=0) for each ID throughout the years. What I would like to do is to generate a dummy variable (DUMMY) which takes the value of the Accident variable occurring in the first year of reference (for each ID), and then it keeps and expands that (binary) value thoughout all the remaining years (for each ID).
    ID Year Accident DUMMY
    1 2001 0 0
    1 2002 1 0
    2 1999 1 1
    2 2000 0 1
    2 2001 1 1
    3 2012 0 0
    3 2013 0 0
    3 2014 1 0

    Here is my (unsuccessful attempt)

    bysort Year: egen DUMMY = rowfirst(Accident)


    Any help would be very much appreciated.
    Thanks.
    Kodi

  • #2
    I have also tried:

    bysort ID(Year): gen DUMMY = Accident[1]

    but I don't know whether it is correct..

    Comment


    • #3
      Kodi, is this what you want?

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte id int year byte accident
      1 2001 0
      1 2002 1
      2 1999 1
      2 2000 0
      2 2001 1
      3 2012 0
      3 2013 0
      3 2014 1
      end
      
      bysort id (year): gen dummy = accident[1]
      Last edited by Fei Wang; 28 Jun 2022, 02:56.

      Comment


      • #4
        Hello Fei! Nice cat
        hum.. I think the sum function doesn't work.

        Comment


        • #5
          Originally posted by Kodi Hannon View Post
          Hello Fei! Nice cat
          hum.. I think the sum function doesn't work.
          I misunderstood your request, and your solution in #2 is correct.

          Comment


          • #6
            Kodi:
            do you mean something along the following lines?
            Code:
            . bysort ID ( Year ): gen wanted=1 if DUMMY==1 & _n==1
            
            . bysort ID ( Year ): replace wanted=wanted[1] if wanted[1]==1
            
            . replace wanted=0 if wanted==. & DUMMY!=.
            
            . list
            
                 +---------------------------------------+
                 | ID   Year   Accident   DUMMY   wanted |
                 |---------------------------------------|
              1. |  1   2001          0       0        0 |
              2. |  1   2002          1       0        0 |
              3. |  2   1999          1       1        1 |
              4. |  2   2000          0       1        1 |
              5. |  2   2001          1       1        1 |
                 |---------------------------------------|
              6. |  3   2012          0       0        0 |
              7. |  3   2013          0       0        0 |
              8. |  3   2014          1       0        0 |
                 +---------------------------------------+
            
            .
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment


            • #7
              Buongiorno Carlo.
              Grazi. Si!
              Kodi

              Comment

              Working...
              X