help randomselect Sean Higgins

Title

randomselect -- Randomly selects observations and marks them with a dummy variable

Syntax

randomselect [varlist] [if] [in] , gen(newvarname) [options]

gen(newvarname) Name of new variable that =1 if selected

n(real) Number of observations or units to select

prop(real) Proportion (between 0 and 1) of observations or units to select

select(varname) Select units of varname rather than observations

seed(real) Sets the seed; see seed

Description

randomselect randomly selects observations and marks them with a dummy variable.
It differs from sample in that it does not drop the non-selected observations from
the data set, and that either individual observations or other units can be randomly
selected. (For example, if each observation in your data set is a household, and each
household lives in a district, you could randomly select some number or portion
of the districts using
the select(varname) option. This would select all households within each
of the randomly selected districts.)

The user must specify a name for the dummy variable that equals 1 for randomly selected
observations and 0 otherwise using the gen(newvarname) option. To randomly
select n observations or other units,
specify n(n); to select a proportion p
of observations or other units, where 0<p<1, specify prop(p).

To ensure that the randomly selected observations or units are the same during
subsequent runs of the do file, the seed can be set using the seed(real) option.
See seed.

Examples

. sysuse census

Select 10 observations (states)

. randomselect, gen(selected1) n(10)

. tab selected1

Select half of the states in the North-Central region

. randomselect if region==2, gen(selected2) prop(0.5)

. tab selected2

. tab selected2 if region==2

Select one region (i.e. all states in one region)

. randomselect, gen(selected3) select(region) n(1)

. tab region selected3

Author

Sean Higgins, UC Berkeley, seanhiggins@berkeley.edu