The SUBTRACT statement subtracts one numeric item, or the sum of two or more numeric items, from one or more numeric items, and stores the result.

The SUBTRACT statement is used to subtract numeric items. There are three formats which works slightly differently and is described below,

The first format is used to subtract field(s) or value(s) from another field. The field(s) or value(s) listed between SUBTRACT and FROM are summed and subtracted from the value of the field(s) following the FROM. The answer is stored in the individual field.

SUBTRACT {indentifier-1 or literal-1}...FROM {indentifier-2 [ROUNDED]}... [ON SIZE ERROR imperative-statement-1] [NOT ON SIZE ERROR imperative-statement-2] [END-SUBTRACT] |

SUBTRACT A FROM B |

- The value in A is subtracted from the value in B and the result is stored in B. The value in A is unchanged.

SUBTRACT C D FROM E F |

- The sum of the values in C and D is subtracted to the value in E, storing the answer in E and the sum of C and D is also subtracted from F, storing the value in F. The values in C and D are unchanged.

The second format is used to subtract field(s) or value(s) from one another, storing the answer in a different field. The field(s) or value(s) listed between SUBTRACT and GIVING are added and stored in the field following the field(s) following the GIVING.

SUBTRACT {indentifier-1 or literal-1}...FROM {indentifier-2 or literal-2} GIVING identifier-3 [ROUNDED] [ON SIZE ERROR imperative-statement-1] [NOT ON SIZE ERROR imperative-statement-2] [END-SUBTRACT] |

SUBTRACT A FROM B GIVING C |

- The value in A is subtracted to the value in B and the result is stored in C. The values in A and B are unchanged.

The third format is used to subtract subordinate field(s) of one group from subordinate field(s) in another, storing the answer in those fields.

Those subordinate field(s) of the group item identifier-1 are subtracted from and stored in those with exactly the same name in the group item identifier-2. The names of the subordinate items must be spelled exactly the same way in both groups to qualify to participate in the subtraction.

SUBTRACT {CORRESPONDING or CORR} indentifier-1 FROM indentifier-2 [ROUNDED] [ON SIZE ERROR imperative-statement-1] [NOT ON SIZE ERROR imperative-statement-2] [END-SUBTRACT] |

Optional phrases in MULTIPLY operation:

END-SUBTRACT permits a conditional SUBTRACT statement to be nested in another conditional statement. END-SUBTRACT can also be used with an imperative SUBTRACT statement.

- The fields to be subtracted must have numeric pictures, that is, they can only have the characters 9, S, and V in their PIC clauses.

- Receiving fields may be either numeric or numeric edited fields.

- Use the SIZE ERROR clause to detect field overflow on the receiving field.

- In all formats, the mathematically correct results are computed, but if the receiving field is too short in either the integer or decimal portion, the result will be truncated, the integer on the left and the decimal on the right.

Including the ROUNDED phrase will result the answer field being rounded instead of truncating. Roundig is always done in the least significant portion of the answer.

* If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!*