Based on the sample data and desired output provided in the original post, here is one way of accomplishing this task:
//SORT EXEC PGM=SORT,PARM='EQUALS'
//SORTIN DD DSN=input.file
//SORTOUT DD DSN=output.file
//SYSOUT DD SYSOUT=*
//SYSIN DD *
SORT FIELDS=(1,10,CH,A)
OUTREC IFTHEN=(WHEN=INIT,OVERLAY=(81:SEQNUM,1,ZD,RESTART=(1,10))),
IFTHEN=(WHEN=(81,1,ZD,EQ,1),BUILD=(1:1,10,11,2,4Z,13,5)),
IFTHEN=(WHEN=(81,1,ZD,EQ,2),BUILD=(1:1,10,2Z,11,2,2Z,13,5)),
IFTHEN=(WHEN=(81,1,ZD,EQ,3),BUILD=(1:1,10,4Z,11,2,13,5))
OUTFIL NODETAIL,REMOVECC,SECTIONS=(1,10,TRAILER3=(1,10,
TOT=(11,2,BI,BI,LENGTH=2),
TOT=(13,2,BI,BI,LENGTH=2),
TOT=(15,2,BI,BI,LENGTH=2),17,5))
/*