Announcement

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

  • generate a policy dummy

    Dear All, I find this question here (in Chinese).The data set is:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int id str6 status int effective byte rt
      1 "join"   2016 0
      2 "join"   2016 0
      5 "join"   2016 1
      5 "remove" 2018 1
      6 "join"   2016 0
      8 "join"   2017 0
      9 "join"   2016 0
     10 "join"   2016 1
     10 "remove" 2017 1
     12 "join"   2016 0
     21 "join"   2016 0
     22 "join"   2018 1
     25 "join"   2017 1
     25 "remove" 2021 1
     27 "join"   2016 0
     28 "join"   2016 0
     29 "join"   2016 1
     29 "remove" 2021 1
     31 "join"   2016 0
     32 "join"   2020 0
     34 "join"   2017 0
     35 "join"   2016 0
     39 "join"   2016 0
     40 "join"   2018 1
     40 "remove" 2020 1
     42 "join"   2016 1
     42 "remove" 2019 1
     45 "join"   2017 1
     45 "remove" 2018 1
     46 "join"   2016 0
     48 "join"   2021 0
     50 "join"   2016 0
     55 "join"   2017 1
     55 "remove" 2018 1
     59 "join"   2016 0
     60 "join"   2016 0
     61 "join"   2016 0
     62 "join"   2016 0
     63 "join"   2016 0
     65 "join"   2016 1
     65 "remove" 2020 1
     66 "join"   2016 0
     68 "join"   2016 1
     68 "remove" 2018 1
     69 "join"   2016 0
     78 "join"   2016 0
     89 "join"   2016 0
     90 "join"   2016 0
     96 "join"   2016 1
     96 "remove" 2019 1
     99 "join"   2016 1
     99 "remove" 2018 1
    100 "join"   2016 0
    150 "join"   2016 1
    150 "remove" 2019 1
    151 "join"   2016 1
    151 "remove" 2017 1
    156 "join"   2016 0
    157 "join"   2016 0
    158 "join"   2016 0
    166 "join"   2016 0
    338 "join"   2016 0
    400 "join"   2016 0
    402 "join"   2016 0
    403 "join"   2020 0
    407 "join"   2017 1
    407 "remove" 2019 1
    409 "join"   2017 1
    409 "remove" 2018 1
    410 "join"   2016 1
    410 "remove" 2017 1
    413 "join"   2016 1
    413 "remove" 2020 1
    415 "join"   2016 0
    417 "join"   2016 1
    417 "remove" 2019 1
    420 "join"   2021 0
    423 "join"   2016 0
    425 "join"   2016 0
    428 "join"   2016 1
    428 "remove" 2017 1
    488 "join"   2016 0
    501 "join"   2016 0
    503 "join"   2016 1
    503 "remove" 2019 1
    506 "join"   2016 1
    506 "remove" 2018 1
    510 "join"   2017 1
    510 "remove" 2018 1
    513 "join"   2016 0
    514 "join"   2016 1
    514 "remove" 2018 1
    517 "join"   2016 1
    517 "remove" 2021 1
    518 "join"   2016 1
    518 "remove" 2018 1
    525 "join"   2016 1
    525 "remove" 2020 1
    528 "join"   2016 0
    531 "join"   2016 1
    end
    For each firm (`id'), we wish to generate a dummy for joining a policy over the 2010-2021 period. When `status' is "join" ("remove"), it means the firm is (not) under the influence of policy, and `effective' means the starting year. When the last variable `rt' is 0, it suggests that the firm joins and has not been removed. When `rt' is 1, it suggests that the firm first joins and then removed (please see `effective' for the corresponding year). As such, the policy dummy is 1 if the firm is under the influence of the policy, 0 otherwise. Note that, of course, before the `join' year, the wanted dummy is 0. Any suggestions? Thanks.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    Code:
    g treat = cond(status=="join",1,0)

    Comment


    • #3
      Dear Jared, My bad. I find my expression above is not complete (exact). Taking id=1, and id=5 as examples, the following is what I want.
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input long id float year byte wanted
      1 2010 0
      1 2011 0
      1 2012 0
      1 2013 0
      1 2014 0
      1 2015 0
      1 2016 1
      1 2017 1
      1 2018 1
      1 2019 1
      1 2020 1
      1 2021 1
      5 2010 0
      5 2011 0
      5 2012 0
      5 2013 0
      5 2014 0
      5 2015 0
      5 2016 1
      5 2017 1
      5 2018 0
      5 2019 0
      5 2020 0
      5 2021 0
      end
      For id=1, the status is 1 from year 2016. Moreover, rt=0, indicating that the firm is not removed. Thus, the dummy `wanted' is 1 starting from (including) year 2016, 0 before 2016. In contrast, for id=5, the status is 1 from year 2016, and 0 from 2018. It indicates that the id=5 is under policy influence only in years 2016 and 2017 but not the other years. Thus, the dummy `wanted' is 1 in these two years, 0 otherwise. Any further suggestions are highly appreciated.
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment


      • #4
        The code below should work. #1 omitted a request of the original Chinese post: Drop the ids whose years of "join" and "remove" are identical.

        Code:
        bys id: drop if _N == 2 & effective[1] == effective[2]
        bys id (effective): gen status_no = _n
        reshape wide status effective, i(id) j(status_no)
        
        expand 12
        bys id: egen year = seq(), from(2010) to(2021)
        gen policy = inrange(year,effective1,effective2-1)

        Comment


        • #5
          Here's another approach. As I see it, the hard part here is that the starting data only begins in 2016, but the desired output is supposed to extend back to 2010. To get that fill, we have add to the data set a fake observation with year = 2010.

          Code:
          //  ADD FAKE OBSERVATION TO EXTEND RANGE OF "EFFECTIVE" DOWN TO 2010
          expand 2 in L
          replace id = 0 in L
          replace status = "" in L
          replace effective = 2010 in L
          
          //  FILL OUT THE DATA SET SO EACH ID HAS AN OBSERVATION IN EACH YEAR
          tsset id effective
          tsfill, full
          
          //  CALCULATE THE DESIRED VARIABLE
          by id (effective): gen wanted = sum(status == "join") - sum(status == "remove")
          drop if id == 0 // REMOVE FAKE OBSERVATIONS

          Comment


          • #6
            Dear @Fei Wang and @Clyde Schechter, Many thanks for these helpful suggestions.
            Ho-Chuan (River) Huang
            Stata 19.0, MP(4)

            Comment

            Working...
            X