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

st: AW: -collapse- with no observations?

From   "Martin Weiss" <>
To   <>
Subject   st: AW: -collapse- with no observations?
Date   Fri, 2 Oct 2009 13:09:48 +0200


Another aspect to this issue is that counts are more easily obtained via
-contract- which features a -zero- option. As it turns out, though, the -if-
qualifier is evaluated first, and the -zero- option only pertains to the
combinations of the -varlist- supplied to -contract-, not to the existence
of any variables to work on...

sysuse auto, clear
sum price
contract mpg  /* 
*/ if price < `r(min)', /* 
*/ zero


-----Ursprüngliche Nachricht-----
[] Im Auftrag von Neil Shephard
Gesendet: Freitag, 2. Oktober 2009 12:30
Betreff: st: -collapse- with no observations?


I've encountered some unexpected behaviour whilst using -collapse- on
different subsets of data.  An example to demonstrate the problem....

sysuse auto, clear
sum price
collapse (count) n = mpg if(price < 2000)

This results in an error as there are no cars with price < 2000...

. sysuse auto, clear
(1978 Automobile Data)

. sum price

    Variable |       Obs        Mean    Std. Dev.       Min        Max
       price |        74    6165.257    2949.496       3291      15906

. collapse (count) n = mpg if price < 2000
no observations

I would have expected the command to run and return a count of zero
(0) as this is a valid number of counts (to my mind at least).

There's a mention at that
indicates that trying to open the data editor with an if qualifier
that resulted in zero observations used to cause Stata to crash, but
that has been fixed.

Searching the archives/FAQ I haven't been able to find whether this
has has been discussed before, or is a reasonable behaviour (as I say
I would have expected to get a count of zero returned).


"The combination of some data and an aching desire for an answer does
not ensure that a reasonable answer can be extracted from a given body
of data." ~ John Tukey (1986), "Sunset salvo". The American
Statistician 40(1).

Email -
Website -
Photos -
*   For searches and help try:

*   For searches and help try:

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