You haven't given enough information for anyone to help you.
You show a maximum of three records with the same key. Is that the actual maximum or can there be more records with the same key (how many maximum)?
Please show a better example of the records in your input file (relevant fields only) and what you expect for output, covering all possible cases. Explain the "rules" for getting from input to output. Give the starting position, length and format of each relevant input and output field. Give the RECFM and LRECL of the input file and expected output file.