Announcement

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

  • make dummy =1 in year t to in year t-1

    Dear all,

    I have a dummy variable, enact, which can only =1 once in the sample period. I want to create a "pseudo" dummy variable, enact1, which =1 in the year before the actual enact dummy =1. e.g., for firm 2, it has enact =1 in 2013, then enact1 should be =1 in 2012. I know this can be partly achieved by using the following code
    Code:
    bysort firm(year): gen enact1 = enact[_n+1]
    replace enact1=0 if enact1==.
    One problem with this code is, my sample period is 2010-2019, and some firms have less than 10 years of observation. If a firm has enact =1 in 2010, and 2010 is the first year of the sample period for that firm, then the code above won't create the enact1 dummy, because I don't have 2009 for that firm. I wonder what code could help to create the year t-1 first, and then code enact1 =1 for that year. Thanks a lot in advance for any help!

    Som sample data
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte firm float(year enact)
    1 2010 1
    1 2011 0
    1 2012 0
    1 2013 0
    1 2014 0
    1 2015 0
    1 2016 0
    1 2017 0
    1 2018 0
    1 2019 0
    2 2010 0
    2 2011 0
    2 2012 0
    2 2013 1
    2 2014 0
    2 2015 0
    2 2016 0
    2 2017 0
    2 2018 0
    2 2019 0
    3 2014 1
    3 2015 0
    3 2016 0
    3 2017 0
    3 2018 0
    3 2019 0
    4 2011 1
    4 2012 0
    4 2013 0
    4 2014 0
    4 2015 0
    4 2016 0
    4 2017 0
    4 2018 0
    4 2019 0
    end

  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte firm float(year enact)
    1 2010 1
    1 2011 0
    1 2012 0
    1 2013 0
    1 2014 0
    1 2015 0
    1 2016 0
    1 2017 0
    1 2018 0
    1 2019 0
    2 2010 0
    2 2011 0
    2 2012 0
    2 2013 1
    2 2014 0
    2 2015 0
    2 2016 0
    2 2017 0
    2 2018 0
    2 2019 0
    3 2014 1
    3 2015 0
    3 2016 0
    3 2017 0
    3 2018 0
    3 2019 0
    4 2011 1
    4 2012 0
    4 2013 0
    4 2014 0
    4 2015 0
    4 2016 0
    4 2017 0
    4 2018 0
    4 2019 0
    end
    
    bysort firm (year) : gen toexpand = cond(_n == 1, 2 * enact[1], 0)  
    expand toexpand 
    bysort firm (year) : replace year = year-1 if _n == 1 
    
    list, sepby(firm)
    
         +--------------------------------+
         | firm   year   enact   toexpand |
         |--------------------------------|
      1. |    1   2009       1          2 |
      2. |    1   2010       1          2 |
      3. |    1   2011       0          0 |
      4. |    1   2012       0          0 |
      5. |    1   2013       0          0 |
      6. |    1   2014       0          0 |
      7. |    1   2015       0          0 |
      8. |    1   2016       0          0 |
      9. |    1   2017       0          0 |
     10. |    1   2018       0          0 |
     11. |    1   2019       0          0 |
         |--------------------------------|
     12. |    2   2009       0          0 |
     13. |    2   2011       0          0 |
     14. |    2   2012       0          0 |
     15. |    2   2013       1          0 |
     16. |    2   2014       0          0 |
     17. |    2   2015       0          0 |
     18. |    2   2016       0          0 |
     19. |    2   2017       0          0 |
     20. |    2   2018       0          0 |
     21. |    2   2019       0          0 |
         |--------------------------------|
     22. |    3   2013       1          2 |
     23. |    3   2014       1          2 |
     24. |    3   2015       0          0 |
     25. |    3   2016       0          0 |
     26. |    3   2017       0          0 |
     27. |    3   2018       0          0 |
     28. |    3   2019       0          0 |
         |--------------------------------|
     29. |    4   2010       1          2 |
     30. |    4   2011       1          2 |
     31. |    4   2012       0          0 |
     32. |    4   2013       0          0 |
     33. |    4   2014       0          0 |
     34. |    4   2015       0          0 |
     35. |    4   2016       0          0 |
     36. |    4   2017       0          0 |
     37. |    4   2018       0          0 |
     38. |    4   2019       0          0 |
         +--------------------------------+

    Comment


    • #3
      Hi Nick,
      Thanks a lot, it works well!

      Comment

      Working...
      X