I think I can see what you are trying. You will need to extend a bit.
You'll only have your key when you have fhe MBX, which is the fourth record, with the other part from the second. But you'll need the first and third records as well.
I'm not sure why you chose two GROUPs for ORIGINAL
GROUP for ORIGINAL:, PUSH input record/see later.
GROUP for DOCNAME:. PUSH input record/see later.
GROUP for TO: PUSH input record/see later.
MBX arrives. GROUP, as you have to PUSH the composed key for the following records, so END=(1,7,CH,EQ,C'ENDMSG:').
SORT on the composed key (all in the same place, way out at the end of each record.
Use OUTFIL / (slash operator) in BUILD to output ORIGINAL, DOCNAME, TO and MBX and stripping everything down to size.
Now, the "see later" bit. If, as you did with ORIGINAL, you specify RECORDS=2 on the GROUP you won't get so much pushing going on but you will get the data, you just have to PUSH the entire extended record in each case.
You were going in the right direction. You have Syncsort, I don't, so see how it goes. Let us know, please, with the working code, as it would be a useful example for others.
This is with DFSORT. No guarantees if you try it on something else, but try.
"To be fair" this thread is probably the one Dave was thinking of yesterday.
I've "simplified" from how I described in the sense that this should be easier to follow. Since all records must be extended, there's probably minimal savings if any by re-arranging the GROUPs as previously mentioned and without using SYMNAMES this is clearer, and more readily adaptable for additional purposes.
Each of the four records from the start until the second element of the key get their own "pushed" record extension. The sort is then on the location of the full key in the two different portions. EQUALS is necessary for your data to be sorted correctly, as you have multiple records with the same key and you want them to stay in the original order.
The four entire records are carried (on all records) to be able to recreate them in their correct place afterwards. The three later groups are forced to end within the bounds of the ORIGINAL group.
The "original" of the records which were pushed have to be removed later, as they are incomplete, so the OMIT on the OUTFIL. The MBX: is identified to write out the pushed records, using the "/" slash operator, and finally itself after the others in original order.
The subsequent are cut down to a size of 80 by the IFOUTLEN.