Sample 24764: Stratified random sample with replacement, unequal allocation
Select a random sample of data where the number of observations     
chosen from each group are of different sizes, with replacement.    
 
 Note:  
Method 1 uses PROC SURVEYSELECT which is part of the     
SAS/STAT package in Version 7 and above.  If you have    
Version 6 of SAS or do not license SAS/STAT see          
Method 2.
          These sample files and code examples are provided by SAS Institute
          Inc. "as is" without warranty of any kind, either express or implied, including
          but not limited to the implied warranties of merchantability and fitness for a
          particular purpose. Recipients acknowledge and agree that SAS Institute shall
          not be liable for any damages whatsoever arising out of their use of this material.
          In addition, SAS Institute will provide no support for the materials contained herein.
        
 
/* Create sample data base of student grade point averages from East High */
/* School, Grades 9 through 12, 100 or more students per grade.  Sampling */
/* will be done with replacement, so an individual student's score may be */
/* sampled more than once.                                                */
 
data EastHigh;
  format GPA 3.1;
  do Grade=9 to 12;
    do StudentID=1 to 100+int(201*ranuni(432098));
      GPA=2.0 + (2.1*ranuni(34280));
      output;
    end;
  end;
run;
 
/* Method 1: Use PROC SURVEYSELECT if you have SAS/STAT Version 7 or higher   */
/*                                                                            */
/* Sample 4 students from Grade 9 , 6 from Grade 10, and 10 from both Grades  */
/* 11 and 12.  N= is the number of observations to select from each group.    */
/* Use METHOD=URS to request unrestricted random sampling, which is selection */
/* with equal probability and with replacement.  The statement STRATA defines */
/* the variable that is used for grouping.  OUT= names the SAS data set that  */
/* will be created by the procedure.                                          */
 
proc surveyselect data=EastHigh method=urs n=(4, 6, 10, 10) out=sample;
  strata Grade;
run;
 
title 'Surveyselect Method';
proc print data=sample;
run;
 
 
 
/* Method 2: Use Base SAS if you do not have SAS/STAT Version 7 or higher       */
/*                                                                              */
/* Sample 4 students from Grade 9, 6 from Grade 10, and 10 from both Grades     */
/* 11 and 12. Use RANUNI to  generate the observation numbers to be sampled.    */
/* Note that the data set is sorted by Grade.  In a following DATA step, create */
/* a counter that increments with each iteration of the step and resets to 0    */
/* when the BY-Group changes.  For each BY-Group specify the maximum number     */
/* of observations to sample.  Output until the counter matches the maximum     */
/* number to output per BY-Group.                                               */
 
/* Determine the starting observation and ending observation for each grade, */
/* or BY-Group.  Use this information to determine the random observations   */
/* to sample from each BY-Group.                                             */
 
data bygroup_range;
  keep grade start stop;
  set easthigh;
  by grade;
  retain start;
  if first.grade then start=_N_;
  else if last.grade then do;
    stop=_N_;
    output;
 end;
run;
 
proc print data=bygroup_range;
  title 'Using Base SAS';
run;
 
/* RANUNI generates a random number between 0 and 1.  To randomly sample from */
/* within a specified numeric range that starts at one, multiply the RANUNI   */
/* result by the maximum of the range.  For numeric ranges that do not start  */
/* with one,  multiply the RANUNI result by the range of data values you want */
/* to capture (maximum - minimum) plus one and add the first integer below    */
/* the minimum to the product.                                                */
 
data grab;
  do i = 1 to 4;
    grade = 9;
    obs2sample = ceil(ranuni(1234)*292);
    output;
  end;
  do i = 1 to 6;
    grade = 10;
    obs2sample = ceil(ranuni(4321)*120) + 292;
    output;
  end;
  do i = 1 to 10;
    grade = 11;
    obs2sample = ceil(ranuni(6789)*166) + 412;
    output;
  end;
  do i = 1 to 10;
    grade = 12;
    obs2sample = ceil(ranuni(9876)*223) + 578;
    output;
  end;
run;
 
