The downside to that solution:
. bysort foreign: list if foreign & _n <= 10
is that the -bysort- will change the sort order of the
dataset (in other datasets other than the auto.dta data
since the auto data just happens to be sorted by foreign.
It also might be the case that the condition you are
interested in is not sort order related. This will list
up to 10 obs per value of mpg that is less than 20:
. bysort mpg: list if mpg < 20 & _n < 10
Also, I realized that since sum() creates a running
sum this will not just list the first 10 observations
because 0 (false) is also less than 10. So this:
. list if sum((foreign == 1)) <= 10
should be:
. list if sum((foreign == 1)) <= 10 & foreign == 1
or:
. list if inrange(sum((foreign == 1)),2,11) & foreign == 1
A better example to show how the running sum also requires
the condition to be repeated would be:
//not correct because it will list observations after the
// first instance where (mpg < 20) until the sum reaches 10
. list if inrange(sum((mpg < 23)),1,10)
//correct
. list if inrange(sum((mpg < 20)),1,10) & mpg < 20
So you have to repeat the condition to subset the dataset to
just those observations before starting the running sum.
Yet another learning experience.
Dan Blanchette
Research Associate
Center of Entrepreneurship and Innovation
Duke University's Fuqua School of Business
Durham, NC USA
[email protected]
From "Martin Weiss" <[email protected]>
To <[email protected]>
Subject st: AW: What's the added value of having -in- subset the data before -if- does?
Date Wed, 4 Feb 2009 17:01:33 +0100
<>
*************
bys for: list if foreign &_n<=10
*************
HTH
Martin
*
* 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/