Announcement

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

  • Creating Treatment Timing Column

    I have the firms treated in different years. I want to create a column that has the year of treatment as the value for all years for that firm. This is the example dataset:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float firmid byte treat int year
    1 0 2016
    1 0 2017
    1 0 2018
    1 0 2019
    1 0 2020
    1 0 2021
    1 0 2022
    2 0 2016
    2 0 2017
    2 0 2018
    2 0 2019
    2 1 2020
    2 1 2021
    2 1 2022
    3 0 2016
    3 0 2017
    3 0 2018
    3 0 2019
    3 0 2020
    3 1 2021
    4 0 2016
    4 0 2017
    4 0 2018
    4 0 2019
    4 0 2020
    4 0 2021
    4 0 2022
    5 0 2016
    5 0 2017
    5 0 2018
    5 0 2019
    5 1 2020
    5 1 2021
    5 1 2022
    end
    I want a treat_timing variable which has value 0 for firm 1, 2020 for Firm 2, 2021 for Firm 3 and so on. Is there a way I can do it without generating a zero variable and replacing with treatment year value for each firm?

  • #2
    It is not exactly what you are asking for, but I recommend you do this:
    Code:
    by firmid, sort: egen treat_timing = min(cond(treat, year, .))
    It differs from your request in that a firm that is never treated receives missing value rather than 0 in the new variable. I recommend that because in Stata it is usually a bad idea to use numeric codes to represent missing values. It is better to explicitly use missing values as most Stata commands will recognize them as such and treat them appropriately. For those firms that are never treated, it is not correct to say that they were treated in year 0 (which is what your approach implies). Rather the time at which they are treated is either non-existent or is some unknown time after the end of the data. So it is more appropriately represented as a missing value.

    If, nevertheless, you have some compelling reason for representing those as 0, then you can use a -replace- or -mvencode- command to do that.

    Comment


    • #3
      Hi Clyde,

      Thank you for the code. This is very helpful. I wanted to use the csdid package by Callaway and Sant’Anna and I believe the gvar variable requires 0 for control groups. So, I will follow what you suggested to fill their values.

      Comment

      Working...
      X