/* GRAB contains the value for the observation number to sample from EASTHIGH */
data final;
  set grab(keep= obs2sample);
  set easthigh point=obs2sample;
  output;
run;
 
proc print data=final;
  title 'Using DATA step';
run;
          These sample files and code examples are provided by SAS Institute
          Inc. "as is" without warranty of any kind, either express or implied, including
          but not limited to the implied warranties of merchantability and fitness for a
          particular purpose. Recipients acknowledge and agree that SAS Institute shall
          not be liable for any damages whatsoever arising out of their use of this material.
          In addition, SAS Institute will provide no support for the materials contained herein.
        
 
Surveyselect Method
                       Student    Number    Expected    Sampling
Obs    Grade    GPA       ID       Hits       Hits       Weight
  1       9     2.5      139         1      0.013699      73.0
  2       9     2.3      216         1      0.013699      73.0
  3       9     4.1      225         1      0.013699      73.0
  4       9     2.5      252         1      0.013699      73.0
  5      10     3.0        5         1      0.050000      20.0
  6      10     2.3       30         1      0.050000      20.0
  7      10     3.0       68         1      0.050000      20.0
  8      10     2.3       73         1      0.050000      20.0
  9      10     3.0       79         1      0.050000      20.0
 10      10     3.4       93         1      0.050000      20.0
 11      11     3.4       19         1      0.060241      16.6
 12      11     2.1       39         1      0.060241      16.6
 13      11     2.3       66         1      0.060241      16.6
 14      11     2.1       67         1      0.060241      16.6
 15      11     2.6       78         1      0.060241      16.6
 16      11     4.0       86         1      0.060241      16.6
 17      11     3.2      100         1      0.060241      16.6
 18      11     3.4      122         1      0.060241      16.6
 19      11     3.7      138         1      0.060241      16.6
 20      11     3.6      156         1      0.060241      16.6
 21      12     2.2       42         1      0.044843      22.3
 22      12     3.6       55         1      0.044843      22.3
 23      12     4.0       57         1      0.044843      22.3
 24      12     2.5       66         1      0.044843      22.3
 25      12     2.7      123         1      0.044843      22.3
 26      12     2.7      150         1      0.044843      22.3
 27      12     2.1      158         1      0.044843      22.3
 28      12     3.3      183         1      0.044843      22.3
 29      12     4.1      204         1      0.044843      22.3
 30      12     3.5      214         1      0.044843      22.3
Using Base SAS
Obs    Grade    start    stop
 1        9        1      292
 2       10      293      412
 3       11      413      578
 4       12      579      801
Using DATA step
                       Student
Obs    GPA    Grade       ID
  1    3.9       9        72
  2    2.8       9        27
  3    2.1       9       112
  4    3.5       9        29
  5    2.4      10        31
  6    2.6      10        11
  7    3.0      10         5
  8    3.8      10        13
  9    3.1      10        54
 10    2.9      10        18
 11    3.8      11         7
 12    3.7      11        76
 13    3.5      11        15
 14    2.2      11       151
 15    2.9      11       161
 16    2.2      11       123
 17    2.1      11        67
 18    2.5      11        62
 19    2.6      11        55
 20    2.4      11        85
 21    3.8      12        64
 22    3.3      12        79
 23    2.2      12        58
 24    2.9      12        98
 25    2.9      12        28
 26    3.9      12       160
 27    3.6      12         1
 28    2.1      12       114
 29    2.8      12       202
 30    2.4      12        53 
Select a random sample of data where the number of  
    observations chosen from each group are of different   
    sizes, with replacement.
| Type: | Sample | 
| Topic: | Analytics  ==>  Survey Sampling and Analysis SAS Reference  ==>  DATA Step SAS Reference  ==>  Procedures  ==>  SURVEYSELECT
  | 
| Date Modified: | 2005-12-08 11:34:31 | 
| Date Created: | 2004-09-30 14:09:11 | 
Operating System and Release Information
| SAS System | Base SAS | All | 6.12 | n/a | 
| SAS System | SAS/STAT | All | 6.12 | n/a |