Showing posts with label Easytrieve VFM. Show all posts
Showing posts with label Easytrieve VFM. Show all posts

Monday, 29 July 2013

Easytrieve SORT Statement: USING, D, and BEFORE Examples

A payroll extract must group employees by department and place the highest gross pay first within each group. In Easytrieve, that requirement belongs to a separate SORT activity: it reads an input file sequentially, passes records to the installation's sort program, and writes the ordered records to an output file.

Easytrieve SORT statement flow from input file through sort keys to output file
Easytrieve SORT sends an input file through ordered keys to a sorted output file.

What the Easytrieve SORT activity does

The Easytrieve SORT statement is an activity, like a JOB activity, rather than a statement inside JOB logic. It sequences records from one defined file into another defined file. The actual ordering work is performed through the sort facility configured at the installation, so work-space allocation, messages, and product-specific options can differ between sites.

Keep the activity boundary in mind when reading an Easytrieve program structure. FILE definitions belong in the library section. The SORT activity follows those definitions, and a later JOB activity can read the sorted output.

Easytrieve SORT statement syntax

SORT input-file TO output-file +
     USING (major-key minor-key [D] ...) +
     [BEFORE procedure-name] +
     [NAME sort-name]
ItemPurposeOperational check
input-fileNames the FILE definition read by the activity.The organization must support sequential processing for the sort input.
TO output-fileNames the FILE definition that receives sorted records.Confirm record length and field layout before a downstream JOB reads it.
USINGLists sort keys from major to minor.Each key must be a field associated with the input record being sorted.
DSorts the immediately preceding key in descending order.A key without D is ascending.
BEFOREInvokes a sort procedure for every input record before release to the sort.The procedure must immediately follow the SORT activity.
NAMEAssigns a name to the SORT activity.Use a descriptive activity name; it is not a data set name.

Complete ascending-key example

This example orders employees by department and then by name. Department is the major key because it appears first.

FILE PERSNL  FB(19 1900)
  NAME       1  10 A
  DEPT      11   5 N
  GROSS-PAY 16   4 P 2

FILE PAY-SORT FB(19 1900)
  SORT-NAME       1  10 A
  SORT-DEPT      11   5 N
  SORT-GROSS-PAY 16   4 P 2

SORT PERSNL TO PAY-SORT +
     USING (DEPT NAME) +
     NAME DEPT-NAME-SORT

Records are ordered by DEPT ascending. Records with the same department are then ordered by NAME ascending. The output field names can differ, but the positions, lengths, and types must describe the record that the next activity will receive. Review the Easytrieve FILE definition guide when the layouts are not identical.

Mixed ascending and descending keys

Place D immediately after the field that must be descending:

SORT PERSNL TO PAY-SORT +
     USING (DEPT GROSS-PAY D NAME) +
     NAME DEPT-PAY-SORT

The result is department ascending, gross pay descending within each department, and name ascending when both earlier keys compare equal. Moving D to another position changes which key is reversed; it does not change every key that follows.

Major and minor key order

Easytrieve reads the USING list from major to minor. If payroll staff need the highest-paid employee first inside each department, code DEPT before GROSS-PAY D. Coding gross pay first would mix departments whenever employees have the same pay value.

Field type also matters. An N or packed-decimal P field is compared as its declared numeric type; an A field is character data. A wrong position, length, or type in the FILE definition can produce an order that looks like a sort failure even when the sort facility followed the supplied key definition exactly.

Filter records with a BEFORE procedure

BEFORE runs a named procedure once for each input record before that record is passed to the sort. It is the Easytrieve mechanism for screening records or changing record contents as part of the SORT activity.

SORT PERSNL TO PAY-SORT +
     USING (DEPT GROSS-PAY D) +
     BEFORE SELECT-HIGH-PAY +
     NAME HIGH-PAY-SORT

SELECT-HIGH-PAY. PROC
  IF GROSS-PAY GE 500
    SELECT
  END-IF
END-PROC

The procedure is placed immediately after the SORT statement. SELECT releases the current record to the sort. In this example, a record below 500 is not selected and therefore does not appear in PAY-SORT.

Why SELECT is required with BEFORE

Once a BEFORE procedure is present, each record intended for the sorted output must execute SELECT. Omitting it can leave the output empty even though the input file opened and the procedure ran. Selecting the same input record more than once does not create multiple output copies; the record is returned once.

Diagnostic clue: if the sort ends normally but writes zero records, inspect the BEFORE conditions and confirm that a reachable SELECT executes for the expected input rows.

Using a VIRTUAL sorted output file

