Please consider the following example which aims to summarize the essence of a problem a friend asked me to help with. I couldn't! Hence this post.
The ultimate goal is to export nicely-formatted tables to Excel or Word using -collect-, ideally with the 2 agegrp tables in one docx (or one Excel sheet) and the 2 hlthstat tables in another docx (or another sheet of same Excel doc).
I can't see how to do this. My friend can get some of the way using -tabout- (Ian Watson, SSC), as follows:
I've tried to use -collect- and not got far. E.g., for one tabulation
This provides the basic raw material for one of the 4 tables.
I looked at
which was not obviously helpful (how does one "see" what is put into each dimension?)
I also experimented:
Either set of output seems to be the place to start playing with -collect style- and other things to get it looking more like the -tabout- examples. But I made no progress. I can't find any -svytab- examples in the [T] Tables manual, nor relevant Statalist posts for this command. Nor was general web searching of useful help.
Your advice and suggestions welcome -- thanks
Code:
sysuse nhanes2, clear
svyset
foreach var of varlist agegrp hlthstat {
svy: tab race `var' , row ci percent pearson
svy: tab sex `var' , row ci percent pearson
}
I can't see how to do this. My friend can get some of the way using -tabout- (Ian Watson, SSC), as follows:
Code:
capture ssc install tabout, replace
* although -tabout- writes to text files only, it's possible to later open the text file with Excel
* and get reasonably formatted tables (say "yes" when Excel asks whether you want to open the file)
* If the output file is being replaced, ensure that it's not open when redoing your code
foreach var of varlist agegrp hlthstat {
forval x = 0/1 {
tabout race sex `var' using `var'`x'.xls, ///
c(row ci) f(1 1) clab(Row_% 95%_CI) svy ///
stats(chi2) npos(lab) per replace
}
}
Code:
collect clear svy: tab race hlthstat, row percent ci collect get _r_b _r_ci
I looked at
Code:
collect dims
I also experimented:
Code:
* coefficient [lower_bound upper_bound] * results for row 1, then row 2, etc collect layout (colname) (result) * is same as: collect layout (colname) (result[_r_b _r_ci]) * coefficient * [lower_bound upper_bound] collect layout (colname##result)
Your advice and suggestions welcome -- thanks

Comment