SAS计数基于设定时间段内的多个层的出现次数

时间:2016-02-01 17:04:05

标签: count sas flags proc-sql datastep

我正在尝试计算每个实例在30天内在同一个地方为同一个人收取四次或更多次费用的事件。例如,输入看起来像:

person   service  place  date
A         x       shop1  01/01/15
A         x       shop1  01/15/15
A         x       shop1  01/20/15
B         y       shop2  03/20/15 
B         y       shop2  04/01/15
C         z       shop1  05/05/15

输出看起来像:

person   service  place  date      count
A         x       shop1  01/01/15  3
A         x       shop1  01/15/15  3
A         x       shop1  01/20/15  3
B         y       shop2  03/20/15  2 
B         y       shop2  04/01/15  2
C         z       shop1  05/05/15  1

我尝试过类似的东西:

data work.want;
 do _n_ =1  by 1 until (last.PLACE);  
   set work.rawdata;
   by PERSON PLACE;
   if first.PLACE then count=0;
   count+1;
 end;
 frequency= count;  
 do _n_ = 1 by 1 until (last.PLACE); 
   set work.rawdata;
   by PERSON PLACE;
   output;
 end;
run;

这给出了一个基于人和地点的计数,但不计入时间。任何帮助或建议将不胜感激!谢谢

2 个答案:

答案 0 :(得分:0)

proc sql;
create table summary as
select person, service, place, count(*) as count
from rawdata
group by person, service, place
having count>=4;
quit;

注意:这不会检查事件是否在30天之内发生。我不知道你的数据集中的数据类型。

答案 1 :(得分:0)

使用proc sql ...

可以轻松完成

您的数据:

data have;
input person $ service $ place $;
datalines;
A         x       shop1
A         x       shop1
A         x       shop1
B         y       shop2
B         y       shop2
C         z       shop1
;
run;

然后我们计算"地点"的出现次数。对于每个1,2组,并加入原始表。

proc sql;
create table want as
select a.*, b._count
from have as a
inner join 
(
    select person, service, count(place) as _count
    from have 
    group by 1,2
) as b
on a.person = b.person
and a.service = b.service
;
quit;

是否有日期字段?我们需要它来按月(或30天)分组数据。