2016-02-09 148 views
0

我試圖創建SAS新的變量,有下面的代碼:重疊範圍SAS

if (Age >= 16) and (Age <= 85) then Age_New = "Total, 16 years and over"; 
else if (Age >= 16) and (Age <= 19) then Age_New = "16 to 19 years"; 
else if (Age >= 20) and (Age <= 85) then Age_New = "20 years and over"; 
else if (Age >= 20) and (Age <= 24) then Age_New = "20 to 24 years"; 
else if (Age >= 25) and (Age <= 85) then Age_New = "25 years and over"; 
else if (Age >= 25) and (Age <= 54) then Age_New = "25 to 54 years"; 
else if (Age >= 55) and (Age <= 85) then Age_New = "55 years and over"; 
else if (Age >= 55) and (Age <= 64) then Age_New = "55 to 64 years"; 
else if (Age >= 65) and (Age <= 85) then Age_New = "65 years and over"; 
else Age_New = "Other"; 

現在是什麼情況持續發生的是,我得到的價值「總計,年滿16週歲」的所有觀察和我理解爲什麼。我如何編輯代碼來完成我想要實現的任務?

+1

我認爲你的目標是重疊組的總結。使用IF語句(sans ELSE),您需要輸出每個命中的觀察值。那麼DO:A​​GE_NEW = ...;輸出;結束爲了使它工作,你還需要刪除ELSE部分。 SAS有一個很好的功能,可以使這更容易稱爲「多標籤格式」。您不需要新變量或製作任何觀察值。轉到PROC FORMAT並閱讀它,然後查看PROC MEANS/SUMMARY或TABULATE中的示例以瞭解如何使用它。 –

+0

我其實知道prof格式選項,但實際上我需要爲這個新變量創建一個新的數據集用於其他目的。 – user2916331

+0

你看起來像你要求一個變量有兩個值。這是不可能的......除非你使用MLF,不管怎麼說,正如DN注意到的那樣。你究竟在做什麼(這實際上是可能的)? – Joe

回答

0

您的if-then-else子句並不相互排斥。所有年齡爲< = 85的if語句都包含所有數字,因此第一個具有年齡< = 85的「if語句」將被強制執行。我拿出了你放置的所有聲明和(年齡< = 85),並顯示了預期的結果。

data have; 
    input age; 
    cards; 
16 
18 
22 
33 
44 
55 
66 
77 


; 
data have; 
    set have; 
if (Age >= 16) and (Age <= 19) then Age_New = "16 to 19 years"; 
else if (Age >= 20) and (Age <= 24) then Age_New = "20 to 24 years"; 
else if (Age >= 25) and (Age <= 54) then Age_New = "25 to 54 years"; 
else if (Age >= 55) and (Age <= 64) then Age_New = "55 to 64 years"; 
else Age_New = "Other"; 
run; 

enter image description here

+0

這並沒有真正的幫助,因爲我仍然需要「總計,16歲以上」;但我很感激。 – user2916331

+0

除非他們是兩個不同的變量,否則你不能同時擁有'16至19年'和'總共16年'。它們都包含16-19歲,所以你需要告訴SAS你想保留哪兩個。 – Keni

+0

在if-then-else語句中,爲了SAS檢查else-if語句並執行,第一個if子句必須爲false。你編碼的方式,如果你所有的值在16-85之間,第一個if語句永遠不會是錯的。 – Keni

2

如果你不想使用的格式,那麼你可以創建的意見。

data have; 
    input age; 
    cards; 
16 
18 
22 
33 
44 
55 
66 
77 
;;;; 
    run; 
data need; 
    set have; 
    if (Age >= 16) and (Age <= 85) then do; Age_New = "Total, 16 years and over"; output; end; 
    if (Age >= 16) and (Age <= 19) then do; Age_New = "16 to 19 years";   output; end; 
    if (Age >= 20) and (Age <= 85) then do; Age_New = "20 years and over";  output; end; 
    if (Age >= 20) and (Age <= 24) then do; Age_New = "20 to 24 years";   output; end; 
    if (Age >= 25) and (Age <= 85) then do; Age_New = "25 years and over";  output; end; 
    if (Age >= 25) and (Age <= 54) then do; Age_New = "25 to 54 years";   output; end; 
    if (Age >= 55) and (Age <= 85) then do; Age_New = "55 years and over";  output; end; 
    if (Age >= 55) and (Age <= 64) then do; Age_New = "55 to 64 years";   output; end; 
    if (Age >= 65) and (Age <= 85) then do; Age_New = "65 years and over";  output; end; 
    run; 
proc print; 
    run; 
proc freq; 
    tables age_new/list nocum nopercent; 
    run;