Hi,
I want the code below to help produce an excel output with item list in the rows and corresponding means by quintile, mix, max and number. The output unfortunately includes the entire original dataset instead of aneat table. Can someone please advise? Thank you in advance!
I want the code below to help produce an excel output with item list in the rows and corresponding means by quintile, mix, max and number. The output unfortunately includes the entire original dataset instead of aneat table. Can someone please advise? Thank you in advance!
Code:
xtile q2004=wealth_index2004 if wealth_index2004!=., nq(5)
tempfile results
save `results', emptyok
local list2 red blue green black
foreach var of local list2 {
* means by quintile
foreach q in 1 2 3 4 5 {
quietly sum `var' if q2004== `q', meanonly
local mean`q'=r(mean)
}
*overall stats
quietly sum `var', meanonly
local mean_all = r(mean)
local minval = r(min)
local maxval = r(max)
local Nobs=r(N)
*create new dataset with sumary results
preserve
clear
set obs 1
gen model = "`var'"
gen Q1 = `mean1'
gen Q2 = `mean2'
gen Q3 = `mean3'
gen Q4 = `mean4'
gen Q5 = `mean5'
gen mean = `mean_all'
gen min = `minval'
gen max = `maxval'
gen N = `Nobs'
tempfile one
save `one'
* Append to growing results dataset
use `results', clear
append using `one'
save `results', replace
restore
}
use `results', clear
export excel using "summary_table.xlsx", firstrow (variables) replace

Comment