Skip to content

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:

  1. integer-n is a data element, literal or data returned from a function call that is an integer.
  2. integer-data-n is a data item that is an integer.
  3. group-n is a group data item containing one or more elementary data items.
  4. statement-n is an imperative stat ement.
  5. The SIZE ERROR exception is triggered if the receiving field is not large enough to accommodate the result of the SUBTRACT function.
  6. In a SUBTRACT CORRESPONDING operation, elementary items in separate group items must have the same elementary data name for the SUBTRACT function to be performed.

General Rules:

  1. In a FORMAT 1 SUBTRACT statement with no GIVING clause, all integer-n data items are subtracted, in sequence, from each of the data items following the FROM clause.
  2. In a FORMAT 2 SUBTRACT statement containing a GIVING clause, all integer-n data items are subtracted, in sequence, from the data item following the FROM clause with the result stored in the integer-data item following the GIVING clause
  3. The ROUNDED clause 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.
  4. For rules regarding the ROUNDED clause, see the entry for ROUNDED in the Common General Rules section.
  5. The SIZE ERROR exception is triggered if the ON SIZE ERROR clause is present and if the receiving field is not large enough to accommodate the result of the SUBTRACT function.
  6. For rules regaring the ON SIZE ERROR clause, see the entry for ON SIZE ERROR in the Common General Rules section.
  7. In a SUBTRACT CORRESPONDING operation, elementary items in separate group items must have the same elementary data name for the SUBTRACT function to be performed.
  8. The SUBTRACT CORRESPONDING operation causes multiple SUBTRACT operations 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 as FILLER.
  9. The rules for identifying a CORRESPONDING data item for the purposes of the SUBTRACT CORRESPONDING clause are the same as the rules used for the purposes of the MOVE CORRESPONDING clause. For more detail, see the General Rules of the MOVE CORRESPONDING clause.
  10. For details on how data items are identified as CORRESPONDING, see the entry Rules for identifying CORRESPONDING data in the Common General Rules section.
  11. In a SUBTRACT CORRESPONDING operation, all SUBTRACT operations will be completed before the ON SIZE ERROR condition 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.
Back to top