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

st: What's the added value of having -in- subset the data before -if- does?

From   Dan Blanchette <>
Subject   st: What's the added value of having -in- subset the data before -if- does?
Date   Wed, 4 Feb 2009 10:27:52 -0500 (EST)

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 */


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

My previous solution was to:

keep  if foreign == 1
local nobs = 10
if  _N  < `nobs'  local nobs = _N
list in 1/`nobs'


Dan Blanchette
Research Associate
Center of Entrepreneurship and Innovation
Duke University's Fuqua School of Business
Durham, NC USA

*   For searches and help try:

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