Skip to content

Count Clause

The COUNT clause counts the number of appearances of any specified report field or group item.

count clause

Count Clause: Coding Rules

  • Each data-name must be the name of any REPORT SECTION entry other than an RD. It may even be the data-name of a group entry that contains the COUNT clause.

  • You may place the COUNT clause in a different report group from the item counted (rolling forward) or in the same group (cross-footing). As with the SUM clause, if the COUNT clause appears in a multiple CONTROL FOOTING, all but the lowest level total is formed by rolling forward the next-lower total.

  • Unlike the situation with the SUM clause, the item counted need not be a numeric field.

  • Like the SUM clause, you may use the COUNT clause in two ways:

    • As a clause in its own right. You write the clause in place of a Source, Value, or Function clause. For example:

      05 COL 21 PIC ZZZ9 COUNT OF R-CUSTOMER-NAME.

    • As a term in an expression used as a Source operand. For example:

      05 COL 21 PIC ZZ9 SOURCE IS (COUNT OF R-GAIN) - (COUNT OF R-LOSS).

  • You may combine SUM and COUNT terms in the same expression. For example, the following will give you the average value of all the instances of a numeric field:

      05 COL 32 PIC ZZZZ9 
         SOURCE (SUM OF PURCHASE) / (COUNT OF PURCHASE) ROUNDED.
    

Count Clause: Operation

  • This clause gives a count of the number of times the item referenced has appeared. In other words, whenever the item appears in the Report, 1 is added to the count. You cannot count items that are outside the REPORT SECTION, such as WORKING-STORAGE items.

  • The item referenced may be at any level; for example:

    • A 01-level entry: you will obtain the number of appearances of a particular group.
    • A LINE entry: you will count the number of appearances of that particular LINE.
    • A COLUMN entry: you will obtain a count of appearances of that particular elementary item.
  • Assuming that you do not use the RESET phrase, when your COUNT field has been output the count returns to zero. Hence, you will always obtain the count of the number of appearances of the item since the last time the count was output.

  • You may use the RESET ON phrase to delay the resetting of the count to zero until a higher-level control break occurs, in exactly the same way as you can with the SUM clause.

  • If the item counted is one that repeats several times because of an OCCURS or REPEATED clause, the count will include each repetition. An OCCURS ... DEPENDING will count just the number of items actually output. A PRESENT clause (or the equivalent) is also taken into account, so that items "not present" do not contribute to the count.

  • You may count more than one item by writing more than one data-name as an operand in the clause. The counts are then simply consolidated internally. (The COUNT clause is really a special variant of the SUM clause, so other points of interest can be found under SUM clause.)

Compatibility

Only new Report Writer provides the COUNT clause.

Back to top