Repository navigation
Expand file tree
/
Copy patheventstudyweights.ado
More file actions
68 lines (60 loc) · 2.14 KB
/
Copy patheventstudyweights.ado
File metadata and controls
68 lines (60 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
*! version 0.2 29jul2021 Liyang Sun, lsun20@mit.edu
** version 0.1 9apr2021 Liyang Sun, lsun20@mit.edu
** version 0.0 31aug2020 Liyang Sun, lsun20@mit.edu
program define eventstudyweights, eclass sortpreserve
version 13
syntax varlist(min=1 numeric) [if] [in] [aw fw iw pw], absorb(varlist numeric ts fv) cohort(varname) ///
rel_time(varname) ///
[COVARIATEs(varlist numeric ts fv) saveweights(string) ///
*]
set more off
* Mark sample (reflects the if/in conditions, and includes only nonmissing observations)
marksample touse
markout `touse' `by' `xq' `covariates' `absorb', strok
* Prepare the list of relative time indicators for partialling out
local nvarlist ""
local dvarlist ""
qui ds
local avarlist = r(varlist)
foreach l of local varlist {
local dvarlist "`dvarlist' `l'"
tempname n`l'
qui gen `n`l'' = `l'
local nvarlist "`nvarlist' `n`l''"
}
capture hdfe, version
if _rc != 0 {
di as err "Error: must have hdfe installed"
di as err "To install, from within Stata type " _c
di in smcl "{stata ssc install hdfe :ssc install hdfe}"
exit 601
}
qui hdfe `nvarlist' `covariates' if `touse' [`weight'`exp'], absorb(`absorb') clear keepvars(`avarlist') // residuals are stored in `nvarlist'
* Initiate empty matrix for weights
tempname bb bb_w
qui levelsof `cohort', local(cohort_list) // missing cohort includes the never treated units
qui levelsof `rel_time', local(rel_time_list)
* Loop over cohort and relative times
foreach yy of local cohort_list {
foreach rr of local rel_time_list {
tempvar catt
qui count if `cohort' == `yy' & `rel_time' == `rr'
if r(N) >0 {
qui gen `catt' = (`cohort' == `yy') * (`rel_time' == `rr')
qui regress `catt' `nvarlist' if `touse' [`weight'`exp'], nocons
mat `bb_w' = e(b)
mat `bb_w' = `yy', `rr', `bb_w'
matrix `bb' = nullmat(`bb') \ `bb_w'
}
}
}
qui capture drop `nvarlist'
matrix colnames `bb' = "`cohort'" "`rel_time'" `dvarlist'
// mat list `bb'
if "`saveweights'" != "" {
putexcel set `saveweights', replace
putexcel A1=matrix(`bb', colnames) using `saveweights',replace
}
ereturn clear
ereturn matrix weights `bb'
end