我有一个数据集,如下所示,我需要检索两件事: 1)每个日期( date -1)和(date-3)之间的值的总和;2)在这5天中,是否有两天的值为0。( >= -1)我认为应该使用PROC SQL,但我不确定如何实现它。输入数据集:
ID DATE VALUE
1 20110101 0
1 20110102 0
1 20110103 1
1 20110104 2
2 20110101 1
2 20110102 2
2 20110103 3
2 20110104 4 对于ID1,输出应为1) 1 (0+0+1),对于ID2,20110104,输出应为6 (1+2+3),20110104。以及2) ID1的标记,20110104,因为在3天窗口中有2天的值为0。
任何帮助都是非常感谢的!
发布于 2011-01-13 20:27:56
这两个问题都可以用类似的SQL查询来解决。你的第二个问题有点令人困惑,因为你曾经提到过一个5天的周期和一个3天的窗口。我对两个查询都使用了相同的3天窗口,因此如果您需要另一个窗口,请修改开始和结束日期。
1)
proc sql;
select t1.id, t1.date, sum(t2.value) as totalvalue
from _input t1
left join _input t2
on t1.date-4 lt t2.date
and t1.date gt t2.date
and t1.id = t2.id
group by t1.id, t1.date;
quit;2)
proc sql;
select t1.id, t1.date
from _input t1
left join _input t2
on t1.date-4 lt t2.date
and t1.date gt t2.date
and t1.id = t2.id
and t2.value = 0
group by t1.id, t1.date
having count(*) ge 2
;
quit;发布于 2011-01-14 06:06:10
这里有一种仅使用数据步骤的替代方法。我假设您不想要小于三条记录的范围内的总和和标记,因此数据步骤显式地将它们设置为未定义。
proc sort data=sample;
by id date;
run;
data result(drop=k count);
retain count;
set sample;
by id;
if first.id then count=0;
sum=lag1(value) + lag2(value) + lag3(value);
if count<3 then sum=.;
k=0;
if lag1(value)=0 then k=k+1;
if lag2(value)=0 then k=k+1;
if lag3(value)=0 then k=k+1;
if k ge 2 then mark=1;
count=count+1;
run;
proc print data=result;
run;https://stackoverflow.com/questions/4678655
复制相似问题