asdocx: Export from Stata to Word, Excel, LaTeX & HTML › Forums › asdocx Forum › Problem with decimals in flexmat › Reply To: Problem with decimals in flexmat
Hello Ana Diaz,
You have mentioned a problem with decimal points in the subject, but in the text, you have mentioned that you have problems with brackets and stars.
In the following code, I have processed the macro to format the values as %9.3f. flexmat can format single values, but if the values involve text or symbols, you have to format the value for decimal points before combining it with other items, such as *, -, or +. I have also shown how to add brackets and *.
[code]
capture program drop balance_fe
program define balance_fe
version 15.1
syntax varlist [if] [in], Doc(name)
asdocx setfile, save(`doc’)
flexmat reset
flexmat addrow, data(\i, T=0 , \i, \i, T=1, \i,\i,\i) row(1) col(1)
flexmat addrow, data(Var, T , C , Diff, T , C , Diff) row(2) col(1)
flexmat addrow, data(\i, (mean) , (mean) , \i , (mean) , (mean) , \i) row(3) col(1)
local i = 4
local j = 5
* Loop through each variable and run the regression separately for entrada == 0 and entrada == 1
foreach var of varlist `varlist’ {
* Regression for entrada == 0
reg `var’ tratados PcD_siempre_req_ayuda i.localidad_cuidador if encuesta == 0
* Capture the coefficient for TRATADOS (this is the adjusted difference)
local coef_0 = _b[tratados]
local se_0 = _se[tratados]
* decimal points
loc coef_0 : dis %9.3f = `coef_0′
loc se_0 : dis %9.3f = `se_0′
local p0 =2*ttail(e(df_r),abs(`coef_0′ / `se_0′))
if `p0′<=0.01 {
local star0 “***”
}
else if `p0′<=0.05{
local star0 “**”
}
else if `p0′<=0.1{
local star0 “*”
}
else {
local star0 ” ”
}
local star0 `star0′
* Calculate the mean for treatment and control groups from the regression for entrada == 0
local intercept_0 = _b[_cons]
local mean_treated_0 = `intercept_0′ + `coef_0′
local mean_control_0 = `intercept_0′
* decimal points
loc intercept_0 : dis %9.3f = `intercept_0′
loc mean_treated_0 : dis %9.3f = `mean_treated_0′
loc mean_control_0 : dis %9.3f = `mean_control_0′
* Regression for entrada == 1
reg `var’ tratados PcD_siempre_req_ayuda i.localidad_cuidador if encuesta == 1
* Capture the coefficient for TRATADOS (this is the adjusted difference)
local coef_1 = _b[tratados]
local se_1 = _se[tratados]
local p1 =2*ttail(e(df_r),abs(`coef_1′ / `se_1′))
* decimal points
loc coef_1 : dis %9.3f = `coef_1′
loc se_1 : dis %9.3f = `se_1′
loc p1 : dis %9.4f = `p1′
if `p1′<=0.01 {
local star1 “***”
}
else if `p1′<=0.05{
local star1 “**”
}
else if `p1′<=0.1{
local star1 “*”
}
else {
local star1 ” ”
}
local star1 `star1′
* Calculate the mean for treatment and control groups from the regression for entrada == 1
local intercept_1 = _b[_cons]
local mean_treated_1 = `intercept_1′ + `coef_1′
local mean_control_1 = `intercept_1′
* decimal points
loc intercept_1 : dis %9.3f = `intercept_1′
loc mean_treated_1 : dis %9.3f = `mean_treated_1′
loc mean_control_1 : dis %9.3f = `mean_control_1′
* Create a table with asdoc, combining results for entrada == 0 and entrada == 1
flexmat addrow, data(`var’, `mean_treated_0′, `mean_control_0′, `coef_0′ `star0′, `mean_treated_1′, `mean_control_1′, `coef_1′ \i `star1′) row(`i’) col(1) dec(3)
flexmat addrow, data(\i, \i, \i, [`se_0”], \i, \i, [`se_1′] ) row(`j’) col(1) dec(3)
local i = `i’+2
local j = `j’+2
}
asdocx export
end
global attributes_cuid female_cuidador
balance_fe $attributes_cuid, doc(opinion)
[/code]