Have you ever wanted to list a selection of observations based on a
condition but only list say a subset of 10 obs of that condition?
If so, perhaps you've been frustrated with the fact that:
. sysuse auto
. list if foreign == 1 in 1/10
lists no observations because in the first 52 observations foreign == 0.
The -in- subsets the data before the -if- condition subsets the data.
This is the opposite in SAS:
/* WHERE subsets the data before OBS subsets the data */
PROC PRINT DATA= SASHELP.SHOES(WHERE=(STORES < 10) OBS = 10);
RUN;
So, the above code lists the first 10 observations where (STORES < 10).
I can't think of any situation where I would want to know how many
times a certain condition exists in the first X observations. Do others
ever need to know that?
I figured out a solution where Stata will subset the data to the
condition and then only list the range of observations I'm interested in:
. list if sum((foreign == 1)) <= 10
The "(foreign == 1)" inside the sum() creates a value equal to 1 when the
condition is true and then sum() creates a running sum of that. You can
use the sum() function to subset your data for other Stata commands.
You could get a range of observations as well:
. list if inrange(sum((foreign == 1)),2,11)
I may decide always to use this since:
. list if sum((foreign == 1)) <= 100
will also work despite the fact there aren't 100 observations in the data.
I'll never again get the error message:
Obs. nos. out of range
r(198);
My previous solution was to:
preserve
keep if foreign == 1
local nobs = 10
if _N < `nobs' local nobs = _N
list in 1/`nobs'
restore
Thoughts?
Dan Blanchette
Research Associate
Center of Entrepreneurship and Innovation
Duke University's Fuqua School of Business
Durham, NC USA
[email protected]
*
* 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/