Most Easytrieve programs operators schedule overnight are batch: read a file, apply rules, write results, print a report, exit with a return code operations can grep in syslog. The batch processing pattern is not one statement—it is the repeatable skeleton teams use so every new job looks familiar. INIT at the top, JOB INPUT driving the loop, validation and UPDATE in the middle, counters incrementing quietly, REPORT or SORT activities when needed, TERM summarizing counts at the bottom. Beginners write brilliant record logic but forget EOF guards, leave counters uninitialized, or nest SORT inside the per-record loop by mistake. Veterans split every problem into JCL steps when one JOB could run SORT then REPORT sequentially. This page teaches the canonical JOB layout, JOB INPUT versus GET trade-offs, multi-file batch designs, embedding SORT and REPORT activities, restart considerations, performance habits, and how batch pattern connects to validation and error-handling frameworks elsewhere in this design patterns chapter.
1234567891011JOB PERFORM INIT-JOB JOB INPUT DRIVER-FILE ADD 1 TO READ-COUNT PERFORM PROCESS-RECORD END-JOB PERFORM TERM-JOB STOP
INIT-JOB opens auxiliary files, zeros counters, reads control parameters. PROCESS-RECORD holds business logic—validate, transform, WRITE output, optional PERFORM UPDATE. JOB INPUT implicitly reads DRIVER-FILE each iteration until EOF. TERM-JOB DISPLAYs or writes READ-COUNT, OK-COUNT, ERR-COUNT. STOP ends the program. END-JOB is alternative terminator depending on release and shop style; be consistent within your library.
| Phase | Purpose | Typical statements |
|---|---|---|
| Initialize | Setup once per run | PERFORM INIT-JOB, OPEN files |
| Read loop | Process each input record | JOB INPUT, ADD counters |
| Transform | Business rules | PERFORM VALIDATE, MOVE, COMPUTE |
| Output | Persist results | WRITE, UPDATE, REPORT LINE |
| Finalize | Summarize and close | PERFORM TERM-JOB, STOP |
Best when exactly one input record per loop iteration drives processing. Easytrieve reads automatically at each JOB pass. After JOB INPUT block body executes, the product fetches the next record. Test IF EOF DRIVER-FILE inside the block before using buffer fields when the last iteration might process empty buffer on some designs—know your release EOF semantics on the final cycle.
Use when only some iterations read—skip records based on prior logic—or when reading multiple files in one iteration. Manual GET inside PROCESS-RECORD with explicit IF EOF after each GET prevents reading when not needed. High-volume jobs sometimes GET from a presorted work file produced by an earlier SORT activity in the same program.
12345678910PROCESS-RECORD. PROC PERFORM VALIDATE-RECORD IF VAL-STATUS NE ZERO PERFORM WRITE-ERROR EXIT END-IF PERFORM TRANSFORM-FIELDS WRITE OUT-FILE ADD 1 TO WRITE-COUNT END-PROC
Keep PROCESS-RECORD at one level of abstraction—PERFORM subtasks, do not inline fifty lines. EXIT on validation failure avoids WRITE. TRANSFORM-FIELDS holds MOVE, COMPUTE, and MASK preparation. Separate PROCs make unit testing with DISPLAY checkpoints easier during development.
Batch pattern often runs SORT before the read loop when CONTROL reports or merge logic require ordered input. SORT activity belongs outside JOB INPUT loop—sort once, then JOB INPUT the sorted work file. Anti-pattern: SORT inside each iteration rebuilds entire file millions of times. Correct sequence: PERFORM INIT-JOB, SORT activity with USING and GIVING work files, JOB INPUT sorted file, PERFORM TERM-JOB.
1234567SORT USING UNSORTED-FILE GIVING SORTED-WORK KEY DEPT ASCENDING EMP-ID ASCENDING END-SORT JOB INPUT SORTED-WORK ... END-JOB
REPORT can run after processing completes—exception listing from ERROR-FILE via JOB INPUT on errors—or detail can print inside JOB INPUT using LINE in a REPORT tied to the same input. Pattern for exception-only report: first JOB pass writes ERROR-FILE, second JOB INPUT ERROR-FILE drives REPORT EXCEPTIONS with no UPDATE logic. Single-pass pattern: REPORT subactivity with JOB INPUT same file prints detail as records are read—efficient when input is presorted for CONTROL.
| Pattern | Description |
|---|---|
| Driver + lookup | JOB INPUT driver; GET or READ reference inside PROCESS-RECORD |
| Two-phase | First JOB INPUT builds work file; second JOB INPUT consumes it |
| Balanced merge | GET from two sorted files comparing keys in one loop PROC |
| Control file | GET parameter file once in INIT-JOB; JOB INPUT transaction file |
READ-COUNT, WRITE-COUNT, ERR-COUNT, SKIP-COUNT belong in working storage with RESET or explicit zero in INIT-JOB. TERM-JOB compares READ-COUNT to input trailer record when file carries batch header/trailer pattern. Mismatch triggers error-handling pattern hard stop. DISPLAY counts for operations; optionally WRITE statistics record to audit file keyed by run date and job name.
Batch jobs rerun after abends. Design PROCESS-RECORD idempotent when possible—UPDATE with same values twice should not double-apply financial effect. Use transaction keys and status fields on output records. Simulated checkpoint: every 10000 records WRITE restart key to CKPT-FILE; INIT-JOB reads CKPT-FILE and skips records until key matches—requires sorted input and documented rerun JCL. Many shops prefer simple full rerun from backup when idempotency is hard.
Batch pattern uses JOB, JOB INPUT, file WRITE, REPORT, STOP. Online uses SCREEN, BEFORE-SCREEN, AFTER-SCREEN, terminal GOTO. Shared Library PROCs for validation and date formatting cross both worlds when called from appropriate activity. Do not PERFORM screen-only PROCs from batch JOB—activity scope rules forbid crossing boundaries. Duplicate thin wrappers if needed.
Batch processing is a factory belt. Boxes (records) arrive one at a time. A worker checks each box, fixes what is inside, puts it on the outbound belt, and counts boxes. When no boxes are left, the worker writes the totals on a clipboard and goes home. The pattern is the factory layout—where the counter sits, where checks happen—so every new product uses the same belt instead of inventing a new factory each time.
1. JOB INPUT establishes:
2. Batch counters (READ-COUNT, WRITE-COUNT) typically reset:
3. Processing stops cleanly when:
4. Multiple input files in one batch JOB often use:
5. Batch pattern differs from online pattern because: