Re: st: -collapse- with no observations?

 From Austin Nichols To statalist@hsphsun2.harvard.edu Subject Re: st: -collapse- with no observations? Date Fri, 2 Oct 2009 12:13:46 -0400

```Neil--
Maybe official -collapse- should be modified to accomodate if
qualifiers that are never true in the data, and return missing or zero
as appropriate, but -collapse- is just a shortcut for doing what you
could do by hand:

sysuse auto, clear
g m=mpg if price<2000
collapse (count) n = m
list
sysuse auto, clear
g n=sum(!mi(mpg) & price<2000)
keep in l
keep n
list

Either approach is byable:

sysuse auto, clear
g m=mpg if price<3500
collapse (count) n = m, by(foreign)
list
sysuse auto, clear
bys foreign:g n=sum(!mi(mpg) & price<3500)
by foreign:keep if _n==_N
keep n
list

On Fri, Oct 2, 2009 at 6:30 AM, Neil Shephard <nshephard@gmail.com> wrote:
> Hi,
>
> 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
> r(2000);
>
> 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 http://www.stata.com/help.cgi?whatsnew10 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).
>
> Neil
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```