A complex conditional workflow in DFSORT is a pipeline that uses several kinds of conditional logic in one step: INREC IFTHEN (reformat before filter and sort), INCLUDE or OMIT (filter by condition), SORT, OUTREC IFTHEN (conditional output layout), and optionally OUTFIL with build or IFTHEN for additional outputs. The key is understanding the order of execution: INREC runs first, so the record that INCLUDE and SORT see is the INREC result; OUTREC runs last when writing to SORTOUT. This page explains that order, when to use IFTHEN in INREC vs OUTREC, how to combine INCLUDE/OMIT with IFTHEN, and how to design a full conditional pipeline.
DFSORT processes control statements in a fixed order. Knowing this order is essential for designing conditional workflows:
So if you need a field to be normalized or built so that INCLUDE or SORT can use it, that must happen in INREC. If you only need different formatting in the written file, OUTREC (or OUTFIL) is enough.
| Phase | Use when | Example |
|---|---|---|
| INREC IFTHEN | The conditional layout must affect INCLUDE/OMIT or the sort key. | Normalize date to YYYY-MM-DD so INCLUDE can filter invalid dates; build a composite key for SORT. |
| OUTREC IFTHEN | You only need different output layout by record type; sort and filter do not depend on it. | Header records get one report format, detail records another; add labels or edit masks for display. |
You can use both. For example: INREC IFTHEN to overlay a default date when invalid (so INCLUDE can keep only valid dates and SORT sees a consistent key), then OUTREC IFTHEN to add a "type" label in the first 10 bytes for report display. INREC affects what gets filtered and sorted; OUTREC affects what gets written.
A common pattern is to normalize or fix data in INREC so that filtering and sort behave correctly. For example, overlay an invalid date with a default in INREC, then use INCLUDE to keep only records where that date is in range, then SORT by that date. The INREC step ensures the date field is in a consistent format before INCLUDE and SORT see it.
12345INREC IFTHEN=(WHEN=INIT,BUILD=(1:1,80)), IFTHEN=(WHEN=(72,10,CH,EQ,C'9999-99-99'), OVERLAY=(72:C'0001-01-01')), INCLUDE COND=(72,10,CH,GE,C'2000-01-01',AND,72,10,CH,LE,C'2099-12-31') SORT FIELDS=(72,10,CH,A)
INREC overlays position 72 with a default when the date is the sentinel. Then INCLUDE keeps only records where the date (now possibly defaulted) is in the 2000–2099 range. SORT orders by that date. Without INREC, the sentinel value would not pass the INCLUDE range check and might sort incorrectly.
After the sort, you may want different output layout by record type. Use OUTREC IFTHEN: e.g. WHEN=(1,2,CH,EQ,C'H1') with one BUILD for headers, WHEN=(1,2,CH,EQ,C'D1') for details, WHEN=NONE for unknown. The sort already used the keys it needed; OUTREC only changes how the record is written, not which records exist or their order.
1234OUTREC IFTHEN=(WHEN=INIT,BUILD=(1:1,80)), IFTHEN=(WHEN=(1,2,CH,EQ,C'H1'),OVERLAY=(1:1,10,C'HEADER ')), IFTHEN=(WHEN=(1,2,CH,EQ,C'D1'),OVERLAY=(1:1,10,C'DETAIL ')), IFTHEN=(WHEN=NONE,OVERLAY=(1:1,10,C'UNKNOWN '))
Every record gets bytes 1–80 from the sorted record (WHEN=INIT). Then the first 10 bytes are overwritten with a label depending on record type. So the written file has a readable prefix without changing sort order or record count.
Put it together: (1) INREC IFTHEN to normalize or fix fields; (2) INCLUDE to drop bad or unwanted records; (3) SORT to order; (4) OUTREC IFTHEN to format output by type. Optionally (5) OUTFIL to write a second file (e.g. report or subset) with its own build or IFTHEN.
Keep each phase focused. Use INREC for "what the rest of the step sees"—normalization and keys. Use INCLUDE/OMIT for "which records stay." Use OUTREC for "what gets written." If a condition is only for display, put it in OUTREC or OUTFIL; if it affects filtering or sort, put it in INREC and/or INCLUDE. Document the intended flow (e.g. in comments in the JCL or SYSIN) so that future changes do not break the order of dependencies.
Think of a factory line. First you fix the toys so they all look the same where it matters (INREC). Then you throw away the ones that don't pass the check (INCLUDE/OMIT). Then you line them up by size (SORT). Then you put different stickers on them depending on their type when you pack them in the box (OUTREC). So fixing happens first, then filtering, then sorting, then the final sticker. The order never changes.
1. In what order do INREC, INCLUDE/OMIT, SORT, and OUTREC run?
2. If you need to normalize a field so INCLUDE can filter on it, where do you do it?
3. When would you use IFTHEN in both INREC and OUTREC in the same job?
4. What is a typical “complex conditional workflow” pattern?
5. Can OUTFIL use IFTHEN for conditional formatting on a secondary output file?