2016-11-08 208 views
-1

我試圖運行下面的代碼,但它無法正常工作。我發現問題是每個case when都會覆蓋下一條語句。使用PROC SQL作爲IF/ELSE IF語句

所以,我需要做的是一個IF/ELSE IF staetment,但我不知道該怎麼做,在PROC-SQL

proc sql; 
create table example 
as select *, 

case when B.variable = 'X'    then 1 else 0 end as variable_X, 
case when B.variable = 'Y'    then 1 else 0 end as variable_Y, 
case when B.variable = 'Z'    then 1 else 0 end as variable_Z, 
case when B.variable = 'W'    then 1 else 0 end as variable_W, 
case when B.variable = 'R'    then 1 else 0 end as variable_R, 
case when B.variable = 'G'    then 1 else 0 end as variable_G, 
case when B.variable = 'T'    then 1 else 0 end as variable_T, 
case when B.variable = 'U'    then 1 else 0 end as variable_U, 
case when B.variable = 'P'    then 1 else 0 end as variable_P, 
case when B.variable = 'L'    then 1 else 0 end as variable_L 

FROM my_table    as A 
LEFT JOIN my_second_table as B 
on A.KEY1=E.KEY1 and A.KEY2=E.KEY2 
; 

我已經嘗試過使用group by聲明,但它沒」工作。

P.S .:我真實的代碼比我的例子大得多,有8 left join以及更多的變量。我剛剛發佈了它的摘錄。

+0

您對「覆蓋」的評論沒有意義。你當然可以做(或多或少)你在做什麼,並得到一個潛在的有效結果。 – Joe

回答

0

在SAS中,如果您嘗試在那裏做你正在做的事情,則不應該使用proc sql。您應該在數據步驟或proc transpose中執行此操作。

如果我有SASHELP.CLASS,並希望每一個時代的標誌,我可以這樣做:

proc sql; 
    select name, age, 
    case when age=11 then 1 else 0 end as age_11, 
    case when age=12 then 1 else 0 end as age_12 
    from sashelp.class; 
quit; 

等 - 大量的代碼,你硬編碼的可能值。或者:

data class; 
    set sashelp.class; 
    x=1; 
run; 

proc transpose data=class out=class_t prefix=age_; 
    by name; 
    id age; 
    var x; 
run; 

然後將其重新合併,但是您希望假設您有其他有用的數據。您可能已經擁有一個變量,您可以爲佔位符x彈出一個變量,而不是立即創建一個變量。