Bookmark and Share

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]

st: combining tables


From   Rebecca Pope <[email protected]>
To   [email protected]
Subject   st: combining tables
Date   Tue, 30 Oct 2012 17:00:59 -0500

Several years ago, Nick Cox posted the text below in a post titled
"Tabling: an agenda". As far as I can tell searching the list and the
documentation, there hasn't been any progress on this agenda item as
of Stata 12. Does anyone know of any new official/user-written
commands that I've failed to discover?

Absent that, I've attempted to produce a combined table of frequencies
within the constraints as I understand them. Note, this is
task-specific, not as general as what Nick proposed below. I could not
figure out how to use the matrices from -tabulate, matcell() matrow()-
to display the results nicely. Instead, I'm using a series of -table-
commands, saving the results into a temporary data set (successive
-append-s), and using -tabdisp- for the end result. I can get decent
looking output of descriptive stats for all variables of interest in
one table, but it is an inelegant solution. I've included an example
using the auto data at the end of this e-mail so everyone can see my
approach. Is there a better way to produce the resulting table?

Regards,
Rebecca

--------------------
http://www.stata.com/statalist/archive/2003-10/msg00247.html
<snip>

Problem 2: combining tables
===========================

As Phil has clearly highlighted, one common need is to put together
what in effect sub-tables into combined tables. It could be argued
that Stata should not interfere between you and your word and text
processor; any way, at first sight it offers next to no tools for
doing this.

Available solutions: ... except that, in a sense, there is a bunch of
commands for joining tables so long as they are (expressible as) Stata
matrices. This line of attack is probably under-appreciated; at the
same time, it falls short of what I guess people often need here.

Required solutions: a whole mini-language for combining tables. In
effect tables could be seen as objects and there would be a set of
operations for combining them, with tunable control of output form:
e.g. join along rows; join along columns; layer. Each combining would
produce alignment, and be more than what anybody could do as a
cut/copy/paste exercise. I guess that this would be a substantial
project for Stata Corp. -graph combine- is a partial analogue.

(But there's more, such as elementwise addition, subtraction,
multiplication, division of tables...)

<snip>
------------------

set more off

sysuse auto.dta, clear
tempfile stats

/* Calculate table values */
	/* Total & price */
qui summ price
local total = `r(N)'

preserve
local lbl2: var label price
clear
set obs 2

gen charac = _n
gen freq = `total' in 1
gen pct = 100 in 1
gen place = 1 in 1
gen type = "" in 1

replace freq = `r(mean)' in 2
replace pct = `r(sd)' in 2
replace place = 2 in 2
replace type = "cont" in 2

save "`stats'"
restore

	/* rep78 */
preserve
local lbl3: var label rep78   /* preserve original variable label */
recode rep78 (1/2=3) (3=4) (4=5) /* not recoding 5 because combining 4 with 5 */
mvencode rep78, mv(6) /* preserve missing, label as such later */
table rep78, contents(freq) replace
rename rep78 charac
rename table1 freq
gen pct = freq/`total'*100
gen place = 3
gen type = "cat"

append using "`stats'"
save "`stats'", replace
restore

	/* foreign */
preserve
local lbl4: var label foreign
recode foreign (0=7) (1=8)
mvencode foreign, mv(9)

table foreign, contents(freq) replace
rename foreign charac
rename table1 freq
gen pct = freq/`total'*100
gen place = 4
gen type = "cat"

append using "`stats'"

/* Display combined results */
	/* Variable headings for table */
label var freq "Frequency"
label var charac "Characteristic"
label var pct "Percent"

	/* Combine all value labels for variables in table */
label def charac 1 "N" 2 "Mean in Dollars (S.D.)" ///
	3 "Poor/Fair" 4 "Average" 5 "Good/Excellent" 6 "Missing/Unknown" ///
	7 "Domestic" 8 "Foriegn" 9 "Missing/Unknown"
label val charac charac

	/* Give display names to value of place variable */
label def place 1 " " 2 "`lbl2'" 3 "`lbl3'" 4 "`lbl4'"
label val place place
label var place "Variable"

	/* Apply display formats */
tostring freq, format(%9.1gc) replace force
tostring pct, format(%09.1fc) replace force
replace pct = "(" + pct + ")" if type=="cont" /* because continuous
vars use S.D., not %*/

	/* Display table of statistics */
tabdisp charac, cellvar(freq pct) by(place) left concise
restore
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index