问题
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