Sample 24762: Stratified random sample with replacement, equal allocation
Select a specific number of observations from different
groups, where observations can be chosen more than once.
Note:
Method 1 uses PROC SURVEYSELECT which is part of the
SAS/STAT package in Version 7 and above. If you do not have
SAS/STAT licensed or if you are running Version 6 of SAS,
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. */
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: Using PROC SURVEYSELECT */
/* */
/* N= is the number of observations to select from */
/* each group. Use METHOD=URS. 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=5 out=sample;
strata Grade;
run;
title 'Method 1: PROC SURVEYSELECT';
proc print data=sample;
run;
/* Method 2: Using Base SAS */
/* */
/* When we sample with replacement, once an obs is selected it goes */
/* back into the pool of possible choices to be selected again. */
/* Instead of reading through the data set sequentially, we use the */
/* POINT= option on the SET statement to read observations directly. */
/* POINT= is incompatible with BY processing, so in order to take */
/* the sample within each BY-Group we'll simulate BY processing in a */
/* macro. */
data _null_;
set EastHigh end=eof;
by Grade;
/* On first observation of a BY-Group, increment counter, */
/* and create macro variable for it, FIRSTn */
if first.grade then do;
i+1;
call symput('first'||trim(left(i)),trim(left(_n_)));
end;
/* For last observation in a BY-Group, assign the value */
/* of the counter into a macro variable, LASTn */
if last.grade then do;
call symput('last'||trim(left(i)),trim(left(_n_)));
end;
/* If at the end of the data set, assign the total number of */
/* BY-Groups into a macro variable, TOTAL */
if eof then do;
call symput('total',trim(left(i)));
end;
run;
/* Macro code to take a simple random sample with replacement from */
/* each BY-Group, or strata */
%macro samp;
%do n=1 %to &total;
data strata&n;
set EastHigh(firstobs=&&first&n obs=&&last&n);
run;
data sample&n(drop=i);
choice=int(ranuni(36830)*n)+1;
set strata&n point=choice nobs=n;
i+1;
/* Enter the desired sample size, 5 in this case */
if i>5 then stop;
run;
%end;
data sample;
set
%do j=1 %to &total;
sample&j
%end;
;;
run;
%mend samp;
/* Invoke the macro SAMPLE */
%samp
proc sort data=sample;
by grade studentid;
run;
title 'Method 2: Using Base SAS';
proc print data=sample;
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.
Method 1: PROC SURVEYSELECT 14:27 Thursday, November 4, 2004 67
Student Number Expected Sampling
Obs Grade GPA ID Hits Hits Weight
1 9 2.2 97 1 0.017123 58.4
2 9 3.3 149 1 0.017123 58.4
3 9 2.9 164 1 0.017123 58.4
4 9 2.4 222 1 0.017123 58.4
5 9 3.2 223 1 0.017123 58.4
6 10 3.1 56 1 0.041667 24.0
7 10 2.9 58 1 0.041667 24.0
8 10 2.8 70 1 0.041667 24.0
9 10 3.5 92 1 0.041667 24.0
10 10 3.6 106 1 0.041667 24.0
11 11 4.0 86 1 0.030120 33.2
12 11 3.0 121 1 0.030120 33.2
13 11 2.5 135 1 0.030120 33.2
14 11 2.9 161 1 0.030120 33.2
15 11 3.1 165 1 0.030120 33.2
16 12 2.9 10 1 0.022422 44.6
17 12 3.0 56 1 0.022422 44.6
18 12 2.1 61 1 0.022422 44.6
19 12 2.3 96 1 0.022422 44.6
20 12 3.6 146 1 0.022422 44.6
Method 2: Using Base SAS 14:27 Thursday, November 4, 2004 68
Student
Obs GPA Grade ID
1 3.5 9 51
2 3.4 9 108
3 3.0 9 155
4 3.9 9 182
5 3.0 9 191
6 3.7 10 21
7 2.4 10 45
8 2.2 10 64
9 2.2 10 75
10 3.0 10 79
11 3.1 11 29
12 4.0 11 61
13 3.5 11 88
14 4.1 11 104
15 3.6 11 109
16 2.8 12 39
17 3.0 12 82
18 4.1 12 119
19 2.3 12 139
20 3.6 12 146
Select a specific number of observations from different
groups, where observations can be chosen more than once.
| Type: | Sample |
| Topic: | Analytics ==> Survey Sampling and Analysis SAS Reference ==> Procedures ==> SURVEYSELECT SAS Reference ==> DATA Step
|
| Date Modified: | 2005-12-08 11:34:31 |
| Date Created: | 2004-09-30 14:09:11 |
Operating System and Release Information
| SAS System | SAS/STAT | All | n/a | n/a |
| SAS System | Base SAS | All | n/a | n/a |