DATA Step, Macro, Functions and more

Is there a dynamic way to identify the matched first and last value per group without sorting the or

Accepted Solution Solved
Reply
SAS Employee
Posts: 17
Accepted Solution

Is there a dynamic way to identify the matched first and last value per group without sorting the or

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.

 


Accepted Solutions
Solution
‎08-09-2016 05:50 AM
Super User
Posts: 6,928

Re: Is there a dynamic way to identify the matched first and last value per group without sorting th

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)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Super User
Posts: 7,392

Re: Is there a dynamic way to identify the matched first and last value per group without sorting th

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;
Solution
‎08-09-2016 05:50 AM
Super User
Posts: 6,928

Re: Is there a dynamic way to identify the matched first and last value per group without sorting th

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)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
SAS Employee
Posts: 17

Re: Is there a dynamic way to identify the matched first and last value per group without sorting th

Thanks very much!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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