Tables with putexcel
This section requires some knowledge on objects in Stata. Make sure to check out the respective chapters in Courseware if you are not yet familiar with return lists, ereturn lists, macros, loops, or matrices.
Tables with putexcel
sysuse census, clear
help putexcel
putexcel set "Demographics.xlsx", sheet(Sheet1) replace // Defines file to create, modify or replace in subsequent putexcel commands
matrix d = J(6,5,.) // Creates matrix that will store results which will be exported
matrix colnames d = "N" "Mean" "SD" "Min" "Max"
local i = 1
foreach var of varlist pop* {
local name: variable label `var'
local k = `i' + 1
putexcel A`k' = ("`name'") // writes variable label in cell corresponding to the variable
qui sum `var', detail
mat d[`i',1] = r(N)
mat d[`i',2] = r(mean)
mat d[`i',3] = r(sd)
mat d[`i',4] = r(min)
mat d[`i',5] = r(max)
local i = `i'+1
}
putexcel B1 = matrix(d), colnames // matrix specifies that a Stata matrix is exported to Excel
putexcel B1:F1, border(bottom, medium, black)
putexcel C2:C7, nformat("number_sep")
putexcel D2:D7, nformat("number_d2")