在 SAS proc-sql 中将数据分组

divide data into groups in SAS proc-sql

我需要在 SAS proc-sql 中创建一些循环,将数据分成几组。 我有资料

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

我需要创建新列 GROUP,其中对于具有相同 ID1、相同 ID2 且 TIME 之间的差异最大为 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

我试过写一些 'do while' 循环但是它不起作用。

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;

非常感谢。

Proc SQL 不是解决您的问题的合适工具。你已经考虑过'Looping',这是一个好的开始。但是,您的代码中缺少的元素是 'lagging' 概念以及一些其他细节。您的分组条件分为两部分:1)基于ID1、ID2 的自然分组 2)在1)之上,如果TIME 相隔超过10 分钟,则生成附加分组。

data have;
    input (ID1     ID2) (:.) 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;