BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
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)

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Kurt_Bremser
Super User
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)

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 836 views
  • 0 likes
  • 3 in conversation