A VIRTUAL file is useful when the sorted records are consumed by another activity in the same Easytrieve program and no permanent output data set is required. A compact pattern is:

FILE PAY-SORT F(19) VIRTUAL

SORT PERSNL TO PAY-SORT +
     USING (DEPT GROSS-PAY D)

JOB INPUT PAY-SORT NAME PRINT-SORTED
  PRINT PAY-RPT

Broadcom notes that the Virtual File Manager is involved when a SORT statement is used. Treat that as runtime behavior, not as permission to ignore storage and installation settings. The program's input, output, and report flow should still be explicit. The Easytrieve JOB statement guide explains how the following activity reads the sorted file.

SORT activity versus report SEQUENCE

Use SORT when another file or activity needs records in a new order. Use the report SEQUENCE declarative when the requirement is to order report lines and support control breaks or totals without creating a separately sorted application file.

RequirementUseReason
Create sorted records for later processingSORT activityWrites ordered records to the named output file.
Filter records before sortingSORT with BEFOREThe procedure evaluates each input record and SELECT controls inclusion.
Order only a printed reportREPORT SEQUENCEOrdering belongs to report processing rather than a separate output file.
Produce subtotals by a report fieldSEQUENCE with CONTROL/SUM as requiredThe report facility groups and totals the sequenced lines.

See Easytrieve basic reporting for FILE, JOB, REPORT, PRINT, and LINE context.

Sort work and installation options

Easytrieve interfaces with the site's sort product. Broadcom's option-file categories include sort options for storage and sort messages. This means a valid Easytrieve statement can still fail because of missing work DD statements, insufficient temporary space, installation limits, or a site-specific option. Capture the Easytrieve message and the underlying sort product message before changing application logic.

The supported number of keys and vendor option syntax depend on the installed environment. Avoid copying sort-control options from a different site without checking the local Easytrieve 11.6 documentation and the installed sort product manual.

Common Easytrieve SORT errors

  • Undefined file name: the input or output name has no matching FILE definition, or the spelling differs.
  • Wrong key order: a minor key is coded before the intended major key.
  • Wrong descending marker: D follows the wrong field and reverses that key instead.
  • Missing SELECT: a BEFORE procedure executes, but no qualifying path releases records.
  • Misplaced procedure: the named sort procedure does not immediately follow its SORT activity.
  • Layout mismatch: the output record definition does not match the bytes written by the sort.
  • Numeric field defined as character: values are ordered by character representation instead of the intended numeric value.
  • Runtime sort failure: the application syntax is valid, but work-space or installation options cause the system sort to fail.

Production review checklist

  1. Confirm both FILE names and record layouts.
  2. List USING fields in major-to-minor order.
  3. Place D only after each descending key.
  4. If BEFORE is coded, place its procedure immediately after SORT and trace every path to SELECT or intentional rejection.
  5. Confirm whether the output should be permanent or VIRTUAL.
  6. Check the downstream Easytrieve program activity that consumes the sorted records.
  7. Retain the Easytrieve and system-sort messages when diagnosing a failure.

Broadcom Easytrieve references

Easytrieve SORT statement FAQ

Is ascending or descending the Easytrieve SORT default?

Ascending is the default. Add D immediately after a key field when that specific key must be descending.

What does BEFORE do on an Easytrieve SORT statement?

BEFORE invokes a procedure for each input record before release to the sort. The procedure can test or modify the current record, and SELECT determines whether it enters the sorted output.

Why did a SORT with BEFORE produce an empty file?

The usual application-level cause is that no reachable path executed SELECT. Check the condition, field definition, and procedure placement before investigating sort work-space failures.

Should a report use SORT or SEQUENCE?

Use SORT when you need an ordered output file or a later activity must read ordered records. Use report SEQUENCE when only the report lines need ordering, control breaks, or totals.

Final check: read the USING list aloud as “major key, then minor key,” and verify that every D and every BEFORE-path SELECT matches that sentence.

Easytrieve Library Section: FILE, VFM, COPY, and EXIT

Easytrieve file declarations

FILE CUSTIN FB(120 1200) tells Easytrieve how a data set is organized before a JOB reads it. If the record format or length disagrees with the DD statement or catalog entry, the program can fail before its business logic processes a record. The Easytrieve Library section is where that contract, the record layout, and program work fields are declared.

Easytrieve Library section showing JCL DD, FILE declaration, record layout, and a VIRTUAL work file
A physical file links a JCL DD to a FILE declaration and record layout; a VIRTUAL work file needs no matching DD.
Scope: this page covers Library-section declarations. For the order of Environment, Library, and Activity source, see Easytrieve program structure.

