help wfegsample

Title

Stata commands for selecting a sample

List of examples

1. Selecting a random sub-sample.

2. Training and confirmation samples.

3. Using an include file to load data and select a sample.

4. Create an indicator if a variable is missing.

5. Creating sample selection variables.

1. Keep if none of a set of variables are missing

mark notmissing // create variable to indicate if not missing
markout notmissing var1 var2 ... varK // check these variables
label def notmissing 0 0_missing 1 1_nomiss
label val notmissing notmissing
tab notmissing, missing

2. Selecting a random sub-sample

use wf-lfp, clear
set seed 11020
generate isin = (uniform()>.8)
label var isin "1 if in random sample (seed 11020)"
label def isin 0 0_NoIn 1 1_InSample
label val isin isin
keep if isin
tabulate isin, missing

3. Training and confirmation samples

use wf-articles, clear
datasignature confirm
set seed 1102
generate train1 = (uniform() < .5)
label var train1 "Training sample?"
label def trainlbl 0 "0Confirm" 1 "1Train"
label val train1 trainlbl

4. Using an include file to load data and select a sample

// include: wf7-include-sample.doi
// used by: wf7-include-sample.do
// task: define sample for tenure example
// project: workflow chapter 7
// author: scott long \ 2008-04-09

// #1
// load data and select sample

use wf-tenure, clear
datasignature confirm
drop if year>=11 // drop cases with long time in rank
drop if prestige<1 // drop if unrated department

5. Create an indicator if a variable is missing

// indicator of whether a case has missing data for art
generate Mart = missing(art)
label var Mart "Is art missing?"
label def Lismiss 0 0_valid 1 1_missing
label val Mart Lismiss

6. Creating sample selection variables

generate sampfem = (female==1)
label var sampfem "Sample: females only"
generate sampmale = (female==0)
label var sampmale "Sample: males only"
label def Lsamp 0 0_Drop 1 1_InSample
label val sampfem Lsamp
label val sampmale Lsamp

Also see

wf