【问题标题】:SAS proc sql with when then statements带有when then语句的SAS proc sql
【发布时间】:2015-06-15 19:01:16
【问题描述】:

我的数据中有一个多年的“时间”变量。我需要使用 PROC SQL 根据以下内容创建一个新 var if time>mean(time)then new var=1 else, new var=0

我不断收到不同的错误,我该如何改进我的代码?

proc sql;
create table v3 as
select*,case
    when time>mean(time)then time_group=1
    else time_group=0 as time_group,*
    from v2;
    quit;

【问题讨论】:

    标签: sas


    【解决方案1】:

    你快到了:

    proc sql ;
        create table v3 as select *, case when time>mean(time) then 1 else 0 end
            as time_group from v2;
    quit;
    

    【讨论】:

      【解决方案2】:

      您的代码中有几个问题。 1. CASE WHEN 的语法有点偏。 2.在使用Mean()等汇总函数时,需要确定均值的范围。如果没有发出“分组依据”来定义范围,则范围是通用的。

      proc sql;
          select *, case when age > mean(age) then 1 else 0 end as _age from sashelp.class group by sex;
      quit;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-23
        • 1970-01-01
        • 2011-03-22
        • 1970-01-01
        相关资源
        最近更新 更多