我需要在中创建一些循环,它将数据分成组。我有数据
ID1 ID2 TIME GROUP
1234 12 22MAY2015:16:10:00.000 0
1234 12 22MAY2015:16:15:00.000 0
1234 12 12JUN2015:6:35:00.000 0
1234 12 12JUN2015:16:35:00.000 0
6549 45 15APR2015:16:10:00.000 0
6549 45 18APR2015:13:15:00.000 0
6549 45 18APR2015:13:18:00.000 0
6549 15 22MAY2015:14:15:00.000 0
6549 15 22MAY2015:14:20:00.000 0我需要创建新的列组,对于具有相同的ID1、相同的ID2和最多10分钟的时间差的行,其中的id是相同的。
结果将是:
ID1 ID2 TIME GROUP
1234 12 22MAY2015:16:10:00.000 1
1234 12 22MAY2015:16:15:00.000 1
1234 12 12JUN2015:6:35:00.000 2
1234 12 12JUN2015:16:35:00.000 3
6549 45 15APR2015:16:10:00.000 4
6549 45 18APR2015:13:15:00.000 5
6549 45 18APR2015:13:18:00.000 5
6549 15 22MAY2015:14:15:00.000 6
6549 15 22MAY2015:14:20:00.000 6我试着写了一些“边做边做”的循环,但它不起作用。
data b;
set a;
time1 = time;
id1_1 = id1;
id2_1 = id2;
time2 = time;
id1_2 = id1;
id2_2 = id2;
group = group+1;
do while (id1_1 eq id1_2 id2_1 eq id2_2 floor((time2-time1)/60)<=10);
group = group;
time2 = time;
id1_2 = id1;
id2_2 = id2;
end;
run;非常感谢。
发布于 2015-07-07 13:54:50
Proc不是解决问题的合适工具。你考虑过‘循环’,这是一个好的开始。但是,代码中缺少的元素是“滞后”概念以及其他一些细节。您的分组条件有两个部分: 1)基于ID1的自然组,ID2 2)在1)顶部,如果时间间隔超过10分钟,则生成额外的组。
data have;
input (ID1 ID2) (:$8.) TIME:datetime23.;
format TIME:datetime23.;
cards;
1234 12 22MAY2015:16:10:00.000 1
1234 12 22MAY2015:16:15:00.000 1
1234 12 12JUN2015:6:35:00.000 2
1234 12 12JUN2015:16:35:00.000 3
6549 45 15APR2015:16:10:00.000 4
6549 45 18APR2015:13:15:00.000 5
6549 45 18APR2015:13:18:00.000 5
6549 15 22MAY2015:14:15:00.000 6
6549 15 22MAY2015:14:20:00.000 6
;
data want;
group+1; /*condition part 1*/
do until (last.id2);
set have;
by id1 id2 notsorted;
lag_time=lag(time);
if not first.id2 then group+intck('minute',lag_time,time)>10; /*condition part 2*/
output;
end;
drop lag_time;
run;https://stackoverflow.com/questions/31264253
复制相似问题