1. 在DATA步实现。
options
mprintsymbolgen
mlogic
;
%macro ransample_data(dsin,dsout,Replacement,SampleSize);
%if %upcase("&Replacement")="YES"
%then %do;
%*creating
random sample with replacement;
data
&dsout(drop=i);
do
i=1to
&SampleSize;
PickIt=ceil(ranuni(123)*TotObs);
ObsPicked=PickIt;
set &dsin
point=PickIt
nobs=TotObs;
output;
end;
stop;
run;
%end;
%else
%if %upcase("&Replacement")="NO"
%then %do;
%*
create random sample without replacement;
data &dsout(drop=SampleSize
ObsLeft);
SampleSize=&SampleSize;
ObsLeft=TotObs;
do
while(SampleSize>0and
ObsLeft>0)
;
PickIt+1;
if ranuni(123)<SampleSize/ObsLeft
then
do;
ObsPicked=PickIt;
set
&dsin point=PickIt nobs=TotObs;
output;
SampleSize=SampleSize-1;
end;
ObsLeft=ObsLeft-1;
end;
stop;
run;
%end;
%else
%putERROR:Replacement
shout be yes or no ;
%mendransample_data;
%ransample_data(sashelp.class,
class_data_rep,yes,10);
%ransample_data(sashelp.class,
class_data_norep,no,10);
%ransample_data(sashelp.class,
class_data_norep,whatever,10);
2.用proc serveyselect 实现
%macro ransample_proc(dsin,dsout,Replacement,SampleSize);
%if %upcase("&Replacement")="YES"
%then %do;
proc surveyselect data=&dsin
out=&dsout method=urs
n=&SampleSize;
run;
%end;
%else %if
%upcase("&Replacement")="NO"
%then %do;
proc surveyselect data=&dsin
out=&dsout method=srs
n=&SampleSize;
run;
%end;
%else %put
ERROR: Replacement shout be yes or no
;
%mendransample_proc;
%ransample_proc(sashelp.class,class_proc_rep,yes,10);
%ransample_proc(sashelp.class,class_proc_norep,no,10);
%ransample_proc(sashelp.class,class_proc_norep,whatever,10);