TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

COBOL - Subtract statement


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,


Format 1: SUBTRACT statement

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.

Syntax:

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]

identifier must name an elementary numeric data item.

literal Must be a numeric literal. Floating-point data items and literals can be used anywhere numeric data items and literals can be specified.

Example 1:

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.

Example 2:

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.
Format 2: SUBTRACT statement with GIVING phrase

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.

Syntax:

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]

identifier must name an elementary numeric data item, unless the identifier follows the word GIVING. Each identifier following the word GIVING must name a numeric or numeric-edited elementary data item.

literal Must be a numeric literal. Floating-point data items and literals can be used anywhere numeric data items and literals can be specified.

Example:

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.
Format 3: SUBTRACT statement with CORRESPONDING phrase

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.

Syntax:

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]

identifier must name an alphanumeric group item or a national group item.

literal Must be a numeric literal. Floating-point data items and literals can be used anywhere numeric data items and literals can be specified.

Optional phrases in MULTIPLY operation:

ROUNDED phrase:

With ROUNDED option, the computer will always round the result to the PICTURE clause specification of the receiving field. It is usually coded after the field to be rounded.

SIZE ERROR phrases:

If this is coded with any operation that ended with SIZE error will not be carried out but the statement follows ON SIZE ERROR will be executed.

NOT ON SIZE ERROR phrases:

This is opposite to SIZE ERROR. If this is coded with any operation that not ended with SIZE error, then the statement follows ON SIZE ERROR will be executed.

END-SUBTRACT phrase:

This explicit scope terminator serves to delimit the scope of the SUBTRACT statement.

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.


Tips:

  • 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!

Are you looking for Job Change? Job Portal