Operations ask for copies constantly: archive yesterday's extract, subset rows for a downstream team, migrate a flat file before a format change, duplicate test data with masked account numbers. IBM utilities like IEBGENER copy bytes fast when no logic is required. Easytrieve shines when copy means filter, transform, validate, or log skips. The file copy utility pattern packages the READ-WRITE loop, EOF handling, counters, and optional selection IF into one PERFORM COPY-FILE PROC beginners drop into any JOB. Without the pattern, each program reinvents ADD 1 TO READ-COUNT, forgets SKIP-COUNT when filtering, or WRITEs after EOF corrupting the output trailer. This page teaches full copy, filtered copy, field transformation during copy, GDG and JCL DD mapping, VSAM sequential access limits, performance on large files, error handling when output fills, and how copy utilities compose with batch processing and validation frameworks in production libraries.
1234567891011121314FILE SOURCE-FILE SEQUENTIAL FILE TARGET-FILE SEQUENTIAL JOB PERFORM INIT-COPY JOB INPUT SOURCE-FILE ADD 1 TO READ-COUNT WRITE TARGET-FILE ADD 1 TO WRITE-COUNT END-JOB PERFORM TERM-COPY STOP
INIT-COPY zeros counters. JOB INPUT reads each source record into buffer; WRITE copies entire buffer to TARGET-FILE. When SOURCE-FILE reaches EOF, loop ends. TERM-COPY DISPLAY READ-COUNT and WRITE-COUNT—they should match for full copy. No transformation—byte-preserving when record layouts identical and LRECL matches.
123456789INIT-COPY. PROC MOVE ZERO TO READ-COUNT WRITE-COUNT SKIP-COUNT END-PROC TERM-COPY. PROC DISPLAY 'COPY READ' READ-COUNT DISPLAY 'COPY WRITE' WRITE-COUNT DISPLAY 'COPY SKIP' SKIP-COUNT END-PROC
Wrap loop body in PERFORM COPY-RECORD for filtered variants. Parameterize by setting COPY-MODE flag in INIT—'F' full, 'S' subset, 'M' mask sensitive fields. One utility JOB cataloged in proclib; operators supply DD names via JCL without recompiling when file names are external DD mapping only.
123456789JOB INPUT SOURCE-FILE ADD 1 TO READ-COUNT IF REGION EQ 'EAST' WRITE TARGET-FILE ADD 1 TO WRITE-COUNT ELSE ADD 1 TO SKIP-COUNT END-IF END-JOB
Selection on any field—date range, status code, amount threshold. Complex filters PERFORM EVALUATE-COPY-RULE setting COPY-YES flag. SKIP-COUNT documents how many records were read but not written—operations reconcile READ = WRITE + SKIP. Multiple output files require IF branches with different WRITE targets or separate JOB passes per region.
Copy is not always identical WRITE. MOVE masked account to OUT-ACCT, reformat dates with MASK, COMPUTE tax column before WRITE to enriched output. Pattern: READ into input buffer, MOVE to output work area, transform, WRITE output layout FILE. Separate FILE definitions when output LRECL differs from input—define OUT-RECORD in Library with new field list.
123456789JOB INPUT SOURCE-FILE ADD 1 TO READ-COUNT MOVE IN-ACCT TO WORK-ACCT PERFORM MASK-ACCOUNT MOVE WORK-ACCT TO OUT-ACCT MOVE IN-AMT TO OUT-AMT WRITE TARGET-FILE ADD 1 TO WRITE-COUNT END-JOB
| DD name | Role | Notes |
|---|---|---|
| SOURCE | Input dataset | DISP=SHR on production read |
| TARGET | Output dataset | DISP=(NEW,CATLG,DELETE), SPACE |
| SYSOUT | Log | TERM-COPY DISPLAY destination |
| TARGET (+1) | GDG output | JCL manages generation; program WRITEs normally |
Sequential JOB INPUT works on QSAM and many ESDS sequential traversals. KSDS selective copy by key range uses START and READ NEXT in a PROC loop instead of JOB INPUT when documentation requires keyed positioning. Pure physical copy of entire KSDS often remains an IDCAMS or utility job; Easytrieve copy utility targets logical record extraction with business filters. Declare FILE type correctly—INDEXED with key fields when using keyed READ. Mismatch causes status errors HANDLE-ENV-ERROR should catch at INIT.
PERFORM VALIDATE-RECORD before WRITE; on failure PERFORM HANDLE-VALIDATION-ERROR writing ERROR-FILE instead of TARGET. COPY-utility-plus-validation is common for feeds from external partners—only clean records land in TARGET. OK-COUNT and ERR-COUNT supplement READ/WRITE/SKIP in TERM-COPY DISPLAY.
Files with batch headers: IF RECORD-TYPE = 'H' PERFORM WRITE-HEADER or skip copying header to target depending on spec. Trailer validation compares READ-COUNT to trailer count field—integrate with error-handling hard stop. Do not blindly WRITE header/trailer without business rules—downstream may expect different control record layout.
SORT activity can copy implicitly with GIVING when no keys specified on some platforms—still use explicit copy utility when no sort keys needed for clarity. SORT when output must be ordered; copy utility when order preserved from source. Combined pattern: COPY to work file, SORT work file, REPORT—three activities one JOB.
Copying a notebook means writing each page into a new notebook. The file copy utility is a photocopier instruction sheet: read one page, put it in the new book, count pages, skip coloring pages if you only want math pages. When done, tell the teacher how many pages you copied and how many you skipped.
1. Basic sequential copy loop uses:
2. Filtered copy adds:
3. COPY counters should track:
4. VSAM copy in Easytrieve typically:
5. Reusable copy utility exposes: