Well, showing unsorted data for the input and just example output without the rules is not clear.
Sort the file on the key.
On OUTREC use GROUP on your key and PUSH a SEQuence number. You have a variable-length record file, so put your sequence in the "fixed" part, right up at the front is easiest.
Your key does not start at 1 for a length of 4, because, with VB, you need to take the RDW into account. Your key is 5 for a length of 4.
On OUTFIL INCLUDE=(whereyourseqis,yourlength,ZD,LE,2) with BUILD to only include the RDW and the original part of the record.