SUBTRACT Statement
The SUBTRACT Statement performs an arithmetic subtract operation on a number of operands and allows for the storage of the result in a number of data items.
Format 1
SUBTRACT {integer-1} ... FROM {integer-data-1 ROUNDED] } ...
[ ON SIZE ERROR statement-1 ]
[ NOT ON SIZE ERROR statement-2 ]
[ END-SUBTRACT ]
Format 2
SUBTRACT {integer-2} ... FROM integer-3
GIVING { integer-data-2 [ROUNDED] } ...
[ ON SIZE ERROR statement-1 ]
[ NOT ON SIZE ERROR statement-2 ]
[ END-SUBTRACT ]
Format 3
SUBTRACT {CORRESPONDING} group-1 FROM group-2 [ROUNDED]
{CORR }
[ ON SIZE ERROR statement-1 ]
[ NOT ON SIZE ERROR statement-2 ]
[ END-SUBTRACT ]
Syntax:
integer-nis a data element, literal or data returned from a function call that is an integer.integer-data-nis a data item that is an integer.group-nis a group data item containing one or more elementary data items.statement-nis an imperative stat ement.- The
SIZE ERRORexception is triggered if the receiving field is not large enough to accommodate the result of theSUBTRACTfunction. - In a
SUBTRACT CORRESPONDINGoperation, elementary items in separate group items must have the same elementary data name for theSUBTRACTfunction to be performed.
General Rules:
- In a
FORMAT 1 SUBTRACTstatement with noGIVINGclause, allinteger-ndata items are subtracted, in sequence, from each of the data items following theFROMclause. - In a
FORMAT 2 SUBTRACTstatement containing aGIVINGclause, allinteger-ndata items are subtracted, in sequence, from the data item following theFROMclause with the result stored in the integer-data item following theGIVINGclause - The
ROUNDEDclause is applied when an arithmetic operation produces a result that includes more decimal places than are included in the description of the data item given to hold the final result of the arithmetic operation. - For rules regarding the
ROUNDEDclause, see the entry for ROUNDED in the Common General Rules section. - The
SIZE ERRORexception is triggered if theON SIZE ERRORclause is present and if the receiving field is not large enough to accommodate the result of theSUBTRACTfunction. - For rules regaring the
ON SIZE ERRORclause, see the entry for ON SIZE ERROR in the Common General Rules section. - In a
SUBTRACT CORRESPONDINGoperation, elementary items in separate group items must have the same elementary data name for theSUBTRACTfunction to be performed. - The
SUBTRACT CORRESPONDINGoperation causes multipleSUBTRACToperations to be performed between elementary data items in separate group items, where the elementary items have the same elementary data name, and are not described asFILLER. - The rules for identifying a
CORRESPONDINGdata item for the purposes of theSUBTRACT CORRESPONDINGclause are the same as the rules used for the purposes of theMOVE CORRESPONDINGclause. For more detail, see the General Rules of the MOVE CORRESPONDING clause. - For details on how data items are identified as
CORRESPONDING, see the entry Rules for identifying CORRESPONDING data in the Common General Rules section. - In a
SUBTRACT CORRESPONDINGoperation, allSUBTRACToperations will be completed before theON SIZE ERRORcondition will be triggered.
Code Sample:
IDENTIFICATION DIVISION.
PROGRAM-ID. SUBTRACT-1.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 FIELD-1 PIC 9V9.
77 FIELD-2 PIC 9V9.
77 FIELD-3 PIC9.9.
01 GROUP-1.
03 FLD-1 PIC 99 VALUE 5.
03 FLD-2 PIC 99 VALUE 15.
01 GROUP-2.
03 FLD-1 PIC 99 VALUE 10.
03 FLD-2 PIC 99 VALUE 20.
77 DUMMY PIC X.
PROCEDURE DIVISION.
MAIN.
*SUBTRACT {INTEGER-1} ... FROM {INTEGER-2 [ROUNDED] } ...
* [ ON SIZE ERROR STATEMENT-1 ]
* [ NOT ON SIZE ERROR STATEMENT-2 ]
* [ END-SUBTRACT ]
*
MOVE 2.5 TO FIELD-1.
MOVE 3.4 TO FIELD-2.
SUBTRACT FIELD-1 FROM FIELD-2
ON SIZE ERROR
DISPLAY "INVALID SUBTRACTION" LINE 10 COL 10
NOT ON SIZE ERROR
DISPLAY FIELD-2 LINE 10 COL 10
END-SUBTRACT.
*SUBTRACT {INTEGER-3} ... FROM INTEGER-4
* GIVING { INTEGER-5 [ROUNDED] } ...
* [ ON SIZE ERROR STATEMENT-1 ]
* [ NOT ON SIZE ERROR STATEMENT-2 ]
* [ END-SUBTRACT ]
*
MOVE 2.5 TO FIELD-1.
MOVE 3.4 TO FIELD-2.
SUBTRACT FIELD-1 FROM FIELD-2 GIVING FIELD-3
ON SIZE ERROR DISPLAY "INVALID SUBTRACTION" LINE 10 COL 10
NOT ON SIZE ERROR DISPLAY FIELD-3 LINE 10 COL 10
END-SUBTRACT.
*SUBTRACT {CORRESPONDING} GROUP-1 FROM GROUP-2 [ROUNDED]
* {CORR }
* [ ON SIZE ERROR STATEMENT-1 ]
* [ NOT ON SIZE ERROR STATEMENT-2 ]
* [ END-SUBTRACT ]
SUBTRACT CORRESPONDING GROUP-1 FROM GROUP-2
ON SIZE ERROR
DISPLAY "INVALID SUBTRACTION" LINE 10 COL 10
NOT ON SIZE ERROR
DISPLAY FLD-1 OF GROUP-2 LINE 12 COL 10
DISPLAY FLD-2 OF GROUP-2 LINE 13 COL 10
END-SUBTRACT.
DISPLAY "SUBTRACT-1 FINISHED!" LINE 15 COL 10.
ACCEPT DUMMY LINE 15 COL 30.
STOP RUN.