SAS proc sql with when then statements

时光怂恿深爱的人放手 提交于 2019-12-25 03:35:34

问题


I have a "time" var of years in my data. I need to create a new var based on the following with PROC SQL if time>mean(time)then new var=1 else, new var=0

I keep getting different error, how can I improve my code?

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;

回答1:


You're nearly there:

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



回答2:


There are couple issues in your code. 1. The syntax of CASE WHEN is a little off. 2. When using summary functions, such Mean(), you need to make sure the scope of the mean. if no 'group by' is issued to define the scope, the scope is universal.

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


来源:https://stackoverflow.com/questions/30853017/sas-proc-sql-with-when-then-statements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!