代码摘录自:《Getting Your Random Sample in Proc SQL》
libname yugao "C:/BOOKS" ;
data yugao.sample;
format rec_no 7.0 age 4.0 sex $6. ;
label rec_no = "Record Number / ID"
age = "Age in Years"
sex = "Gender"
norm01_rv = "RV N(0,1)"
uni01_rv = "RV U(0,1)" ;
do i=1 to 1000000 ; /* one million records */
rec_no = i ; /* unique record identifier */
norm01_rv = rannor(2736); /* Random Variable/Number: Normal(0,1) */
uni01_rv = ranuni(3627); /* Random Variable/Number: Uniform(0,1) */
if i le 300000 then do ; /* first 30% will be in "Male" group */
sex = "Male" ;
age = ROUND( 4*RANNOR(36) + 50 ) ; /* Normal(50,4) */
end;
else do ; /* the rest (70%) will be in 'Female' group */
sex = "Female" ;
age = ROUND( 4*RANNOR(36) + 40 ) ; /* Normal(40,4) */
end;
output;
end;
drop i;
run;
/* example of WHERE used in Proc SQL */
proc sql outobs=10;
select * from yugao.sample WHERE sex="Female" ;
quit;
/* select a 10% sample */
proc sql;
create table sample_tenp as
select A.*
from yugao.sample as A
where RANUNI(4321) between .45 and .55 ;
quit;
proc sql outobs=10;
select * from yugao.sample ORDER BY age ;
quit;
/* select exactly 100,000 */
proc sql OUTOBS=100000 ;
create table sample_size as
select A.*
from yugao.sample as A
order by RANUNI(4537) ;
quit;
/* select exactly 30,000 females and 30,000 males */
proc sql OUTOBS=30000 ;
create table sample_30k_females as
select A.*
from yugao.sample as A
where sex eq "Female"
order by RANUNI(4321) ;
create table sample_30k_males as
select A.* from yugao.sample as A
where sex eq "Male"
order by RANUNI(4321) ;
RESET OUTOBS=; /* reset Proc SQL options --- don't need OUTOBS=30000 anymore */
create table sample_30k_fm as
select M.* from sample_30k_males as M
OUTER UNION CORR
select F.* from sample_30k_Females as F ;
quit;