首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将PROC SQL用作IF/ELSE IF语句

将PROC SQL用作IF/ELSE IF语句
EN

Stack Overflow用户
提问于 2016-11-09 01:27:44
回答 1查看 15.1K关注 0票数 0

我正在尝试运行以下代码,但它不能正常工作。我发现问题在于每个case when都会覆盖下一条语句。

所以,我需要做的是一个IF/ELSE IF语句,但我不知道如何在PROC-SQL中做到这一点

代码语言:javascript
复制
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和更多的变量。我刚刚贴出了它的摘录。

EN

回答 1

Stack Overflow用户

发布于 2016-11-09 01:56:12

在SAS中,如果你正在尝试做你正在做的事情,你不应该使用proc sql。您应该在数据步骤或proc transpose中执行此操作。

如果我有SASHELP.CLASS,并希望每个年龄都作为旗帜,我可以这样做:

代码语言:javascript
复制
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;

等。-大量代码,并且您硬编码了可能的值。或者:

代码语言:javascript
复制
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弹出一个变量,而不是动态创建一个。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40492984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档