Announcement

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

  • Create a group variable first_treat indicating the first year when each unit becomes treated

    I am trying to create a group variable first_treat indicating the first year when each unit becomes treated, to perform a DID analysis. There is variation in treatment timing, and therefore I need to define a variable "first-treat" reporting for each ID the year when they first became treated (treatment = 0 if not treated, 1 otherwise). In case the units are never treated, the value of first_treat will be zero. I report below a simplified dataframe: I have the variables ID, Year, and Treatment. I need to create the variable first.treat as follows.
    a 2016 0 2017
    a 2017 1 2017
    a 2018 1 2017
    b 2016 1 2016
    b 2017 1 2016
    b 2018 1 2016
    c 2016 0 2018
    c 2017 0 2018
    c 2018 1 2018
    d 2016 0 0
    d 2017 0 0
    d 2018 0 0

    What is the best way to do this in stata? Thankyou in advance.

  • #2
    Jules:
    welcome to this forum.
    You may want to consider something along the following lines (the variable in the rightmost column was not considered as I do not get its role here):

    Code:
    . bysort id (Year): g wanted=sum(Treatment) if Treatment==1
    
    . g first=1 if wanted==1
    
    . drop wanted
    
    . list
    
         +------------------------------+
         | id   Year   Treatm~t   first |
         |------------------------------|
      1. |  a   2016          0       . |
      2. |  a   2017          1       1 |
      3. |  a   2018          1       . |
      4. |  b   2016          1       1 |
      5. |  b   2017          1       . |
         |------------------------------|
      6. |  b   2018          1       . |
      7. |  c   2016          0       . |
      8. |  c   2017          0       . |
      9. |  c   2018          1       1 |
     10. |  d   2016          0       . |
         |------------------------------|
     11. |  d   2017          0       . |
     12. |  d   2018          0       . |
         +------------------------------+
    
    .
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Code:
      clear
      input str1 id year treatment want
      a    2016    0    2017
      a    2017    1    2017
      a    2018    1    2017
      b    2016    1    2016
      b    2017    1    2016
      b    2018    1    2016
      c    2016    0    2018
      c    2017    0    2018
      c    2018    1    2018
      d    2016    0    0
      d    2017    0    0
      d    2018    0    0
      end
      
      bysort id : egen wanted = min(cond(treatment == 1, year, .))
      replace wanted = 0 if wanted == .
      
      list, sepby(id)
      
           +--------------------------------------+
           | id   year   treatm~t   want   wanted |
           |--------------------------------------|
        1. |  a   2016          0   2017     2017 |
        2. |  a   2017          1   2017     2017 |
        3. |  a   2018          1   2017     2017 |
           |--------------------------------------|
        4. |  b   2016          1   2016     2016 |
        5. |  b   2017          1   2016     2016 |
        6. |  b   2018          1   2016     2016 |
           |--------------------------------------|
        7. |  c   2016          0   2018     2018 |
        8. |  c   2017          0   2018     2018 |
        9. |  c   2018          1   2018     2018 |
           |--------------------------------------|
       10. |  d   2016          0      0        0 |
       11. |  d   2017          0      0        0 |
       12. |  d   2018          0      0        0 |
           +--------------------------------------+

      Comment


      • #4
        Thank you very much, this was really helpful!

        Comment


        • #5
          I have an additional question, does anyone have a suggestion on how to perform a DID analysis with multiple treatment periods? Because when I generate a variable: relative treatment time (generated by subtracting the variable wanted from the survey year), I have the treatment group alligned where 0 is the time where the shock took place. But for the control group, just the survey year is available.

          Thank you very much in advance for helping, I really appreciate it.

          Comment


          • #6
            That's quite a different question. Please start a new thread with a suitable title.

            Comment


            • #7
              Hi all, apologies for posting this thread, but this was a question similar to mine. I am creating minimum years for first_treat for house transactions to apply csdid. The grouping is the same house sold in different transactions.

              I applied the formula below, but it worked sporadically, I thought at first it was where there was only one group transaction, but that doesn't seem to be it. Could anyone help?


              bysort objectid : egen ftreat_10km = min(cond(treated_10km == 1, year, 0))

              Thanks!

              Data example below
              objectid addedyear treated_10km ftreat_10km
              1 1997 0 0
              1 2005 1 0
              2 2012 1 2012
              3 2000 0 0
              5 1996 0 0
              5 1998 0 0
              7 1993 0 0
              13 2008 1 2008
              14 2022 1 2022
              15 1999 0 0
              15 2022 1 0
              Last edited by Jon Thomson-Settle; 19 Nov 2024, 15:47. Reason: Sorry - table changed when I posted it

              Comment


              • #8
                The main idea in #7 seems right, but I can't explain your result for objectid 1.

                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input byte objectid int addedyear byte treated_10km int ftreat_10km
                 1 1997 0    0
                 1 2005 1    0
                 2 2012 1 2012
                 3 2000 0    0
                 5 1996 0    0
                 5 1998 0    0
                 7 1993 0    0
                13 2008 1 2008
                14 2022 1 2022
                15 1999 0    0
                15 2022 1    0
                end
                
                bysort objectid : egen wanted = min(cond(treated_10km, addedyear, .))
                
                list, sepby(objectid)
                
                     +----------------------------------------------------+
                     | objectid   addedy~r   treate~m   ftreat~m   wanted |
                     |----------------------------------------------------|
                  1. |        1       1997          0          0     2005 |
                  2. |        1       2005          1          0     2005 |
                     |----------------------------------------------------|
                  3. |        2       2012          1       2012     2012 |
                     |----------------------------------------------------|
                  4. |        3       2000          0          0        . |
                     |----------------------------------------------------|
                  5. |        5       1996          0          0        . |
                  6. |        5       1998          0          0        . |
                     |----------------------------------------------------|
                  7. |        7       1993          0          0        . |
                     |----------------------------------------------------|
                  8. |       13       2008          1       2008     2008 |
                     |----------------------------------------------------|
                  9. |       14       2022          1       2022     2022 |
                     |----------------------------------------------------|
                 10. |       15       1999          0          0     2022 |
                 11. |       15       2022          1          0     2022 |
                     +----------------------------------------------------+

                Comment

                Working...
                X