2015-04-06 87 views
0

我的數據是學校及其在某些科目評估中的表現以及在課程中註冊性別的百分比列表。我創建了一個樣本數據設置如下:正如你所看到的,沒有性別比例共收集學校Y.研究有條件填補SAS

data have; 
    input school $ subject $ perc_male perc_female score similar_school $; 
datalines; 
X math 51 49 93 Y 
X english 48 52 95 Y 
X tech 60 40 90 Y 
X science 57 43 92 Y 
Y math . . 87 X 
Y english . . 83 X 
Y science . . 81 X 
Y language . . 91 X 
Z math 40 60 78 Z 
Z english 50 50 76 Z 
Z science 45 55 80 Z 
; 
run; 

表明,學校X有一個非常類似的性別分佈,所以我要歸咎於主題從X到Y的特定百分比。另一個問題是Y有語言得分,而X沒有進行這種評估。在這種情況下,我希望採用估算值的平均值(51,48,57)來得到男性語言接受者百分比的52。

執行,這將證明我所需的輸出:

data want; 
    input school $ subject $ perc_male perc_female score; 
datalines; 
X math 51 49 93 Y 
X english 48 52 95 Y 
X tech 60 40 90 Y 
X science 57 43 92 Y 
Y math 51 49 87 X 
Y english 48 52 83 X 
Y science 57 43 81 X 
Y language 52 48 91 X 
Z math 40 60 78 Z 
Z english 50 50 76 Z 
Z science 45 55 80 Z 
; 
run; 

有一個downvote,因此添加什麼我試着讓幾乎我在哪裏我需要。對於誰低估,我想知道你是否有任何建設性的反饋意見。謝謝!我想知道是否有方法將平均插補部分構建到我當前的片段中。另外,我認爲可能有更有效的方法來做到這一點。任何幫助將不勝感激。

proc sql; 
    select distinct cats("'",similar_school,"'") into :school_list separated by ',' 
    from have 
    where perc_male=.; 
quit; 

proc sql; 
    create table stuff as 
    select similar_school as school, subject, perc_male, perc_female 
    from have 
    where school in (&school_list.); 
quit; 

proc sql; 
    create table want2 as 
    select a.school, a.subject, coalesce(a.perc_male,b.perc_male), coalesce(a.perc_female,b.perc_female), a.score, a.similar_school 
    from have as a 
    left join stuff as b 
     on a.school=b.school and a.subject=b.subject 
    ; 
quit; 

回答

1

根據您的預期數據,palin簡單的SQL可以解決您的問題。您可以先根據學校和類似的學校信息進行自我加入,併合並perc_male & perc_female信息。這將照顧你的第一個問題。對於問題的第二部分,你可以計算每個學校的平均值,並將perc_male & perc_female信息與相應的學校平均值結合起來。看看下面的SQL,讓我知道它是否有幫助。

proc sql; 
create table want as 
select aa.school 
    , aa.subject 
    , coalesce(aa.perc_male, mean(aa.perc_male)) as perc_male 
    , coalesce(aa.perc_female,mean(aa.perc_female)) as perc_female 
    , score 
    , similar_school 
from (
     select a.school 
      , a.subject 
      , coalesce(a.perc_male ,b.perc_male) as perc_male 
      , coalesce(a.perc_female,b.perc_female) as perc_female 
      , a.score 
      , a.similar_school 
     from have as a 
     left join have as b 
       on b.school=a.similar_school 
      and a.subject=b.subject 
    ) as aa 
group by aa.school 
; 
quit; 
+0

謝謝你sushil。這執行我需要的插補。 – pyll 2015-04-06 20:37:57