What the Easytrieve Library section does

The Library section sits after optional Environment-section PARM statements and before the first JOB or SORT activity. It describes the data that executable statements will use. Typical entries include input and output FILE statements, fields associated with those files, working-storage fields, COPY statements, and optional file-exit information.

DeclarationPurposeOperational check
FILENames a file and can describe organization, record format, length, and processing attributes.For a physical z/OS file, confirm that the file name matches the JCL DD name and that DCB attributes agree.
File fieldsMap names to positions, lengths, data types, and decimal places in a record.Ensure that the last field does not extend beyond the record length.
Working storageDefines counters, switches, totals, and other fields not read directly from a file record.Choose a type and length that suit the calculation or comparison.
COPYDuplicates the field layout of a file already declared in the source.Qualify duplicated field names when both files are referenced.
EXITCalls a user routine around supported file I/O.Verify the interface, parameters, supported file type, and installation conventions.

Easytrieve FILE statement and record layout

A simple disk-file definition can carry explicit record attributes, or it can rely on attributes available through allocation. Site standards differ, so treat the JCL, catalog entry, and Easytrieve source as one definition. Do not copy an FB or VB declaration from another job without checking the real data set.

FILE CUSTIN FB(120 1200)
  CUSTOMER-ID       1   10 A
  CUSTOMER-NAME    11   30 A
  REGION-CODE      41    3 A
  BALANCE          44    9 P 2

WS-SELECTED        W     7 N 0
WS-HIGH-BALANCE    W     9 P 2

Here, CUSTIN is the file name used by later Easytrieve statements. The file fields describe bytes within its logical record. The W fields are program storage rather than part of CUSTIN. For the full rules behind position, length, type, masks, and redefinitions, use the separate Easytrieve field-definition guide.

When can the FILE statement omit attributes?

Some z/OS input files can obtain record information from the allocated data set. Coding only FILE CUSTIN may therefore work at one installation. It also removes a useful compile-time check. Broadcom documents B055 cases where defined fields extend beyond the FILE length; removing the length suppresses that comparison but does not correct an inaccurate layout. The safer response is to verify the record definition and update the length when the source is wrong.

F versus FB and compiler message B049

F(80) describes fixed-length records with a logical record length. If a second numeric value is supplied for block size, the format must support blocking. Broadcom's B049 example changes F(730 13870) to FB(730 13870). Make that change only when it matches the data set's actual RECFM and sizes.

Production check: compare Easytrieve FILE attributes with the allocation shown in the job log, catalog, or data-set listing. A syntactically valid declaration can still describe the wrong bytes.

VFM and VIRTUAL work files

Virtual File Manager (VFM) provides sequential work files during an Easytrieve execution. Code VIRTUAL on the FILE statement; the work file does not require a matching JCL DD. A common use is to hold sorted or selected records for a later activity in the same program.

FILE SORTWK VIRTUAL FB(80 800)
  WK-ACCOUNT        1   10 A
  WK-NAME          11   30 A
  WK-AMOUNT        41    9 P 2

SORT CUSTIN TO SORTWK USING (REGION-CODE CUSTOMER-ID)

JOB INPUT SORTWK
  PRINT REGION-RPT

The SORT activity and its exact syntax belong on the Easytrieve sorting page; the example here shows why the Library declaration exists. VFM uses configured memory and can spill excess work data to an installation-defined disk area. Large sorts or multiple sequenced reports can therefore create real DASD activity even though the source says VIRTUAL.

Use RETAIN only when another read is required

Without RETAIN, a VIRTUAL file is normally consumed and released after it is read. Add RETAIN when later activities must read the same VFM file again during the program execution.

FILE SELECTED VIRTUAL RETAIN FB(80 800)

RETAIN does not turn the work file into a permanent cataloged data set. Broadcom states that the retained VFM file lasts until the Easytrieve program ends. For a persistent output consumed by another job, define a physical output file and allocate it in JCL.

COPY a record layout without repeating every field

The Easytrieve COPY statement duplicates field definitions from a previously declared file. It is useful when an input and output record share the same layout.

FILE FILEA FB(80 800)
  ACCOUNT-NO        1   10 A
  ACCOUNT-NAME     11   30 A
  STATUS-CODE      41    1 A

FILE FILEB FB(80 800)
  COPY FILEA

JOB INPUT FILEA
  IF FILEA:STATUS-CODE EQ 'A'
     MOVE FILEA:ACCOUNT-NO TO FILEB:ACCOUNT-NO
     PUT FILEB
  END-IF

