|
|
沙发

楼主 |
发表于 2010-9-29 08:38:44
|
只看该作者
Re: 计算总合!
没太明白你的规则
开始我以为是把总和的数据拼回原数据集,但发现有些观测的总和是missing; 于是我猜group的观测数等于1的总和值不要; 但问题又来了,对有2条观测的组为什么有些组是2条观测都有总和值,而有些组只有后一条观测有总和值呢?
[code:3275b3eq]data raw;
infile datalines truncover;
input date :yymmdd10. id time $ action $ count sum;
format date yymmdd10.;
datalines;
2008/1/5 1 100101 A 2 56
2008/1/5 1 102201 A 3 56
2008/1/5 1 102456 B 1
2008/1/5 1 105533 A 51 56
2008/1/5 2 110055 A 11 28
2008/1/5 2 112051 A 12 28
2008/1/5 2 132505 A 5 28
2008/1/6 3 133555 B 6
2008/1/6 1 101200 B 41 42
2008/1/6 1 115555 A 55
2008/1/6 1 125455 B 1 42
2008/1/7 2 113311 A 2 5
2008/1/7 2 115555 A 3 5
2008/1/7 3 134455 B 44
2008/1/8 3 105633 A 15
2008/1/8 3 112255 A 5 20
2008/1/8 3 120044 B 6
2008/1/8 1 125444 A 7
2008/1/8 1 133333 A 8 15
;
proc sql;
create table out as
select a.*, b.calculate_count
from raw a left join
(select date, id, action,count(id) as count, sum(count) as calculate_count
from raw
group by date, id, action
having count gt 1) b
on a.date=b.date and a.id=b.id and a.action=b.action
order by date, time;
quit;[/code:3275b3eq] |
|