Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Eric Booth <eric.a.booth@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Formatted 3-way tables |

Date |
Tue, 27 Mar 2012 10:07:50 -0500 |

<> On Mar 27, 2012, at 2:52 AM, Richard Moverare wrote: > Eric, David, > > thank you for your replies! Both -tab3way- and -tablecol- seem to be > doing what I want to do. But it seems a bit difficult to use -logout- > in order to get a result that doesn't need further editing. I don't have a lot of experience using -logout-, but perhaps if you provide specific details/examples of the difficulties you are having someone here could help. > Thanks also for the information about using loops in -tabout-, I don't > understand how I didn't see that. However, as I understand it, it is > not a solution if you want supercolumns, only when you add more row, > or do I miss something here? > I do use a similar method to add 'super columns' only in the sense that I create a combined cross- or column variable (with -egen-, as mentioned in my previous message), label that variable with the combined categories, and then use that combined variable as the 'by' variable in -tabout- as the cross-var. This method doesn't stack the column labels like[1]: Supercol1 label Supercol2 label sublabel 1 sublabel2 sublabel3 sublabel4 instead, this method produces tables with the combined columns variable like this: Supercol1-sublabel1 Supercol1-sublabel2 Supercol2-sublabel3 Supercol2-sublabel4 which is the same information but with different column label formats. Here is an example of what I'm talking about. The first examples below are simple versions of two and three way tables with -tabout-. The last example automates this process, as I described before, including adding the labels to the new combined cross-variable (called a "by" variable below). Also, as I previously mentioned, if you've got tons of tables to produce, you can include all those variables in the loop (notice that I create a table for each combination of cross-variables for every other numeric variable in the -auto.dta- … you could use this to mass-produce your tables for many variables). I often use a version of this code to produce many tables with combined cross-vars -- you'll have to adapt the code to your data[2]. ******************************* ******************************* watch for wrapping issues below: *----make some fake data: sysuse auto, clear recode rep78 (.=0) lab def r 0 "zero" 1 "one" 2 "two" 3 "three" 4 "four" 5 "five" lab val rep78 r fre rep78 *--create more BY vars: clonevar rep782 = rep78 clonevar foreign2 = foreign ******************************* *----1 twoway table //foreign table trunk foreign, row col miss tabout trunk foreign using "twoway.xls", c(freq) replace //rep78 table trunk rep78, row col miss tabout trunk rep78 using "twoway.xls", c(freq) append **all the twoway tables in a loop: cap rm "twoway_loop.xls"= foreach x in rep78 foreign rep782 foreign2 { tabout trunk `x' using "twoway_loop.xls", c(freq) append } ******************************* *-----2. combine the "by" vars to create N-way tables: **simple example (no automation): egen test = group(foreign rep78), missing ta foreign rep78 ta test lab def tlab 1 "domestic-zero" 2 "domestic-one" 3 "domestic-two" 4 "domestic-three" 5 "domestic-four" 6 "domestic-five" 7 "For-one" 8 "For-three" 9 "For-four" 10 "For-Five", modify lab val test tlab fre test table trunk test, row col miss tabout trunk test using "threeway_test.xls", replace c(freq) drop test lab drop tlab /* THIS PROCESS OF BUILDING VAR LABELS FOR THE SUPER-COLS COULD BE TEDIOUS, A SOLUTION IS TO DO THIS IN A LOOP==> */ **put all the combinations of supercols in this list: cap rm "threeway_tabouts_allvars.xls" /* NOTICE that you put the pairs of variables you want combined to create a cross- or column variable in the local macro 'vars' below -- so you can repeat variables that you want paired with something else. In 'vars', rep78 & foreign are combined, then rep782 and foreign2...*/ loc vars rep78 foreign rep782 foreign2 foreign foreign2 rep78 foreign2 token `"`vars'"' while `"`1'"' != "" { di `"ByVars: `1' and `2'"' *--combine cross-variables tempvar x egen `x' = group(`1' `2') lab var `x' `"`1' and `2'"' levelsof `1', loc(one) levelsof `2', loc(two) loc w1 `"`:val lab `1''"' loc w2 `"`:val lab `2''"' loc build "" loc i = 1 *--build labels for supercol cross-vars foreach o in `one' { foreach t in `two' { *--account for missing pairs of By vars: qui su `1' if `1' == `o' & `2' == `t' if `r(N)' != 0 loc build `"`build' `i' `"`:lab `w1' `o''-`:lab `w2' `t''"' "' if `r(N)' != 0 loc i = `i'+1 } //end.t } //end.o di in r `"`build'"' lab def `x'lab `build' ,modify lab val `x' `x'lab *--tabout for all other vars: /* change the next line to "local myvars myvar1...varN" if you want to specify all the variables in your dataset that you want tables for */ ds make `vars' __* , not foreach vv in `r(varlist)' { qui tabout `vv' `x' using "threeway_tabouts_allvars.xls", /// c(freq) append clab(Freq.) h1(`"`vv' BY the combined `:var l `x'' var "') } //end.vv mac shift 2 } //end.while ******************************* ******************************* - Eric __ Eric A. Booth Public Policy Research Institute Texas A&M University ebooth@ppri.tamu.edu Office: +979.845.6754 _____________ [1] But I suppose you could use the LaTeX options of -tabout- to get the supercol labels stacked like this (like Table 14 in Watson's PDF guide to -tabout- I pointed you to earlier), but I haven't toyed with this since I've always found the method I describe to work fine for me. [2] I'm sure there are ways to clean-up the code above to make it more efficient, but I use this template since it's easier for me to find/debug errors in the loop if the tables aren't coming out just right. * * 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/

**Follow-Ups**:**Re: st: Formatted 3-way tables***From:*Richard Moverare <richard.moverare@gmail.com>

**References**:**st: Formatted 3-way tables***From:*Richard Moverare <richard.moverare@gmail.com>

**Re: st: Formatted 3-way tables***From:*Eric Booth <eric.a.booth@gmail.com>

**RE: st: Formatted 3-way tables***From:*"David Radwin" <dradwin@mprinc.com>

**Re: st: Formatted 3-way tables***From:*Richard Moverare <richard.moverare@gmail.com>

- Prev by Date:
**RE: st: Formatted 3-way tables** - Next by Date:
**st: Questions relating to cases selection** - Previous by thread:
**RE: st: Formatted 3-way tables** - Next by thread:
**Re: st: Formatted 3-way tables** - Index(es):