# IFWINS

Subset dataset by if exp before subsetting by in range

If you want to use **ifwins**, then feel free to use Stata's command ssc install to download and install **ifwins**:

ssc install ifwins, replace

Disclaimer: There is no warranty on this software either expressed or implied. This program is released under the terms and conditions of GNU General Public License.

### About ifwins

Programmer: Dan Blanchette

The Carolina Population Center

The University of North Carolina

Chapel Hill, NC USA

The Carolina Population Center

The University of North Carolina

Chapel Hill, NC USA

Date: 10Feb2009

Last updated: 25Feb2009

Last updated: 25Feb2009

**Subset dataset by if exp before subsetting by in range**

` `**ifwins** [if *exp*] [in *range*] **:** *stata_cmd*

### Description

**ifwins**is a prefix command that runs most any Stata command that does not modify the dataset in memory (e.g. generate, replace, etc.).

**ifwins**will have if subset the dataset before in subsets the dataset. This is the opposite of what happens when both if and in are used in the same Stata command. For example, the following code will first subset the dataset to the first 10 observations and then subset the dataset to the specified condition:

Since the auto.dta dataset is sorted by the variable. sysuse auto . list if foreign == 1 in 1/10

**foreign**, the above code will not list any observations because in the first 10 observations

**foreign == 0**. So, if looses and in wins when "battling" over which one subsets the dataset before the other one does.

If you want to run a Stata command on a certain number of observations when a certain condition exists, you would have to:

or use. preserve . keep if foreign == 1 . list in 1/10 make turn weight . restore

**ifwins**as a prefix to the desired Stata command:

The above will list the first 10 observations of when the variable. ifwins if foreign == 1 in 1/10 : list make turn weight

**foreign**is equal to 1 (one). So now if wins!...but in is still helpful.

### Remarks

Since

The list, browse, edit commands are the only exceptions to the note above.

if and in are not allowed in the Stata command

**ifwins**(when not running the list, browse, edit commands) temporarily subsets the dataset before running the submitted Stata command the observations numbers will not be the same as in the original dataset. So, the system variables**_n**and**_N**as well as the c(N) macro variable are reset to the subsetted dataset before the specified Stata command is run by**ifwins**. Also,**ifwins**will never return the error message "Obs. nos. out of range r(198)" if the range specified is at least within the number of observations in the dataset.**ifwins**corrects ranges specified by in to at least 1 observation of the specified condition since it is hard to know how many observations will be available after the if condition subsets the dataset.The list, browse, edit commands are the only exceptions to the note above.

if and in are not allowed in the Stata command

**ifwins**is specified to run.

### Examples

. ifwins if mpg < 25 in -10/L : list make turn weight . ifwins if income > 1000000 in 1/200 : summarize x1 x2 x3 x4 year89 year90