*! version 5.0 2Nov1997 * Written by Jonah B. Gelbach, Department of Economics, University of Maryland ************************************************************************************ * * This program calculates all cell means, differences, and differences * in differences needed to create difs-in-difs tables. * The syntax is as follows: * * . did varlist [if] , rows( row-variable ) columns( column-variable ) * * where row-variable is the variable allowed to change across the two * rows, column-variable is the variable allowed to change across the two * columns. varlist may have only one entry. ************************************************************************************ program define did2 local varlist "required existing max(1)" local if "optional" **** old options line **** **local options "ROWS(string) COLUMNS(string) Table(integer 1) Start(integer 1) ROWDIFS(string)"** **** old options line **** local options "ROWS(string) COLUMNS(string) ROWDIFS(string)" parse "`*'" parse "`varlist'", parse(" ") local number = 0 local colcoun = `start' - 1 local depvar = substr("`1'", 1, 6) if "`rowdifs'" == "" { local rowdifs "rowdif" } tempvar use quietly mark `use' `if' `in' quietly markout `use' `varlist' `rows' `columns' if "`if'" ~= "" { local if "`if' & `use' == 1 " } else { local if "if `use' == 1 " } local oldif "`if'" quietly summarize `rows' `if' local rowmin = _result(5) local rowmax = _result(6) quietly summarize `columns' `if' local colmin = _result(5) /* getting min value of columns variable* */ local colmax = _result(6) /* getting max value of columns variable* */ local rows2 = "`rowmin'" local rows1 = "`rowmax'" tempvar rowvar0 colvar0 rowvar1 colvar1 inter gen `rowvar1' = `rows' == `rowmax' /* making dummy for row variable equals its max */ gen `colvar1' = `columns' == `colmax' /* making dummy for col variable equals its max */ *gen `inter' = `rowvar1'*`colvar1' /* interacting row and column dummies */ gen `rowvar0' = `rowvar1' == 0 /* making dummy for row variable equals its min */ gen `colvar0' = `colvar1' == 0 /* making dummy for col variable equals its min */ if "`if'" ~= "" { local if "`if' & " } else { local if "if" } *di "`if'" quietly regress `varlist' `rowvar1' `rowvar0' `if' `colvar1' == 1, nocons /* holding colvar = max */ local numobs = _result(1) tempname b v matrix `b' = get(_b) matrix `v' = get(VCE) local b11 = `b'[1,1] /* Mean for row max, col max */ local b21 = `b'[1,2] /* Mean for row min, col max */ local s11 = sqrt(`v'[1,1]) /* SE for row max, col max */ local s21 = sqrt(`v'[2,2]) /* SE for row min, col max */ quietly regress `varlist' `rowvar1' `rowvar0' `if' `colvar0', nocons /* holding colvar = min */ local numobs = `numobs' + _result(1) matrix `b' = get(_b) matrix `v' = get(VCE) local b12 = `b'[1,1] /* Mean for row max, col min */ local b22 = `b'[1,2] /* Mean for row min, col min */ local s12 = sqrt(`v'[1,1]) /* SE for row max, col min */ local s22 = sqrt(`v'[2,2]) /* SE for row min, col min */ local b13 = `b11' - `b12' /* Differencing across column var, holding row at max */ local b23 = `b21' - `b22' /* Differencing across column var, holding row at min */ local b31 = `b11' - `b21' /* Differencing across row var, holding column at max */ local b32 = `b12' - `b22' /* Differencing across row var, holding column at min */ local b33 = `b31' - `b32' /* Difference in Differences Estimate */ local s13 = sqrt(`s11'^(2) + `s12'^(2)) /* SE for dif across column var, holding row at max */ local s23 = sqrt(`s21'^(2) + `s22'^(2)) /* SE for dif across column var, holding row at min */ local s31 = sqrt(`s11'^(2) + `s21'^(2)) /* SE for dif across row var, holding column at max */ local s32 = sqrt(`s12'^(2) + `s22'^(2)) /* SE for dif across row var, holding column at min */ local s33 = sqrt(`s31'^(2) + `s32'^(2)) /* SE for Difference in Differences Estimate */ display _newline _newline display "The difs-in-difs estimates will be computed with `numobs' observations." local i 1 ******************************************************** ******** Making Viewer-Friendly Table ******** ******************************************************** local r 1 local c 1 while `r' < 4 { while `c' < 4 { global s`r'`c' "`s`r'`c''" global b`r'`c' "`b`r'`c''" local b`r'`c' : display %9.3f `b`r'`c'' local s`r'`c' : display %9.3f `s`r'`c'' parse "`s`r'`c''", parse(" ") local s`r'`c' = "`1'" local c = `c' + 1 } /* ending columns loop */ local c 0 local r = `r' + 1 } /* ending rows loop */ di "tab `rows' `cols' `oldif'" tab `rows' `columns' `oldif' di _newline _newline di "Results for Dependent Variable *`varlist'*: " _newline di " C o l u m n V a r i a b l e" di " -------------------------------------------" di _column(17) "`columns' = `colmax' " _column(33) "`columns' = `colmin'" _column(49) "Differences" di "Row Variable:" di "-------------" di "`rows' = " _column(13) "`rowmax'" _column(17) `b11' _column(33) `b12' _column(49) `b13' di _column(17) "(`s11')" _column(33) "(`s12')" _column(49) "(`s13')" _newline di "`rows' = " _column(13) "`rowmin'" _column(17) `b21' _column(33) `b22' _column(49) `b23' di _column(17) "(`s21')" _column(33) "(`s22')" _column(49) "(`s23')" _newline di "Row Diff " _column(17) `b31' _column(33) `b32' _column(49) `b33' di _column(17) "(`s31')" _column(33) "(`s32')" _column(49) "(`s33')" _newline end * *old stuff for my purposes: * * ************************************************************************************** **** Beginning routine for first column (relative to textable) of current variable *** ************************************************************************************** * *local colcoun = 1 + `colcoun' *display _newline _newline * * *display ". *table begin `table' `colcoun' heading= `columns'=`colmax'" _newline *display "Holding column variable constant at `columns' == `colmax'" _newline *display "Row difference for column variable `columns' == `colmax' follows" _newline *hyphens, lines(3) *display " Mean | Estimate Std. Err. " *display "---------+--------------------------------------------------------------------" *while `i' <= 2 { * display "`depvar'`i'" _column(10) "| " `b`i'1' _column(25) `s`i'1' " . . . . `rows' == `rows`i''" * local i = `i' + 1 * } * *display "`rowdifs'" _column(10) "| " `b31' _column(25) `s31' " . . . . Difference" *display "------------------------------------------------------------------------------" _newline *display ". *table end" * * *************************************************************************************** **** Beginning routine for second column (relative to textable) of current variable *** *************************************************************************************** * *local i 1 *local colcoun = 1 + `colcoun' *display _newline _newline *display ". *table begin `table' `colcoun' heading= `columns'=`colmin'" _newline *display "Holding column variable constant at `columns' == `colmin'" _newline *display "Row difference for column variable `columns' == `colmin' follows" _newline *hyphens, lines(3) *display " Mean | Estimate Std. Err. " *display "---------+--------------------------------------------------------------------" *while `i' <= 2 { * display "`depvar'`i'" _column(10) "| " `b`i'2' _column(25) `s`i'2' " . . . . `rows' == `rows`i''"" * local i = `i' + 1 * } * *display "`rowdifs'" _column(10) "| " `b32' _column(25) `s32' " . . . . Difference" *display "------------------------------------------------------------------------------" _newline *display ". *table end" * * ************************************************************************************** **** Beginning routine for third column (relative to textable) of current variable *** ************************************************************************************** * *local i 1 *local colcoun = 1 + `colcoun' *display _newline _newline _newline *display ". *table begin `table' `colcoun' heading= (1)-(2)" _newline *display "Differences across columns:" _newline *hyphens, lines(3) *display " Mean | Estimate Std. Err. " *display "---------+--------------------------------------------------------------------" *while `i' <= 2 { * display "`depvar'`i'" _column(10) "| " `b`i'3' _column(25) `s`i'3' " . . . . `rows' == `rows`i''"" * local i = `i' + 1 * } * *display "`rowdifs'" _column(10) "| " `b33' _column(25) `s33' " . . . . Dif-in-difs" *display "------------------------------------------------------------------------------" _newline *display ". *table end" *