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 have SAS/STAT licensed, see
Methods 2 and 3 which use Base SAS.
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.
/* WORK.EASTHIGH is a 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 */
/* */
/* Use METHOD=SRS. N= is the number of observations to select. */
/* The sample is stored in the OUT= data set, SAMPLE1. */
proc surveyselect data=EastHigh method=srs n=15 out=sample1;
run;
title "Method 1: PROC SURVEYSELECT";
proc print data=sample1;
run;
/* Method 2: Using Base SAS */
/* Create a new variable X containing random numbers between 0 and 1 */
data random;
set EastHigh;
x=ranuni(1234);
run;
/* Sort on the random variable X */
proc sort data=random;
by x;
run;
/* Keep the first n observations. Since the data points are randomly */
/* sorted, these observations constitute a simple random sample. */
data sample2(drop=x);
set random (obs=15);
run;
title "Method 2: DATA step - with sort ";
proc print data=sample2;
run;
/* Method 3: Using SAS DATA Step with no sort required */
data sample3(drop=k n);
/* Initialize K to the number of sample obs needed and N to the */
/* total number of obs in the data set. */
retain k 15 n;
if _n_=1 then n=total;
set EastHigh nobs=total;
/* To randomly select the first observation for the sample, use the */
/* fact that each obs in the data set has an equal chance of being */
/* selected: k/n. If a random number between 0 and 1 is less than */
/* or equal to k/n, we select that the first obs for our sample */
/* and also adjust k and the number of obs needed to complete the */
/* sample. */
if ranuni(1230498) <= k/n then
do;
output;
k=k-1;
end;
/* At every iteration, adjust N, the number of obs left to */
/* sample from. */
n=n-1;
/* Once the desired number of sample points are taken, stop iterating */
if k=0 then stop;
run;
title "Method 3: DATA step, no sort ";
proc print data=sample3;
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 41 Student Obs GPA Grade ID 1 4.0 9 7 2 2.8 9 11 3 2.4 9 45 4 3.7 9 81 5 3.9 9 100 6 2.8 9 234 7 2.2 9 271 8 4.1 10 19 9 2.8 11 3 10 2.9 12 11 11 3.6 12 59 12 2.1 12 61 13 2.5 12 85 14 2.5 12 100 15 2.6 12 177 Method 2: DATA step - with sort 14:27 Thursday, November 4, 2004 42 Student Obs GPA Grade ID 1 2.2 11 49 2 2.8 9 27 3 2.7 11 80 4 2.0 12 184 5 2.9 9 101 6 4.0 12 102 7 2.4 9 127 8 2.7 9 93 9 2.8 12 144 10 3.0 12 32 11 2.0 11 5 12 2.7 12 141 13 4.1 12 195 14 2.8 9 234 15 3.8 12 122 Method 3: DATA step, no sort 14:27 Thursday, November 4, 2004 43 Student Obs GPA Grade ID 1 3.3 9 3 2 3.9 9 10 3 3.5 9 54 4 3.2 9 156 5 3.1 9 161 6 3.0 9 259 7 3.8 9 272 8 3.7 10 3 9 2.4 10 52 10 2.0 11 27 11 2.9 11 77 12 3.4 12 47 13 3.9 12 95 14 3.1 12 126 15 2.7 12 223
Type: | Sample |
Topic: | Analytics ==> Survey Sampling and Analysis SAS Reference ==> Procedures ==> SURVEYSELECT SAS Reference ==> DATA Step |
Date Modified: | 2015-09-28 16:58:53 |
Date Created: | 2004-09-30 14:09:07 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | All | 8 TS M0 | n/a |
Z64 | ||||
SAS System | SAS/STAT | Z64 |