Announcement

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

  • Problem extracting coefficients from a DID regression

    Hi all,

    I'm replicating a Donald & Lang (2007)-style 2-step difference-in-differences approach using Stata with svy:. In Step 1, I regress an outcome (maternity_leave) on an interacted treatment-by-year set of dummies using:
    svy: reg outcome i.year i.treat#i.year controls, nocons
    I want to extract only the coefficients of the treat#year and combine them with the summed person weights (asecwt) by year to prepare a dataset for Step 2 including: year, coefficients (pi_vector), and summed weights. Years included are 2015-2024 (10 coefficients to extract).

    I've tried using matrix b = e(b) followed by svmat, but I ran into errors because of the structure of e(b). I'd like a simple and reproducible way to:
    • Pull out the treat#year πₜ coefficients
    • Match them to a year variable
    • Add summed weights by year
    What’s the cleanest way to do this? For reference, Step 2 is: reg pi_vector post [aw=sum_weight] (post is an indicator for year >=2019).

    Thanks in advance!
    Last edited by Iman Haupricht; 26 Jun 2025, 07:48.

  • #2
    r(table)[r,c] ?

    or use e(b) to get the names of the coefficients and use _b[x]

    Comment


    • #3
      Thank you for your response ! I finally did figure it out this way :

      tempfile wts
      preserve
      keep if treat1 == 1 | control1 == 1
      egen sum_weight = total(asecwt), by(year)
      keep year sum_weight
      duplicates drop year, force
      save `wts', replace
      restore

      *use `wts', clear
      *list, clean

      * 3. First‐step pooled regression with year × treat interactions
      svy: reg maternity_leave ///
      i.year i.year#i.treat ///
      i.age_cat i.race_cat i.marst_cat i.educ_cat i.yngch bornus, nocons

      * 4. Extract π̂ into a matrix (years 2015–2024)
      matrix results = J(10, 2, .)
      local i = 1

      foreach y of numlist 2015/2024 {
      matrix results[`i', 1] = `y' // year
      matrix results[`i', 2] = _b[`y'.year#1.treat] // π̂ₜ
      local ++i
      }

      * 5. Convert matrix to dataset for Step 2
      clear
      svmat results, names(col)
      rename c1 year
      rename c2 pi_hat

      * 6. Merge in year‐level weights
      merge 1:1 year using `wts'
      assert _merge == 3
      drop _merge

      7. Step 2 ...

      Comment

      Working...
      X