Announcement

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

  • Generating a Time to Treat variable for an Event Plot with Multiple treatments starting at Different times

    Hello Statalisters,

    I am in dire need of help as I am unable to generate the time_to_treat variable needed in most event study packages. My dataset has two treatments Major and Minor and I have created the respective years of those treatments, I now need to generate the time variable relative to those treatments (Year_Minor and Year_Major)

    input int year long fips float(Year_Minor Year_Major)
    1991 1001 . .
    1992 1001 . .
    1993 1001 . .
    1994 1001 . .
    1995 1001 . .
    1996 1001 . .
    1997 1001 . .
    1998 1001 . .
    1999 1001 . .
    2001 1001 . .
    2002 1001 . .
    2003 1001 . .
    2004 1001 . .
    2005 1001 . .
    2008 1001 . .
    2010 1001 . .
    2014 1001 . .
    2015 1001 . .
    2016 1001 . .
    2017 1001 . .
    2018 1001 . .
    2019 1001 . .
    2020 1001 . .
    2021 1001 . .
    2022 1001 . .
    2023 1001 . .
    1991 1003 . .
    1992 1003 . .
    1993 1003 . .
    1994 1003 . .
    1995 1003 1995 .
    1996 1003 . .
    1997 1003 1997 .
    1998 1003 1998 .
    1999 1003 . .
    2001 1003 . .
    2002 1003 . .
    2003 1003 . .
    2004 1003 . 2004
    2005 1003 2005 .
    2008 1003 . .
    2010 1003 . .
    2014 1003 . .
    2015 1003 . .
    2016 1003 . .
    2017 1003 . .
    2018 1003 2018 .
    2019 1003 . .
    2020 1003 . 2020
    2021 1003 . .
    2022 1003 . .
    2023 1003 . .
    1991 1005 . .
    1992 1005 . .
    1993 1005 . .
    1994 1005 . .
    1995 1005 . .
    1996 1005 . .
    1997 1005 . .
    1998 1005 . .
    1999 1005 . .
    2001 1005 . .
    2002 1005 . .
    2003 1005 . .
    2004 1005 . .
    2005 1005 . .
    2008 1005 . .
    2010 1005 . .
    2014 1005 . .
    2015 1005 . .
    2016 1005 . .
    2017 1005 . .
    2018 1005 2018 .
    2019 1005 . .
    2020 1005 . .
    2021 1005 . .
    2022 1005 . .
    2023 1005 . .
    1991 1007 . .
    1992 1007 . .
    1993 1007 . .
    1994 1007 . .
    1995 1007 . .
    1996 1007 . .
    1997 1007 . .
    1998 1007 . .
    1999 1007 . .
    2001 1007 . .
    2002 1007 . .
    2003 1007 . .
    2004 1007 . .
    2005 1007 . .
    2008 1007 . .
    2010 1007 . .
    2014 1007 . .
    2015 1007 . .
    2016 1007 . .
    2017 1007 . .
    2018 1007 . .
    2019 1007 . .


  • #2
    Code:
    egen ttminor = max(Year_Minor), by(fips)
    egen ttmajor = max(Year_Major), by(fips)
    
    g t2tminor = year-ttminor
    g t2tmajor = year-ttmajor

    Comment


    • #3
      Hi George,

      Thank you very much for responding. However, this does not work for my design. I am trying to generate two separate event plots one for a major hurricane and another for a minor hurricane. Thus, I had created the years of the specific treatments and hence, I need to get the relative time_to_treat from these years of treatment. Also in my data, a county is treated multiple times.

      bys fips year: gen Year_Minor = year if Wind>=64 & Wind<=95, before(year)
      bys fips year: gen Year_Major = year if Wind>95 & Wind<999, before(year)

      input int year double fips float(Year_Minor Year_Major Wind)
      1990 1001 . . .
      1991 1001 . . 2.2893946
      1992 1001 . . 17.231062
      1993 1001 . . 1.446508
      1994 1001 . . 29.7827
      1995 1001 . . 59.5928
      1996 1001 . . 4.4682536
      1997 1001 . . 30.071825
      1998 1001 . . 26.45237
      1999 1001 . . 6.222265
      2000 1001 . . .
      2001 1001 . . 33.63532
      2002 1001 . . 19.51485
      2003 1001 . . 3.527311
      2004 1001 . . 51.00212
      2005 1001 . . 43.84644
      2006 1001 . . .
      2007 1001 . . .
      2008 1001 . . 11.092205
      2009 1001 . . .
      2010 1001 . . 1.9921166
      2015 1001 . . 4.912673
      2016 1001 . . 22.574387
      2017 1001 . . 30.829926
      2018 1001 . . 40.05725
      1990 1003 . . .
      1991 1003 . . 1.6038173
      1992 1003 . . 22.37851
      1993 1003 . . .8866239
      1994 1003 . . 44.78434
      1995 1003 1995 . 73.828476
      1996 1003 . . 2.9656675
      1997 1003 1997 . 66.42067
      1998 1003 1998 . 66.62316
      1999 1003 . . 4.2566943
      2000 1003 . . .
      2001 1003 . . 42.00578
      2002 1003 . . 34.52595
      2003 1003 . . 7.246689
      2004 1003 . 2004 112.5204
      2005 1003 2005 . 79.35527
      2006 1003 . . .
      2007 1003 . . .
      2008 1003 . . 27.58461
      2009 1003 . . .
      2010 1003 . . 2.527151
      2014 1003 . . 3.516019
      2015 1003 . . 4.6265264
      2016 1003 . . 20.51896
      2017 1003 . . 53.46472
      2018 1003 2018 . 65.19765
      1990 1005 . . .
      1991 1005 . . 3.594553
      1992 1005 . . 11.187775
      1993 1005 . . 1.9936098
      1994 1005 . . 34.939507
      1995 1005 . . 59.0539
      1996 1005 . . 6.938338
      1997 1005 . . 28.01658
      1998 1005 . . 35.86039
      1999 1005 . . 9.723162
      2000 1005 . . .
      2001 1005 . . 40.92657
      2002 1005 . . 14.635206
      2003 1005 . . 3.12525
      2004 1005 . . 37.109432
      2005 1005 . . 41.45851
      2006 1005 . . .
      2007 1005 . . .
      2008 1005 . . 10.149692
      2009 1005 . . .
      2010 1005 . . 1.482621
      2015 1005 . . 3.6058176
      2016 1005 . . 36.78129
      2017 1005 . . 27.29396
      2018 1005 2018 . 71.08755
      1991 1007 . . 1.780672
      1992 1007 . . 23.25992
      1994 1007 . . 23.457073
      1995 1007 . . 40.64491
      1996 1007 . . 3.896007
      1997 1007 . . 26.58225
      1998 1007 . . 25.45125
      2000 1007 . . .
      2002 1007 . . 23.321436
      2003 1007 . . 3.597654
      2004 1007 . . 47.12711
      2005 1007 . . 36.923332
      2006 1007 . . .
      2007 1007 . . .
      2008 1007 . . 11.34131
      2009 1007 . . .
      2010 1007 . . 2.429665
      2015 1007 . . 6.089266
      2016 1007 . . 16.46585
      2017 1007 . . 34.35956
      2018 1007 . . 27.97218
      1990 1009 . . .
      1991 1009 . . 2.0355842
      1992 1009 . . 24.81802

      Comment


      • #4
        Code:
        gsort fips -year
        by fips: gen minor_num = sum(!missing(Year_Minor))
        by fips: gen major_num = sum(!missing(Year_Major))
        gsort fips minor_num -year
        by fips minor_num: gen tt_minor = Year_Minor[1] - year
        gsort fips major_num -year
        by fips major_num: gen tt_major = Year_Major[1] - year
        sort fips year
        drop minor_num major_num

        Comment

        Working...
        X