Announcement

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

  • Create a variable that indicates when unit was first treated with panel data

    Hello everyone. This is my first post here, I have been looking for information on how to do this but have yet to find a way. I am working with an unbalanced panel data set for my thesis and I want to use did_imputation. One of the required input variables is Ei. It indicates when a unit was first treated. It takes the value . (missing) for all observations of units that were never treated, and the value of the period of first treatment for all observations of units that were.

    My data is for years 2008 to 2021, and the first treated units appear in 2009. This is an example of how I would like the data to look for one unit when the unit was treated in 2013 and one that was never treated:
    unit year treatment firsttreat
    1 2008 0 2013
    1 2009 0 2013
    1 2010 0 2013
    1 2011 0 2013
    1 2012 0 2013
    1 2013 1 2013
    1 2014 1 2013
    1 2015 1 2013
    1 2016 1 2013
    1 2017 1 2013
    1 2018 1 2013
    1 2019 1 2013
    1 2020 1 2013
    1 2021 1 2013
    2 2012 0 .
    2 2013 0 .
    2 2014 0 .
    2 2015 0 .
    2 2016 0 .
    2 2017 0 .
    2 2018 0 .
    2 2019 0 .
    2 2020 0 .
    2 2021 0 .

    I am honestly at a complete loss as to how I could go about this. I would very much appreciate any help.

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte unit int year byte treatment
    1 2008 0
    1 2009 0
    1 2010 0
    1 2011 0
    1 2012 0
    1 2013 1
    1 2014 1
    1 2015 1
    1 2016 1
    1 2017 1
    1 2018 1
    1 2019 1
    1 2020 1
    1 2021 1
    2 2012 0
    2 2013 0
    2 2014 0
    2 2015 0
    2 2016 0
    2 2017 0
    2 2018 0
    2 2019 0
    2 2020 0
    2 2021 0
    end
    
    by unit (year), sort: egen first_treated = min(cond(treatment, year, .))
    In the future, when showing data examples, please use the -dataex- command to do so,as I have done here. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte unit int year byte treatment
      1 2008 0
      1 2009 0
      1 2010 0
      1 2011 0
      1 2012 0
      1 2013 1
      1 2014 1
      1 2015 1
      1 2016 1
      1 2017 1
      1 2018 1
      1 2019 1
      1 2020 1
      1 2021 1
      2 2012 0
      2 2013 0
      2 2014 0
      2 2015 0
      2 2016 0
      2 2017 0
      2 2018 0
      2 2019 0
      2 2020 0
      2 2021 0
      end
      
      by unit (year), sort: egen first_treated = min(cond(treatment, year, .))
      In the future, when showing data examples, please use the -dataex- command to do so,as I have done here. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.
      Ok, perfect. Thank you! I will use dataex next time.

      Comment


      • #4
        See also

        https://www.stata-journal.com/articl...article=dm0055 Section 9

        https://www.stata.com/support/faqs/d...t-occurrences/

        Comment


        • #5
          Thank you! I've been looking for info on how to use gen, even and cond for more complicated applications.

          Comment

          Working...
          X