SAS proc sql 使用计数和最大值更新 table
SAS proc sql update table with count and max
我想在左侧的基础上在右侧创建一个 table:
我首先使用以下代码创建了一个新的 table(成功了!):
proc sql;
create table (table_right) as
select distinct Customer format .
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=.,
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;
非常感谢任何解决方案!
不需要multiple proc sql语句,一口气搞定。另请注意,proc sql
以 quit
结束,而不是 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;
我想在左侧的基础上在右侧创建一个 table:
我首先使用以下代码创建了一个新的 table(成功了!):
proc sql;
create table (table_right) as
select distinct Customer format .
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=.,
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;
非常感谢任何解决方案!
不需要multiple proc sql语句,一口气搞定。另请注意,proc sql
以 quit
结束,而不是 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;