我正在尝试运行以下代码,但它不能正常工作。我发现问题在于每个case when都会覆盖下一条语句。
所以,我需要做的是一个IF/ELSE IF语句,但我不知道如何在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语句,但它不起作用。
附言:我的真实代码比我的例子要大得多,有8个left join和更多的变量。我刚刚贴出了它的摘录。
发布于 2016-11-09 01:56:12
在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弹出一个变量,而不是动态创建一个。
https://stackoverflow.com/questions/40492984
复制相似问题