How can we possibly help you when you haven't shown us your JCL, input, output, etc? We have no idea what functions you're using or what you're actually doing/trying to do.
Show an example of the records in your input files (relevant fields only) and what you expect for output. Explain the "rules" for getting from input to output. If input file1 can have duplicates within it, show that in your example. If input file2 can have duplicates within it, show that in your example. Give the RECFM and LRECL of the input files. Give the starting position, length and format of all relevant fields.