I would like to run several linear regressions using categorical exposure variables and to output the results to an excel sheet.
The code below works fine when the exposure is continuous. However, for categorical exposures the code only outputs the first row of results rather for ever level of the exposure.
*Code which works for continuous exposures
sysuse auto.dta
describe
summ
postfile temp str40 exp str40 outcome adjusted N beta se lci uci pval using ///
"\test.dta", replace
foreach out in price weight {
foreach exp in i.rep78 {
foreach adjust in 1 {
if `adjust'==1 local adjusted "mpg"
xi: reg `out' `exp' `adjusted'
local N = e(N)
matrix table=r(table)
local beta = table[1,1]
local se = table[2,1]
local lci = table[5,1]
local uci = table[6,1]
local pval=table[4,1]
post temp ("`out'") ("`exp'") (`adjusted') (`N') (`beta') (`se') ///
(`lci') (`uci') (`pval')
}
}
}
postclose temp
use "\test.dta", clear
export excel using "\test.xlsx", firstrow(variables)
The above code only produces one row with estimates for the first level of rep78
when it should produce 4 rows (rep78
is a 5-level categorical variable).