Two ways: JOINKEYS with the same dataset for both files. In JNF1CNTL add a sequence number, starting from zero, in the JNF2CNTL a sequence number starting from one. The sequence numbers are the key. You need JOIN UNMATCHED,F1. The only unmatched you get is the first record. For all the matches, you have your current record (F1) and the previous record (F2). Use the REFORMAT to get the values you need. In the main task do the subtraction and formatting that you need.
The other way is with WHEN=GROUP, have a look here.
Once I had error message saying you can not use OUTREC on JNF1CNTL (CNTL on JOIN), hence I assumed I can not use INREC on JNFnCNTL as well. In future, I will use INREC as and where required to utilize JNFnCNTL's power.
I am aware I should use SORTED on JOINKEYS if records are already sorted. I was just lazy. I will read more about NOSEQCK to write efficient SORT (JOIN).
I think I need to use OVERLAY more and more to write efficient code.
If you look at the section on DFSORT Processing, and Figure 2, then the chapter on JOINKEYS and Figure 15, you should be able to see why OUTREC and OUTFIL are not available on the JNFnCNTL files.
There are various restrictions, for similar reasons, on various ICETOOL operators.
OVERLAY is (generally) more efficient, and where it can be used, better expresses what you want to achieve. Since you only specify the locations where you want to OVERLAY, there is less to go wrong, also.
I think if an OVERLAY extends a record, all the data in a record will be moved, so there is not always a "performance" question.
There is always a "current record". OVERLAY operates on the current record (unless the record is extended, when a new current record will be created).
BUILD always creates a new current record.
With BUILD, you can't overwrite a field you've already written. With OVERLAY you can.
Because BUILD creates a new current record, you can do (2,1,1,1), try that with OVERLAY and you'll get an unexpected result.
Experiment. Add one thing at a time and look at the output. "If the data were like that I could do it" so is it possible make the data like that?
There's also a mass of good stuff from Frank Yaeger and Kolusu spread around the place. Find something that looks complicated, and work out how it works.