Is one of your requirements "You must do this task in one pass"? If not, then why not massage the incoming files into an intermediate stage that's more suitable for your needs. For example, I'd add all the missing dates so that my files corresponded line by line. Then, I'd create the final file from those temporary ones.
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?