Reputation: 1
I have the below dataset, I would like to show only the consecutive rows that Centre_code changed per Contract_No by Customer.
DATASET 1
Date Customer Contract_No Centre_code
09Jul19 AAAAA AAAAA1234 319
10Jul19 AAAAA AAAAA1234 319
11Jul19 AAAAA AAAAA1234 102
09Jul19 AAAAA AAAAA5678 102
10Jul19 AAAAA AAAAA5678 102
11Jul19 AAAAA AAAAA5678 319
09Jul19 BBBBB BBBBB1234 515
10Jul19 BBBBB BBBBB1234 515
11Jul19 BBBBB BBBBB1234 515
09Jul19 BBBBB BBBBB11878 511
10Jul19 BBBBB BBBBB11878 511
11Jul19 BBBBB BBBBB11878 515
10Jul19 BBBBB BBBBB48741 511
10Jul19 BBBBB BBBBB48741 511
11Jul19 BBBBB BBBBB48741 515
DATASET 2
Date Customer Contract_No Centre_code
10-Jul-19 AAAAA AAAAA1234 319
11-Jul-19 AAAAA AAAAA1234 319
12-Jul-19 AAAAA AAAAA1234 319
13-Jul-19 AAAAA AAAAA1234 319
14-Jul-19 AAAAA AAAAA1234 319
15-Jul-19 AAAAA AAAAA1234 319
16-Jul-19 AAAAA AAAAA1234 319
17-Jul-19 AAAAA AAAAA1234 319
18-Jul-19 AAAAA AAAAA1234 319
19-Jul-19 AAAAA AAAAA1234 319
20-Jul-19 AAAAA AAAAA1234 319
21-Jul-19 AAAAA AAAAA1234 319
22-Jul-19 AAAAA AAAAA1234 102
Desire Output DATASET 1
Date Customer Contract_No Centre_code
10Jul19 AAAAA AAAAA1234 319
11Jul19 AAAAA AAAAA1234 102
10Jul19 AAAAA AAAAA5678 102
11Jul19 AAAAA AAAAA5678 319
10Jul19 BBBBB BBBBB11878 511
11Jul19 BBBBB BBBBB11878 515
10Jul19 BBBBB BBBBB48741 511
11Jul19 BBBBB BBBBB48741 515
Desire Output DATASET 2
Date Customer Contract_No Centre_code
21-Jul-19 AAAAA AAAAA1234 319
22-Jul-19 AAAAA AAAAA1234 102
Upvotes: 0
Views: 121
Reputation: 51601
If by
consecutive rows that Center_code changed
you mean you want to see the observation before the change and the observation after the change you can just use the FIRST.CENTER_CODE and LAST.CENTER_CODE flags. (Make sure to use NOTSORTED keyword since records are sorted by DATE, not CENTER_CODE within the CONTRACT_NO values.) If you also eliminate the first observation for the first center code and the last observation for the last center code then you will only get contracts where there is at least one change.
data want;
set have ;
by customer contract_no center_code notsorted ;
if (last.center_code and not last.contract_no)
or (first.center_code and not first.contract_no)
;
run;
Upvotes: 1
Reputation: 51601
Just use the NOTSORTED keyword on the BY statement.
data have;
length Date 8 Customer $8 Contract_No $20 Center_code 8;
input Date Customer Contract_No Center_code ;
informat date date.;
format date date9.;
cards;
09Jul19 AAAAA AAAAA1234 319
10Jul19 AAAAA AAAAA1234 319
11Jul19 AAAAA AAAAA1234 102
09Jul19 AAAAA AAAAA5678 102
10Jul19 AAAAA AAAAA5678 102
11Jul19 AAAAA AAAAA5678 319
09Jul19 BBBBB BBBBB1234 515
10Jul19 BBBBB BBBBB1234 515
11Jul19 BBBBB BBBBB1234 515
09Jul19 BBBBB BBBBB11878 511
10Jul19 BBBBB BBBBB11878 511
11Jul19 BBBBB BBBBB11878 515
10Jul19 BBBBB BBBBB48741 511
10Jul19 BBBBB BBBBB48741 511
11Jul19 BBBBB BBBBB48741 515
;
data want;
set have;
by customer contract_no Center_code notsorted;
if last.Center_code;
run;
From the comments it sounds like instead of listing where there is a change to center code you want to list ALL CENTERS when there is ANY change. One way to do that is to check if the center code is ever different from the first center code. You can implement that with two DO loops. One to test for changes and one to output the records when there is a change. We can continue to still check the LAST.CENTER_CODE flag just in case the same center code appears for multiple dates in a row.
proc sort data=have;
by customer contract_no date center_code;
run;
data want;
do until(last.contract_no);
set have;
by customer contract_no ;
if first.contract_no then first_center=center_code;
if center_code ne first_center then change=1;
end;
do until(last.contract_no);
set have;
by customer contract_no center_code notsorted;
if change and last.center_code then output;
end;
run;
Upvotes: 1