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.
Format 1: SUBTRACT statement .------------------. V | >>-SUBTRACT----+-identifier-1-+-+--FROM-------------------------> '-literal-1----' .---------------------------. V | >----identifier-2--+---------+-+--------------------------------> '-ROUNDED-' >--+--------------------------------------------+---------------> '-+----+--SIZE ERROR--imperative-statement-1-' '-ON-' >--+-------------------------------------------------+----------> '-NOT--+----+--SIZE ERROR--imperative-statement-2-' '-ON-' >--+--------------+-------------------------------------------->< '-END-SUBTRACT-'
All identifiers or literals preceding the keyword FROM are added together and their sum is subtracted from and stored immediately in identifier-2. This process is repeated for each successive occurrence of identifier-2, in the left-to-right order in which identifier-2 is specified.
Format 2: SUBTRACT statement with GIVING phrase .------------------. V | >>-SUBTRACT----+-identifier-1-+-+--FROM--+-identifier-2-+-------> '-literal-1----' '-literal-2----' .---------------------------. V | >--GIVING----identifier-3--+---------+-+------------------------> '-ROUNDED-' >--+--------------------------------------------+---------------> '-+----+--SIZE ERROR--imperative-statement-1-' '-ON-' >--+-------------------------------------------------+----------> '-NOT--+----+--SIZE ERROR--imperative-statement-2-' '-ON-' >--+--------------+-------------------------------------------->< '-END-SUBTRACT-'
All identifiers or literals preceding the keyword FROM are added together and their sum is subtracted from identifier-2 or literal-2. The result of the subtraction is stored as the new value of each data item referenced by identifier-3.
Format 3: SUBTRACT statement with CORRESPONDING phrase >>-SUBTRACT--+-CORRESPONDING-+--identifier-1--FROM--------------> '-CORR----------' >--identifier-2--+---------+------------------------------------> '-ROUNDED-' >--+--------------------------------------------+---------------> '-+----+--SIZE ERROR--imperative-statement-1-' '-ON-' >--+-------------------------------------------------+----------> '-NOT--+----+--SIZE ERROR--imperative-statement-2-' '-ON-' >--+--------------+-------------------------------------------->< '-END-SUBTRACT-'
Elementary data items within identifier-1 are subtracted from, and the results are stored in, the corresponding elementary data items within identifier-2.
When the ARITH(COMPAT) compiler option is in effect, the composite of operands can contain a maximum of 30 digits. When the ARITH(EXTEND) compiler option is in effect, the composite of operands can contain a maximum of 31 digits. For more information about arithmetic intermediate results, see the Enterprise COBOL Programming Guide.
For all formats:
In format 2, 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.
In format 3, must name an alphanumeric group item or a national group item.
The following restrictions apply to date fields:
There are two steps to determining the result of a SUBTRACT statement that involves one or more date fields:
Floating-point data items and literals can be used anywhere numeric data items and literals can be specified.
For information about the ROUNDED phrase, and for operand considerations, see ROUNDED phrase.
For information about the SIZE ERROR phrases, and for operand considerations, see SIZE ERROR phrases.
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.
For more information, see Delimited scope statements.