Suppose I have a data set with 100 records and three variables, x, y and speed. And I hope to group at least consective 10 records into a category (variable called cat). But at first I need to satify the condition that matches the first and last records per group have the same value of speed (if the first observation of speed is 0). I also want to keep the missing observations. Other obs in the same group can have different values. If the value of speed at 10th and 11th records are not 0, the 12th record has speed 0, then it is the last record of the group. This should be included in the first category. When the second category starts, the value of first and the last record will be the value of 13th value for the second category.
Is there a dynamic way to identify the matched first and last value per group without sorting the original dataset.
For example,
data test ; input x y speed ; datalines ; 0.1 2 0 0.3 3 1 0.9 5 4 0.8 4 2 1.1 3 5 1.4 2 3 0.9 2 4 0.5 2 2 0.8 2 2 0.1 5 1 0.1 3 1 1.1 3 0 1.3 4 5 1.6 6 5 1.2 5 . 1.4 3 4 1.5 1 0 1.4 6 5 1.2 5 5 0.3 2 3 0.5 2 0 1.4 3 . 0.0 1 0 3.1 6 5 ; run;
The desired output -
Obs
x
y
speed
cat
1
0.3
3
0
1
2
0.9
5
1
1
3
0.8
4
4
1
4
1.1
3
2
1
5
1.4
2
5
1
6
0.9
2
3
1
7
0.5
2
4
1
8
0.8
2
2
1
9
0.1
5
2
1
10
0.1
3
1
1
11
1.1
3
1
1
12
1.3
4
0
1
13
1.6
6
5
2
14
1.2
5
5
2
15
1.4
3
.
2
16
1.5
1
4
2
17
1.4
6
0
2
18
1.2
5
5
2
19
0.3
2
5
2
20
0.5
2
3
2
21
1.4
3
0
2
22
0.0
1
.
2
23
3.1
6
0
2
24
3.1
6
5
2
Any insights are appreciated.
... View more