So the last account only has one transition, from 1 to 2. Unless you also want to include the first record? Here is a method using BY processing. You could do the first step as a VIEW if the data is large. data have ; length AccountNo $7 Seq_no 8 change_date $10 prodcode $3 prod_id 8 ; input AccountNo change_date ProdCode Prod_id Seq_no; cards; 1900001 2003-04-14 R01 2 1 1900001 2003-07-14 R01 2 2 1900001 2006-01-16 R01 2 3 1900001 2006-04-18 R01 2 4 1550002 2006-07-24 XR5 1 1 1550002 2006-09-15 TG7 1 2 1550002 2006-11-17 R01 2 3 1550002 2007-01-19 R01 2 4 run; data step1 ; set have ; by accountno prodcode notsorted ; if first.prodcode; run; data want; set step1 ; by accountno prod_id notsorted ; if first.accountno and last.accountno then delete; prevcode = lag(prodcode); if first.prod_id and not first.accountno ; put (_all_) (=); run; AccountNo=1550002 Seq_no=3 change_date=2006-11-17 prodcode=R01 prod_id=2 prevcode=TG7 data want; set step1 ; by accountno prod_id notsorted ; if first.accountno and last.accountno then delete; prevcode = lag(prodcode); if first.accountno then call missing(prevcode); if first.prod_id ; put (_all_) (=); run; AccountNo=1550002 Seq_no=1 change_date=2006-07-24 prodcode=XR5 prod_id=1 prevcode= AccountNo=1550002 Seq_no=3 change_date=2006-11-17 prodcode=R01 prod_id=2 prevcode=TG7
... View more