OUTREC Arithmetic with numeric and constants
OUTREC Arithmetic with numeric and constants
The OUTREC statement can be used to perform arithmetic operations with numeric fields and constants. This allows us to add, subtract, multiply, or divide values from files, modify the output records, and include calculated results.
Syntax -
//SYSIN DD *
OUTREC FIELDS=(starting_pos_of_field1,
field1_length,
field1_data_type,
arithmetic_operator,
+n/-n,…)
/*
starting_pos_of_field1 | Specifies the starting byte of the numeric field in the record. |
field1_length | Specifies the length of the numeric field. |
field1_data_type | Specifies the numeric data type, such as ZD (Zoned Decimal), PD (Packed Decimal), or BI (Binary). |
arithmetic_operator | Specifies the arithmetic operation to be performed (minimum-MIN,maximum-MAX, addition-ADD, subtraction-SUB, multiplication-MUL, or division-DIV). |
+n/-n | Specifies the constant value to be used in the arithmetic operation. |
The order of operators assessment is shown below -
- MIN and MAX
- MUL, DIV and MOD
- ADD and SUB
Points to Note -
- The result of an arithmetic operation is a 15-digit ZD value.
- The result of an arithmetic operation can be converted to a different numeric format, or edited using pre-defined edit masks or user-defined edit masks.
Examples -
Scenario1 - Adding a Constant to a Numeric Field.
//SYSIN DD *
SORT FIELDS=COPY
OUTREC FIELDS=(1,10,11,5,ZD,ADD,+100)
/*
Adds 100 to the 5-byte zoned decimal field starting at position 11. The result is stored in the output record in the same position.
Scenario2 - Subtracting a Constant from a Numeric Field.
//SYSIN DD *
SORT FIELDS=COPY
OUTREC FIELDS=(1,10,20,4,ZD,SUB,50)
/*
Subtracts 50 from the 4-byte zoned decimal field starting at position 20.
Scenario3 - Dividing a Numeric Field by a Constant.
//SYSIN DD *
SORT FIELDS=COPY
OUTREC FIELDS=(1,10,11,5,ZD,DIV,+5)
/*
Divides the 5-byte zoned decimal field starting at position 11 by 5.