COMPUTE

You can use the COMPUTE statement for simple arithmetic computations involving two fields, or one field and a constant. The COMPUTE statement adds to (+), hash-adds to (P), subtracts from (-), hash-subtracts from (M), multiplies (*), or divides (/) the first (or destination) operand by the value specified in the second (or source) operand.

COMPUTE

(1)
(2) (3) (4)(5) (6)
(7) (8)

(5) Operator (+, -, *, /, P, M) | |

(4) Destination occ (constant/index) |
(8) Source occurrence (constant/index) |

The destination field can be packed decimal or binary, and the source field can be packed decimal, binary, or alpha. The packed decimal fields can have either a fixed or variable decimal position, and binary fields always have a fixed decimal position.

If the second field is an
alpha field type (valid only for *P* and *M* operators), characters
other than *0* to *9*, except trailing spaces, are converted
to *0*. Then, up to the first 31 characters are converted to a packed
decimal integer before the computation is performed. Once converted, the
remainder of the computation is identical to a computation that contains
a packed decimal integer for the second field. The hash total operators
ignore overflow.

If the result of a *P*
or *M* calculation results in a value greater than the destination
field can hold, the result wraps around from zero. For example, if a
destination field has a maximum of five digits, *99999 P 1* results
in a value of *00000*. The most significant number is discarded.
As another example, assuming that the packed decimal number could contain
a value up to *999*, *996 P 8* results in a value of *4*.

Similarly, if the result of the* P* or *M* calculation
results in a negative value, the result wraps around from the largest
value the destination field could hold plus *1*. For example, assuming
that the packed decimal number could contain a value up to *9999*,
then *3 M 6* results in a value of *9997*.

If there is a division by zero, or the result is too large to be stored in the destination field, an overflow situation occurs. Overflow processing is dependent on the use of the OVERFLOW statement.

For the* P* and *M* operators, the destination
field must be a packed decimal numeric type.

If OVERFLOW has not been specified, an overflow condition causes an unrecoverable processing error.

If either field contains a null value, no computation takes
place, and the statement following the **COMPUTE** is executed.

The following is an example
of a **COMPUTE **statement:

COMPUTE TAR ORDER2 SUBTOTAL + TAR ORDER3 LINE AMOUNT

In this example, the value in the field ORDER3 LINE AMOUNT is added to the value in ORDER2 SUBTOTAL. Note that the value in ORDER3 LINE AMOUNT is not changed by the computation.