Hi List,
I am rather new to programming in stata and just read Nick Cox's
"Suggestions on Stata programming style" (which I can really
recommand for newbies) in the last issue of the Stata Journal. He
urges programmers to avoid "presere" if possible. This suggestions
makes sense to me, but I am struggeling on how to avoid it.
My program makes frequent use of the "collapse" command, which of
course changes the user's data, so it has to be restored each time. I
wonder wheter there is an elegant way that obviates using collapse()
(or similarly statsby() which uses collapse()).
Here is an example of my problem:
use http://www.stata-press.com/data/r9/invest2.dta, clear
tsset company time
local pvar "`r(panelvar)'"
Assume my "varlist" contains variables like invest, market and stock.
The task is to create a matrix that contains for each panel unit (i.e.
company) the means of the varibales in `varlist' over the time
periods. What my program does:
qui collapse (mean) `varlist', by("`pvar'") fast
qui mkmat `varlist', matrix(`X')
That works well but I have to use "restore" now to go on. The natural
thing to me was to look for a command like:
bys "`pvar'": sum `varlist' meansonly
but the return list does not allow me to grab the means by unit. So I
thought how about:
tabstat `varlist', by("`pvar'") statis(mean) save
this gets me what i want on the screen, but the return list r()
results only refer to each row of the summary table, not the table as
a whole. I could loop through these individual rows and collect the
matrix of course, but that takes long and seems suboptimal (exp. when
I have a lot of units).
Rather what I am looking for is something like the table , matcell()
option but for tabstat, i.e. a command that grabs the table displayed
on the screen and puts it in a matrix.
Any suggestions? One complexcification may be that apart from getting
the means over the time periods in other parts of my program i need to
apply several aggregation functions the sd, quantiles, etc. But I
would be happy if I could for now just find a elegant solution to get
the means.
Thanks a lot!
Tom
*
* 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/