Getting closer. The problem, however, is that between those two, there is only one observation for any combination of CodeX and yearz. So there can be no firm-year regressions in this example. But you can get year cross-sectional regressions:
Now, this code does not actually run to completion in your example data. That's because in year 2002, we have D = 0 for all observations. That means that the D variable and all its interactions drop from the regression due to colinearity. So when the code tries to store the delta values for year 2002, it feels because the corresponding coefficients do not exist. Presumably you do not have D = 0 in all observations in year 2002 in your real data, so you won't run into that problem.
Code:
forvalues i = 1/4 { gen alpha`i' = . gen delta`i' = . } levelsof yearz, local(yereg) foreach y of local yereg { capture noisily reg E (i.D##c.R)##c.(SIZE LEV MTB)if yearz == `y' if c(rc) == 0 { replace alpha1 = _b[R] if yearz == `y' replace alpha2 = _b[c.R#c.SIZE] if yearz == `y' replace alpha3 = _b[c.R#c.MTB] if yearz == `y' replace alpha4 = _b[c.R#c.LEV] if yearz == `y' replace delta1 = _b[1.D#c.R] if yearz == `y' replace delta2 = _b[1.D#c.R#c.SIZE] if yearz == `y' replace delta3 = _b[1.D#c.R#c.MTB] if yearz == `y' replace delta4 = _b[1.D#c.R#c.LEV] if yearz == `y' } else if inlist(c(rc), 2000, 2001) { display as text "Insufficient observations in year `y'" } else { display as error "Unexpected regression error in year `y'" exit c(rc) } }
Comment