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.
data want;
set have;
retain cat 1 count 0 startspeed;
if count = 0 then startspeed = speed;
count + 1;
output;
if count >= 10 and speed = startspeed
then do;
cat + 1;
count = 0;
end;
drop count startspeed;
run;
(Untested)
Sounds like a simple datastep with a counter:
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;
data want;
set test;
retain counter cat;
if _n_=1 then do;
counter=1;
cat=1;
end;
if counter >=10 and speed=0 then do;
counter=1;
cat=cat+1;
end;
else counter=counter+1;
run;
data want;
set have;
retain cat 1 count 0 startspeed;
if count = 0 then startspeed = speed;
count + 1;
output;
if count >= 10 and speed = startspeed
then do;
cat + 1;
count = 0;
end;
drop count startspeed;
run;
(Untested)
Thanks very much!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.