COPY creates the same field names under both files. When a statement could refer to either copy, prefix the field with its file name, as in FILEA:ACCOUNT-NO. Broadcom identifies an unqualified duplicate as a cause of ambiguity message B039. IBM also documents file qualification for COPY-generated names.

COPY boundary: this statement reuses a record layout inside Easytrieve source. It is separate from Easytrieve macros, which are covered in the Easytrieve macro guide.

FILE EXIT for specialized I/O

An EXIT parameter associates a user routine with supported file I/O. The routine can adapt data that normal Easytrieve processing does not handle directly. IBM's Migration Utility documentation shows a non-MODIFY sequential-file form such as:

FILE FILE01 DISK F(80) EXIT (FSYTIXIT)

A non-MODIFY exit receives the file-record address and a request code, followed by fields named in USING when present. With MODIFY, the exit can inspect or change a record after input or before output. The exact interface and linkage rules are product- and installation-sensitive; start from a supplied sample and confirm them with the Easytrieve administrator.

IBM documents FILE EXIT support for sequential and VSAM files in this context and notes that Easytrieve Plus does not support it for DLI/IMS, IDMS, or Db2. For ordinary keyed-file logic, use the Easytrieve VSAM file-handling guide rather than adding a custom exit.

How Library declarations connect to an activity

The Library section defines names and layouts; a JOB or SORT later opens or processes them. In automatic input, the JOB names the input file. REPORT and LINE definitions then describe presentation rather than storage.

FILE CUSTIN FB(120 1200)
  CUSTOMER-ID       1   10 A
  CUSTOMER-NAME    11   30 A
  BALANCE          44    9 P 2

JOB INPUT CUSTIN NAME CUSTOMER-LIST
  IF BALANCE GT 0
     PRINT CUST-RPT
  END-IF

REPORT CUST-RPT
  TITLE 1 'CUSTOMERS WITH A BALANCE'
  LINE 1 CUSTOMER-ID CUSTOMER-NAME BALANCE

See the Easytrieve JOB statement for automatic input options and the Easytrieve reporting guide for REPORT, TITLE, and LINE behavior.

Common Library-section errors

SymptomLikely causeCheck
B049 parameter ignoredTwo size values were coded with F rather than a compatible blocked format.Verify RECFM, LRECL, and block size; use FB only when it matches the data set.
B055 field exceeds fileThe record layout extends beyond the FILE length.Find the last defined byte and reconcile it with the real LRECL instead of merely removing the length.
B039 ambiguous nameCOPY or repeated layouts created the same field name in multiple files.Use a qualifier such as FILEA:FIELD1.
File-not-found or open failureA physical FILE name has no matching DD, or the allocation is unsuitable.Compare the FILE name with the execution JCL and review allocation messages.
Unexpected valuesA field position, length, or type does not match the incoming record.Inspect sample bytes and compare the layout with the producer's copybook.
VFM space problemA work file or sequenced report exceeded configured VFM memory and spill resources.Review EZTVFM allocation and VFM settings with the Easytrieve administrator.

Library-section review checklist

  1. Place all Library declarations before the first JOB or SORT activity.
  2. Match each physical file name to its JCL DD name.
  3. Verify RECFM, LRECL, and any block size against allocation data.
  4. Confirm that field endpoints stay within the logical record.
  5. Qualify fields duplicated through COPY.
  6. Use VIRTUAL for program-lifetime work data and RETAIN only when repeated reads are needed.
  7. Treat EXIT routines as a controlled interface with documented linkage and return behavior.

Official references

Frequently asked questions

What belongs in the Easytrieve Library section?

The Library section contains FILE declarations, file-associated record fields, and working-storage definitions used by later JOB or SORT activities. It follows optional PARM settings and precedes the Activity section.

Does an Easytrieve VIRTUAL file need a JCL DD statement?

No. VFM creates a VIRTUAL work file for the program, so it does not need a matching JCL DD statement. A spill data set may still be used internally when the configured VFM memory is exhausted.

Why does COPY create ambiguous Easytrieve field names?

COPY duplicates another file's field definitions. When both files participate in the same activity, qualify a duplicated name with its file, such as FILEA:ACCOUNT and FILEB:ACCOUNT, to avoid ambiguity errors.

What causes Easytrieve B049 on a FILE statement?

A common cause is coding two numeric values with record format F. If block size is supplied as the second value, use FB when that matches the real data set attributes, and verify the declaration against the JCL or catalog DCB.

Working rule: when a file fails, compare the Library declaration, field endpoints, JCL DD, and catalog attributes before changing the JOB logic.

New In-feed ads