# 用SAS进行抽样

1、利用SURVEYSELECT过程进行等比例分层抽样
* 利用SURVEYSELECT过程对数据集进行等比例分层抽样;
PROC SURVEYSELECT DATA = students out = samp1 method = srs samprate = .5 seed = 9876;
STRATA class gender;
RUN;
* 查看分层抽样的结果;
PROC FREQ DATA = samp1;
TABLES class * gender /NOPERCENT NOROW NOCOL;
RUN;
2、利用SURVEYSELECT过程进行不等比例分层抽样
* 利用SURVEYSELECT过程对数据集进行等不比例分层抽样;
PROC SURVEYSELECT DATA = students out = samp2 method = srs samprate = (.4 .6 .4 .6 .4 .6) seed = 9876;
STRATA class gender;
RUN;
* 查看分层抽样的结果;
PROC FREQ DATA = samp2;
TABLES class * gender /NOPERCENT NOROW NOCOL;
RUN;
3、利用SURVEYSELECT过程根据抽样数量进行分层抽样
* 利用SURVEYSELECT过程对数据集进行指定数量的分层抽样;
PROC SURVEYSELECT DATA = students out = samp3 method = srs n = (8 4 6 8 5 7) seed = 9876;
STRATA class gender;
RUN;
* 查看分层抽样的结果;
PROC FREQ DATA = samp3;
TABLES class * gender /NOPERCENT NOROW NOCOL;
RUN;
4、利用随机数函数RANUNI对数据集进行粗略划分
* 利用RANUNI函数将数据集粗略的划分为N=5份;
DATA s1 s2 s3 s4 s5;
SET students;
r = RANUNI(991889);
IF r<0.2 THEN OUTPUT s1;
ELSE IF r<0.4 THEN OUTPUT s2;
ELSE IF r<0.6 THEN OUTPUT s3;
ELSE IF r<0.8 THEN OUTPUT s4;
ELSE OUTPUT s5;
DROP r;
RUN;
5、利用随机数函数RANUNI对数据集进行精确划分
* 根据数据集创建视图students_v,增加随机数列;
DATA students_v /view=students_v;
SET students;
srt = RANUNI(999890);
RUN;
* 按照随机数列对数据集进行排序,创建数据集students_srt,删除随机数列;
PROC SORT DATA = students_v OUT = students_srt(DROP = srt);
BY srt;
RUN;
* 将数据集精确地划分为N=5份;
DATA s1 s2 s3 s4 s5;
RETAIN per ;
SET students_srt NOBS= total;
IF _N_ = 1 THEN per = INT(total/5);
if _N_<= per then output s1;
ELSE IF _N_<= 2 * per THEN OUTPUT s2;
ELSE IF _N_<= 3 * per THEN OUTPUT s3;
ELSE IF _N_<= 4 * per THEN OUTPUT s4;
ELSE OUTPUT s5;
DROP per;