{smcl} {* 19Nov2008}{...} {cmd:help wfex_sample}: Examples of selecting observations {hline} {title:List of examples} 1. Retain only cases with no missing values for a set of variables. 2. Selecting a random sub-sample. 2. Creating training and confirmation samples. 3. Using an include file to load data and select a sample. 4. Creating an indicator if a variable is missing. 5. Creating sample selection variables. {title:1. Retaining only cases with no missing values for a set of variables} 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 {title:2. Selecting a random sub-sample} use wf-lfp, clear set seed 11020 generate isin = (uniform()>.80) // 20% subsample 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 {title:3. Creating 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 {title: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 {title:5. Creating 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 {title:6. Creating sample selection variables} // note: assumes no missing values for female 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 {p2colset 5 12 12 2} {title:Also see} {helpb wf:Workflow} INCLUDE help wf_footer