SUM statement


The SUM statement is used to sum numeric fields across records with identical key values. When multiple records have the same key, DFSORT sums the specified numeric fields and eliminates duplicates, retaining only the summed record.

The data types allowed to be coded with the SUM statement are -

  • binary (BI)
  • fixed-point (FI)
  • packed decimal (PD)
  • zoned decimal (ZD)
  • floating-point (FL)

Syntax -

//SYSIN    DD *
  SORT FIELDS=...
  SUM FIELDS=(field1_starting_pos_in_input_file,
        field1_length,
		field1_data_type,...)
/*
field1_starting_pos_in_input_fileSpecifies starting position of field1
field1_lengthSpecifies length of the field1
field1_data_typeSpecifies the format of the field1 like CH, PD, BI etc,.

Examples -


Scenario1 - Summing zoned decimal fields with NOVLSHRT.

//SYSIN   DD  *
  OPTION NOVLSHRT
  SORT FIELDS=(1,10,CH,A)
  SUM FIELDS=(11,5,ZD),NOVLSHRT
/*

Summing the 5-byte zoned decimal field starting at position 11. If the sum of values exceeds the field length (5 bytes in this case), DFSORT will terminate with an overflow error instead of truncating the result.

Scenario2 - Summing zoned decimal fields with VLSHRT.

//SYSIN   DD  *
  OPTION VLSHRT
  SORT FIELDS=(1,10,CH,A)
  SUM FIELDS=(11,5,ZD)
/*

Summing the 5-byte zoned decimal field starting at position 11. If the sum exceeds the 5-byte zoned decimal field length, the result is truncated to fit within the field size, avoiding overflow errors.