* ======================================================================= * File: random_sample.SPS . * Date: 31-Jan-2003 . * Author: Bruce Weaver, weaverb@mcmaster.ca . * Notes: Randomly sample X of N records . * ======================================================================= . * For this example, let N = 500 total cases, and suppose * we want to draw a random sample of 25 cases. * First, use LOOP to generate the 500 cases. new file. input program. loop #i = 1 to 500. /* Put YOUR value of N where the 500 appears. compute X = 25. /* Replace 25 with the number of cases you wish to select. compute case = $casenum. end case. end loop. end file. end input program. exe. format X case(f8.0). show seed. * Default value of random number seed = 2,000,000. * You should either set it equal to a value of your own * choosing, or use SET SEED RANDOM, as below. set seed random. show seed. * Now compute random numbers using the RV.UNIFORM function. COMPUTE randnum = RV.UNIFORM(0,1) . EXECUTE . format randnum(f8.6). * Sort the N cases in ascending order of the random numbers. * Then flag the first X cases as ones you want to use--i.e., * the cases with case number (after sorting) LESS THAN OR EQUAL to X. sort cases by randnum. compute #newcase = $casenum. compute flag = (#newcase LE X). exe. format flag(f1.0). val lab flag 0 '0- Not selected' 1 '1- Selected'. * Verify that X cases were selected. freq flag. * Re-sort to original order . * Then list the CASE NUMBERS of the selected cases . sort cases by case(a). TEMPORARY. select if flag. /* "if flag" means "if (flag=1)", because flag is a 0/1 variable. list case to flag. * Note that you could replace the LIST command above with a * SAVE OUTFILE command if you wanted to save the list of selected * cases to a data file. * Finished. * ======================================================================= .