2016-04-28 51 views
0

有没有办法在随机抽样中更改和操纵SAS中某个变量的比例?SAS更改随机样本的比例

可以说我有1000人组成的桌子。 (500名男性和500名女性)

如果我想随机抽取100名性别分层的样本 - 我的输出中将有50名男性和50名女性。

我想知道是否有办法达到所需比例的性别价值?

我们可以随机抽样100人,男性70人,女性30人?

回答

0

通常这就是proc surveyselect的用途。

但对于一个快速和肮脏datastep解决方案:

data in_data; 
    do i= 1 to 500; 
     sex = 'M'; output; 
     sex = 'F'; output; 
    end; 
run; 
data in_data; 
    set in_data; 
    rannum = ranuni(12345); 
run; 
proc sort data= in_data; by rannum; run; 
data sample_data; 
    set in_data; 
    retain count_m count_f 0; 
    if  sex = 'M' and count_m lt 70 then do; count_m + 1; output; end; 
    else if sex = 'F' and count_f lt 30 then do; count_f + 1; output; end; 
run;  
proc freq data= sample_data; 
    table sex; 
run; 
2

PROC SURVEYSELECT是做到这一点的方式,使用数据集nsamprate,而不是数量。

data strata_to_Sample; 
    length sex $1; 
    input sex $ _NSIZE_; 
datalines; 
M 70 
F 30 
;;;; 
run; 
proc sort data=strata_To_sample; 
    by sex; 
run; 

data to_sample; 
    set sashelp.class; 
    do _i = 1 to 1e5; 
    output; 
    end; 
run; 
proc sort data=to_Sample; 
    by sex; 
run; 

proc surveyselect data=to_sample n=strata_to_sample out=sample; 
    strata sex; 
run;