SAS proc sql with when then 语句

SAS proc sql with when then statements

我的数据中有 "time" 年的变量。我需要使用 PROC SQL 基于以下内容创建一个新的 var if time>mean(time)then new var=1 否则,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;

你快到了:

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

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

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