Announcement

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

  • Generate treatment that is dependent on previous and future years' variable values

    Hi all,

    I'm having difficulty generating a treatment and control group for my panel after searching unsuccessfully for a solution on Statalist. I have firm id, year, regis_group (firm's registration type), sales, export, employment.

    1) How do I generate treatment in which before year 2002, regis_group can be either 1, 2 or 3 or combination and year 2002 till 2006 the regis_group needs to be 4? (see dataex id "100013968")
    2) How do I generate a control in which before year 2002, regis_group can be either 1, 2 or 3 or combination and in year 2002 till 2006, the regis_group needs to be different from the last value in 2001 but not take the value 4 and follow this pattern thereafter? (see dataex id "100019964")

    Appreciate your help.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str9 id float(year regis_group) double(sales export) long emp
    "100011508" 2003 1           0         0    70
    "100011516" 1998 1 10810299000 134505000 60530
    "100011516" 1999 1  8537410000 107470000 50465
    "100011516" 2000 1  8922932000 231721000 52226
    "100011516" 2001 1 13978554000 248648000 44609
    "100011516" 2003 1 16871809000   8514000 41993
    "100011516" 2005 1  1195377000         0  9770
    "100013968" 1998 1   561527000  32659000 10645
    "100013968" 1999 1   590480000  45579000 10486
    "100013968" 2000 1   744993000  66293000  8949
    "100013968" 2001 1   780799000  64777000  8877
    "100013968" 2002 4   723199000 172516000  8495
    "100013968" 2003 4   825809000 183443000  7880
    "100013968" 2004 4  1844572000         .  7183
    "100013968" 2005 4  1055243000 240015000  6925
    "100013968" 2006 4  1061813000 308052000  6727
    "100014151" 2006 4    24193000         0    25
    "100016069" 1998 1   261016000         0    41
    "100016069" 1999 1   189262000         0    63
    "100016069" 2004 1    35064000         .    64
    "100016069" 2006 1    76030000  23007000   102
    "100019964" 1998 3   329570000   6510000  1471
    "100019964" 1999 3   384030000   8990000  1403
    "100019964" 2000 3   3.801e+08   9880000  1381
    "100019964" 2001 3   509510000   8870000  1214
    "100019964" 2002 2   985290000   8270000  1205
    "100019964" 2003 2  1481020000  28000000  1260
    "100019964" 2004 2  1550048000         .  1285
    "100019964" 2005 1  1415840000 154870000  1235
    "100019964" 2006 1  1537510000 188980000  1185
    "100023808" 2001 3    39780000         0   200
    "100023808" 2002 3    27743000         0   123
    "100023808" 2003 3    27480000         0   257
    "100023808" 2004 3    28991000         .   261
    "100023808" 2005 3    23443000         0   254
    "100023808" 2006 3     6408000         0   117
    "100024130" 1999 3    16131000         0    35
    "100024130" 2000 3    25651000         0    36
    "100024130" 2001 3    10468000         0    36
    "100024130" 2002 3    27733000         0    30
    "100024130" 2003 3    61086000         0    30
    "100024130" 2004 3   143185000         .    35
    "100024130" 2005 3   347424000         0    37
    "100024130" 2006 3   818107000         0    39
    "100024982" 1999 3    38578000         0    78
    "100024982" 2000 3   369757000         0   121
    "100025037" 1999 3     7436000         0    15
    "100026347" 2004 3     8420000         .    42
    "100026435" 2005 3   138082000         0    12
    "100026435" 2006 3   146080000         0    16
    "100026793" 1998 3   253740000         0  1705
    "100026793" 1999 3   795443000  49014000  2438
    "100026793" 2000 3  1926873000  43737000  2971
    "100026793" 2001 3  2837997000  32810000  2585
    "100026793" 2002 3  3548779000  63879000  2478
    "100026793" 2003 3  4038365000 115249000  2879
    "100026793" 2004 3  5242410000         .  2857
    "100026793" 2005 3  5314752000  86281000  3101
    "100026793" 2006 3  5677846000 170152000  3223
    "100026961" 2004 3   302533000         .   818
    "100026961" 2005 3   303240000  79650000   677
    "100026961" 2006 3   225360000  76870000   638
    "100027067" 1999 1      110000         0    19
    "100027067" 2000 1       90000         0     6
    "100028078" 2001 3    75144000         0   167
    "100028078" 2002 3    82364000         0   108
    "100028078" 2003 3    82504000         0    86
    "100028190" 2001 3     6017000         0     9
    "100028190" 2002 3     7598000         0     9
    "100028190" 2004 3     9820000         .    45
    "100028190" 2005 3    19928000         0    58
    "100028190" 2006 3    28218000         0    55
    "100116596" 1998 1      229000         0     4
    "100132211" 1998 1      898000         0   165
    "100140035" 2001 1     6384000   6384000   197
    "100140035" 2002 1     8893000   8079000   415
    "100207201" 1998 1      510000         0   130
    "100257110" 1999 3    18235000   1422000    68
    "100257110" 2000 3     1800000         0    68
    "100360652" 2001 2    28350000         0   180
    "100360652" 2002 2    33180000         0   286
    "100360652" 2003 2    42990000         0   270
    "100360652" 2005 3    40400000         0   210
    "10055211X" 2000 1     5300000         0    30
    "100584066" 1998 4    25000000         0   250
    "100584066" 1999 1    12000000         0   150
    "100584066" 2000 1     5330000         0   140
    "100725471" 2004 3    59283000         .   256
    "100725471" 2005 3   190807000         0   256
    "100725471" 2006 3   249957000         0   260
    "100740735" 2001 1     9600000         0    40
    "100740735" 2002 1    10500000         0    40
    "1007722"   1999 2     6870000         0   150
    "10091298X" 1998 1    50980000         0   391
    "10100769"  2003 1        1000         0    45
    "1010076X"  1998 1      499000         0    33
    "1010076X"  1999 1     6812000         0    58
    "1010076X"  2000 1     7286000         0    45
    "1010076X"  2001 1     7434000         0    45
    "1010076X"  2002 1     4846000         0    45
    end
    ------------------ copy up to and including the previous line ------------------

    Listed 100 out of 1716001 observations

  • #2
    You did not say what to do if there is no observation for year 2001 to serve as the reference for the comparison for years 2002-2006 for controls. But from your example data, it appears that you do not want to apply criterion 2) if there is no such observation. So:
    Code:
    by id (year), sort:egen part1 = min(cond(year < 2002, inlist(regis_group, 1, 2, 3), .))
    
    by id (year): egen regis2001 = max(cond(year == 2001, regis_group, .))
    
    by id (year): egen part2 = min(cond(inrange(year, 2002, 2006), regis_group == 4, .))
    
    by id (year): egen part3 = ///
        min(cond(inrange(year, 2002, 2006), ///
            !inlist(regis_group, 4, regis2001) & !missing(regis2001), .))
    
    label define treat_control    0    "Control"    1    "Treated"
    gen byte treat_control:treat_control = 1 if part1 == 1 & part2 == 1
    replace treat_control = 0 if part1 == 1 & part3 == 1

    Comment


    • #3
      Hi Clyde. Thanks for your help and code. Indeed I do not want to assign controls if no observation for year 2001.

      Code works fine except for by id (year) : egen part3. Appreciate your help here. "///" may be typo?

      Code:
      by id (year): egen part3 = ///
      unknown egen function ///()
      r(133);

      . min(cond(inrange(year, 2002, 2006), ///
      command min is unrecognized
      r(199);

      . !inlist(regis_group, 4, regis2001) & !missing(regis2001), .))

      zsh:1: parse error near `)'

      Comment


      • #4
        Clyde Schechter is writing code on the assumption that you will run it from the do-file editor. It’s not 3 commands to be typed in interactively one by one, but one command that Clyde typed on 3 physical lines. Each use of /// flags to Stata that the command has not yet finished but /// is not allowed interactively.

        Comment


        • #5
          Thank you Clyde Schechter and Nick Cox , I tried it already and it works fine! Really appreciate it!

          Comment

          Working...
          X