SAS proc sql update table with count和max

时间:2017-06-15 07:42:28

标签: mysql sql sql-server sas proc-sql

我想在左侧创建一个右侧的表格: enter image description here

我首先使用以下代码创建一个新表(工作!):<​​/ p>

proc sql;
    create table (table_right) as
        select distinct Customer format $15.
        from (table_left);
    select * from (table_right);
run;

proc sql;
    alter table (table_right)
        add Count_bin30 num label='Count_bin30' format=1000000.,
            Max_Date_Diff num label='Max_Date_Diff' format=1000000.;
    select Customer format=$15.,
           Count_bin30,
           Max_Date_Diff
    from (table_right);
run;

然后当我尝试更新新添加的列时,我陷入困境:

proc sql;
    update (table_right)
        set Count_bin30 = select count(Date_bin)
        from (table_left)
        where Date_bin='bin30';
run;

proc sql;
    update (table_right)
        set Max_Date_Diff = select max(Date_diff)
        from (table_left);
run;

非常感谢任何解决方案!

1 个答案:

答案 0 :(得分:1)

不需要多个proc sql语句,可以一次性实现结果。另请注意,proc sqlquit而不是run结束。

data table_left;
input customer $ date_diff date_bin $;
datalines;
James 0 0 
James 35 bin30
James 0 0
James 30 bin30
James 0 0
James 0 0
Daniel 0 0
Daniel 45 bin30
Daniel 61 bin60
Daniel 0 0
Ivy 0 0
Ivy 0 0
Ivy 0 0
Ivy 0 0
Ivy 108 bin90
Laura 0 0
Laura 0 0
;
run;

proc sql;
create table table_right
as select
customer,
sum(case when date_bin='bin30' then 1 else 0 end) as Count_bin30,
max(date_diff) as Max_Date_Diff
from table_left
group by customer
;
quit;