Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Table from loop
From
Kye Lippold <[email protected]>
To
[email protected]
Subject
Re: st: Table from loop
Date
Tue, 1 Feb 2011 22:40:23 -0500
I would suggest using -file- to write the table, as it is probably the
most flexible solution. I assume you want to pull the results directly
from e(b) and write those to an external tab-separated file rather
than displaying your table in Stata itself.
I have been looking to get more practice with -file-, so your question
inspired me to write the code below. It is not necessarily the most
efficient solution, but you should find it easy to modify. Note this
code assumes that your "race" and "sex" variables have value labels,
that you want an Excel file as output, and that you want your
significance stars in the same cell as the difference, but all those
aspects are easy to change.
Kye Lippold
Research Assistant
The Urban Institute
*******************************
clear
cd `someplace' /* make sure you are in the directory where you want
your table */
webuse nhanes2f
svyset psuid [pweight=finalwgt], strata(stratid)
gen sample=(age<50)
/* Create separate sample indicators by race group (needed to run
regressions separately) */
forval r = 1/3 {
qui gen sample`r' = sample==1 & race==`r'
}
file open output using table.xls, write replace
/* Write the title and column headers */
file write output "Table of Means by Height and Weight" _n _tab
forval r = 1/3 {
file write output _tab "`: label race `r''" _tab _tab
}
file write output _n
file write output "Variable" _tab
forval i = 1/3 {
file write output "`: label sex 1'" _tab "`: label sex 2'" _tab "Diff" _tab
}
file write output _n
/* Write the table statistics */
foreach i of var height weight {
file write output "`: var label `i''" _tab
eststo clear
svy, subpop(sample): mean `i', over(race sex)
mat A = e(b)
/* Loop over the values of race as columns */
forval r = 1/3 {
file write output "`: display %9.2f A[1,`r'*2-1]'" _tab "`: display
%9.2f A[1,`r'*2]'" _tab
qui eststo: qui svy linearized, subpop(sample`r') : regress `i' sex
mat B = e(b)
qui test sex==0
loc t = r(p)
loc star = cond(`t'<.001,"***",cond(`t'<.01,"**",cond(`t'<.05,"*","")))
file write output "`: display %9.2f B[1,1]'`star'" _tab
}
file write output _n
/* Output the regression estimates in Stata (optional) */
esttab, label nodepvar nonumber title(Table for variable `i')
}
file close output
*******************************
On Mon, Jan 31, 2011 at 10:12 PM, Sergio Prada <[email protected]> wrote:
>
> Hi, I'd like to create a Table out of the following loop. I am using
> Stata 11 for Windows.
>
> *****************************
> clear
> webuse nhanes2f
> svyset psuid [pweight=finalwgt], strata(stratid)
> gen sample=(age<50)
>
> foreach i of var height weight {
> eststo clear
> svy, subpop(sample): mean `i', over(race sex)
> quietly bys race: eststo: quietly svy linearized, subpop(sample) :
> regress `i' sex
> esttab, label nodepvar nonumber title(Table for variable `i')
> }
> ********************************
> The table I'd like look like this (see below) with race in columns
> (White, Black and Other)
>
> White
> Male Female Diff
> Height 176.89 163.21 -13.68***
> Weight 79.26 63.98 -15.27***
>
> Any suggestions would be highly appreciated.
>
> Thanks,
>
>
> Sergio
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/