if it is just for learning just start trying something Yourself
some hints ...
joinkeys <== the trick is to put the total amount only on the first occurrence of the key
if amount >= invoice
invoice = 0
amount = amount-invoice
if amount = 0
see what happens without cleaning up the work columns
my point is not about the lowly technicalities, but about the proper business approach
The tricky part is keeping the records with zeros on. There is no information other than the zero - in this "requirement".
Is there a maximum number of records for a key?
Why we are wary is because of the previous two, where the first "requirement" looks like a "get some skeleton code so I can do the rest", but where you didn't understand it enough to be able to implement it, and the combined effort of getting to the actual solution for the actual requirement far exceeded that needed to just provide the actual requirement.
So, do a JOINKEYS, with a SUM in the JNFnCNTL for the multi-record file. When you get the match, update the balance, copy the balance to the multiple-now-one record and write to two seperate files with OUTFIL.
If you are somehow desperate to keep the zeros, which are meaningless, and there is no sensible limit to the number of records per key, let us know, as I think you'll need another JOINKEYS step just to do it, which seems a bit of a waste.
Requires two single-record files for a header and a trailer, same DCB as other files.
First file, transactions
SUMmed on amount (have to convert it for the SUM) and the character "1" to get SUM to indicate how many records make up the summed record.
GROUP using header and trailer to set a marker for masters
Main processing (not necessarily in order/consolidated, see INREC/OUTREC comment)
REFORMAT sticking both records together
GROUP on the 2nd file part, generating sequence
GROUP on the 2nd file part pushing amount
if Master, update to reflect transactions
if not Master, set to "+0 " or the trans
if trans no is equal to seq no, set to value from master
You'll need to use INREC and OUTREC in the main processing.
There are probably better ways, but it can definitely be done.
EDIT: Forgot. Two OUTFILs to create the new datasets.