2016-11-08 23 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弹出一个变量,而不是立即创建一个变量。