Bookmark and Share

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]

st: Tabulate summary statistics by percentiles and save output


From   annoporci <annoporci@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   st: Tabulate summary statistics by percentiles and save output
Date   Fri, 28 Dec 2012 19:43:44 +0800

Dear Satalist,

I wish to tabulate some summary statistics for some percentiles and to
export the tables to files in tex format. I have never done that before. I
have read the stata help about esttab, estpost, estout, tabout and tried
various things that failed.

My best shot so far. Consider:

   sysuse ibm,clear
   tsset date
   local variables ibm spx

/// Tabulate moments for different percentiles:

   foreach var of varlist `variables' {
     summarize `var' if inrange(`var',`=r(p1)',`=r(p10)'), detail
     summarize `var' if inrange(`var',`=r(p90)',`=r(p100)'), detail
   }


The result is displayed in the Stata window. For reference:
   p1-p10:   Obs = 493, Kurtosis = 5.05607
   p90-p100: Obs = 50,  Kurtosis = 3.619203


/// Export some summary satistics:

foreach var of varlist `variables' {
     quietly estpost summarize `var' if inrange(`var',`=r(p1)',`=r(p10)'),
detail
     esttab using moments`var'.tex, replace cells("count mean sd skewness
kurtosis") noobs nodis nonumbers nonotes
quietly estpost summarize `var' if inrange(`var',`=r(p90)',`=r(p100)'),
detail
     esttab using moments`var'.tex, append cells("count mean sd skewness
kurtosis") noobs nodis nonumbers nonotes
   }

There are two issues: one about the display, one about the data.

1) FIRST issue: my code creates two separate tables stacked on top of each
other. I would like instead that the second round of data for count, mean,
sd, skewness, kurtosis be "inserted" into the first table just below the
first round of data, something like:

                    &       count&        mean&          sd&    skewness&
kurtosis\\
\hline
ibm,    p1-p10    &         493&    .0461508&    1.231737&
.144965&     5.05607\\
ibm,    p90-p100  &         493&    .0461508&    1.231737&
.144965&     5.05607\\


2) SECOND issue: my loop is not correctly selecting the percentiles. As you
can see, the results for the two parts of the loop are identical.

In fact, the very first loop reported above is also incorrect. The total
number of observations in the dataset is 493, so p1-p10 should have fewer
than that and not 493 as reported.

I have tried playing around with preserve/restore, I have also tried
inserting "use ibm,replace" at various stages inside the loops, but
couldn't work it out.

I may be approaching this from the wrong angle. Any suggestions will be
much appreciated. Thanks.

---
Patrick Toche.
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index