Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: Exporting tabstat results to .txt (or other format) files


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: Exporting tabstat results to .txt (or other format) files
Date   Fri, 31 Mar 2006 10:31:33 +0100

I played a bit more with this. There is no help 
file, but many non-programmers should be able
to work out the syntax from the -syntax- 
statement a few lines below and a glance at
the code. The compulsory option -statistics()- 
may only contain statistics acceptable to 
-collapse-, as that is how they are calculated. 
-cw- is the corresponding option 
of -collapse-. The program uses -outfile- 
to export and takes -outfile- options, unless
-outsheet- is specified, in which case
-outsheet- is used and it takes -outsheet- 
options. 

-by()- is no longer compulsory. 

The -total- option applies if you also specified
-by()-, and says "I also want these statistics 
for the whole dataset". 

This is where I would appreciate volunteer testing
from Unix and Mac users. The -total- option is 
implemented in a crude but fairly simple way, by 
spitting out a separate temporary file and then 
using the operating system to put them together. 
I have guessed at a way to do this on Unix and on
Macs, but I need to know whether it works and if
not how to fix it, please.  

This program is indicative of what Stata user-programmers
can do with modest effort, and _not_ a really general 
program, and _not_ (despite the slightly coy name) a wrapper 
for -tabstat-, or an attempt at a full emulation of
-tabstat-. 

Example: 

sysuse auto, clear 
mytabstatexport turn trunk length using mytab.txt, s(mean sd) by(rep78) total 

*! NJC 1.1.0 31 March 2006 
program mytabstatexport
	version 8.2 
	syntax varlist(numeric) [if] [in] using/ ///
	[aweight fweight iweight pweight]        ///
	, Statistics(str) [ BY(varname) CW OUTSHEET TOTAL * ]

	marksample touse 
	if "`by'" != "" { 
		markout `touse' `by', strok 
		local byby "by(`by')" 
	}
		
	qui count if `touse' 
	if r(N) == 0 error 2000 

	local out = cond("`outsheet'" != "", "outsheet", "outfile") 

	preserve 
	foreach s of local statistics {
		local call "`call'(`s') " 
		foreach v of local varlist { 
			local call "`call'`v'`s' = `v' " 
		}
	} 

	qui collapse `call' if `touse' [`weight' `exp'], `cw' `byby' 
	
	if "`by'" == "" {
		tempvar by 
		gen `by' = _n      
	}
	
	qui reshape long `varlist', i(`by') j(stats) string 

	if "`byby'" == "" { 
		drop `by' 
	}	
	
	list, subvarname
	
	`out' using "`using'", `options' 

	if "`total'" != "" & "`byby'" != "" { 
		restore 
		preserve 
		marksample `touse' 
		markout `touse' `by', strok 
		qui collapse `call' if `touse' [`weight' `exp'], `cw' 
		tempvar tot
		gen `tot' = "Total"
		char `tot'[varname] `" "' 
		qui reshape long `varlist', i(`tot') j(stats) string 

		list, subvarname 
		
		tempfile total 
		qui `out' using `total', `options' 
		if "`c(os)'" == "Windows" { 
			shell copy `using' + `total' `using' 
		}
		else shell cat `total' >> `using' 
	}
end 	

If "Venable" would please reveal more of his 
or her name, I can use that in an Ack't in the
help when it gets written. 

Nick 
[email protected] 

Nick Cox
 
> Your example command has the form 
> 
> 	two or more variables 
> 
> BY 
> 
> 	one or more statistics
> 
> BY
> 
> 	categories of a variable. 
> 
> These requests are, logically, for several matrices, 
> not one. 
 
[...] 

[posting of rough initial version of mytabstatexport.ado] 
 
Venable
  
> > I use Stata 8.2 for Windows. I would like to export results from
> > tabstat to .txt files. The format in the Stata Results window, with
> > the statistics "stacked" over each other, is almost perfect (if I
> > could put standard deviations in parentheses, that would be 
> perfect),
> > but copying and pasting is time-consuming and could lead to errors,
> > plus it makes replication difficult.
> > 
> > The sort of tabstat command I would like to export is
> > 
> > tabstat wheat corn barley, stat(mean sd) by(year) save
> > 
> > I have tried -statsmat- and -tabstatmat-, but neither seems to work
> > quite right for what I want to do:
> > 
> > -statsmat-: using statsmat as a substitute for tabstat is 
> problematic,
> > because "by(byvar) is allowed only with a single varname in 
> varlist".
> > Furthermore, even when you use just one varname, the different
> > statistics (mean sd in my example) are displayed in columns rather
> > than one beneath the other (as in tabstat).
> > 
> > -tabstatmat-: this seems to have similar issues - I get a
> > "conformability error" with multiple varnames in the varlist; with
> > just one varname in the varlist, the statistics are returned in
> > separate columns rather than one under the other (this 
> occurs whether
> > or not I specify columns(variables) in the tabstat command)
> > 
> > I have also tried the -tablemat- command but this allows only 
> > one statistic.
> > 
> > The closest thing for what I would like to do seems to be 
> -latabstat-,
> > which exports beautifully to LaTeX tables - is there 
> something similar
> > for text files?
> > 
> > Does anyone have any advice? I apologize if this has been discussed
> > before - I did a quick search of the Statalist archives but did not
> > find anything addressing these points. Please let me know if I have
> > overlooked something.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index