Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

Re: st: Tabulate summary statistics by percentiles and save output

From   Nick Cox <>
To   "" <>
Subject   Re: st: Tabulate summary statistics by percentiles and save output
Date   Fri, 28 Dec 2012 17:59:21 +0000

One problem here is where you think Stata is looking for saved results such as r(p1). Either non-missing values are accessible because they were left behind by some _previous_ command; or Stata will use missing values, which in your case will result in lack of restriction on the observations used. It's not an error to refer to saved results that don't exist.

At least at the start of your first loop no such non-missing values are (likely to be) present.

You refer here to several user-written commands but in no case do you explain where they come from.


On 28 Dec 2012, at 11:43, annoporci <> wrote:

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)'),
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)'),
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&
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:

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