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
Type: | Sample |
Topic: | Analytics ==> Survey Sampling and Analysis SAS Reference ==> Procedures ==> SURVEYSELECT SAS Reference ==> DATA Step |
Date Modified: | 2016-01-11 16:06:51 |
Date Created: | 2004-09-30 14:09:11 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | Windows 7 Ultimate x64 | ||
Windows 7 Enterprise x64 | ||||
Windows 7 Home Premium 32 bit | ||||
Windows 7 Home Premium x64 | ||||
Windows 7 Professional 32 bit | ||||
Windows 7 Enterprise 32 bit | ||||
Microsoft Windows Server 2012 Std | ||||
Microsoft Windows XP Professional | ||||
Microsoft Windows Server 2012 R2 Datacenter | ||||
Microsoft Windows Server 2012 R2 Std | ||||
Windows 7 Ultimate 32 bit | ||||
Microsoft Windows Server 2012 Datacenter | ||||
Microsoft Windows Server 2008 for x64 | ||||
Microsoft Windows Server 2008 R2 | ||||
Microsoft Windows Server 2008 | ||||
Microsoft Windows Server 2003 for x64 | ||||
Microsoft Windows Server 2003 Enterprise Edition | ||||
Microsoft Windows Server 2003 Standard Edition | ||||
Microsoft Windows Server 2003 Datacenter Edition | ||||
Microsoft Windows NT Workstation | ||||
Microsoft Windows 2000 Professional | ||||
Microsoft Windows 2000 Server | ||||
Microsoft Windows 2000 Datacenter Server | ||||
Microsoft Windows 2000 Advanced Server | ||||
Microsoft Windows 95/98 | ||||
Microsoft Windows 10 | ||||
Microsoft Windows 8.1 Pro 32-bit | ||||
Microsoft Windows 8.1 Pro | ||||
Microsoft Windows 8.1 Enterprise x64 | ||||
Microsoft Windows 8.1 Enterprise 32-bit | ||||
Microsoft Windows 8 Pro x64 | ||||
Microsoft Windows 8 Pro 32-bit | ||||
Microsoft Windows 8 Enterprise x64 | ||||
Microsoft Windows 8 Enterprise 32-bit | ||||
OS/2 | ||||
Microsoft® Windows® for x64 | ||||
Microsoft Windows XP 64-bit Edition | ||||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | ||||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | ||||
Microsoft® Windows® for 64-Bit Itanium-based Systems | ||||
OpenVMS VAX | ||||
Teradata on Linux | ||||
z/OS | ||||
z/OS 64-bit | ||||
Netezza TwinFin 64-bit S-Blades | ||||
Netezza TwinFin 64-bit SMP Hosts | ||||
Netezza TwinFin 32-bit SMP Hosts | ||||
DB2 Universal Database on Linux x64 | ||||
Greenplum on Linux x64 | ||||
Netezza TwinFin 32bit blade | ||||
Windows 7 Professional x64 | ||||
Aster Data nCluster on Linux x64 | ||||
DB2 Universal Database on AIX | ||||
Windows Millennium Edition (Me) | ||||
Windows Vista | ||||
Windows Vista for x64 | ||||
64-bit Enabled AIX | ||||
64-bit Enabled HP-UX | ||||
64-bit Enabled Solaris | ||||
ABI+ for Intel Architecture | ||||
AIX | ||||
HP-UX | ||||
HP-UX IPF | ||||
IRIX | ||||
Linux | ||||
Linux for x64 | ||||
Linux on Itanium | ||||
OpenVMS Alpha | ||||
OpenVMS on HP Integrity | ||||
Solaris | ||||
Solaris for x64 | ||||
Tru64 UNIX | ||||
SAS System | SAS/STAT | z/OS | ||
z/OS 64-bit | ||||
OpenVMS VAX | ||||
Microsoft® Windows® for 64-Bit Itanium-based Systems | ||||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | ||||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | ||||
Microsoft Windows XP 64-bit Edition | ||||
Microsoft® Windows® for x64 | ||||
OS/2 | ||||
Microsoft Windows 8 Enterprise 32-bit | ||||
Microsoft Windows 8 Enterprise x64 | ||||
Microsoft Windows 8 Pro 32-bit | ||||
Microsoft Windows 8 Pro x64 | ||||
Microsoft Windows 8.1 Enterprise 32-bit | ||||
Microsoft Windows 8.1 Enterprise x64 | ||||
Microsoft Windows 8.1 Pro | ||||
Microsoft Windows 8.1 Pro 32-bit | ||||
Microsoft Windows 10 | ||||
Microsoft Windows 95/98 | ||||
Microsoft Windows 2000 Advanced Server | ||||
Microsoft Windows 2000 Datacenter Server | ||||
Microsoft Windows 2000 Server | ||||
Microsoft Windows 2000 Professional | ||||
Microsoft Windows NT Workstation | ||||
Microsoft Windows Server 2003 Datacenter Edition | ||||
Microsoft Windows Server 2003 Enterprise Edition | ||||
Microsoft Windows Server 2003 Standard Edition | ||||
Microsoft Windows Server 2003 for x64 | ||||
Microsoft Windows Server 2008 | ||||
Microsoft Windows Server 2008 R2 | ||||
Microsoft Windows Server 2008 for x64 | ||||
Microsoft Windows Server 2012 Datacenter | ||||
Microsoft Windows Server 2012 R2 Datacenter | ||||
Microsoft Windows Server 2012 R2 Std | ||||
Microsoft Windows Server 2012 Std | ||||
Microsoft Windows XP Professional | ||||
Windows 7 Enterprise 32 bit | ||||
Windows 7 Enterprise x64 | ||||
Windows 7 Home Premium 32 bit | ||||
Windows 7 Home Premium x64 | ||||
Windows 7 Professional 32 bit | ||||
Windows 7 Professional x64 | ||||
Windows 7 Ultimate 32 bit | ||||
Windows 7 Ultimate x64 | ||||
Windows Millennium Edition (Me) | ||||
Windows Vista | ||||
Windows Vista for x64 | ||||
64-bit Enabled AIX | ||||
64-bit Enabled HP-UX | ||||
64-bit Enabled Solaris | ||||
ABI+ for Intel Architecture | ||||
AIX | ||||
HP-UX | ||||
HP-UX IPF | ||||
IRIX | ||||
Linux | ||||
Linux for x64 | ||||
Linux on Itanium | ||||
OpenVMS Alpha | ||||
OpenVMS on HP Integrity | ||||
Solaris | ||||
Solaris for x64 | ||||
Tru64 UNIX |