Entering content frame

Calculating Numbers and Totals Locate the document in its SAP Library structure

When you read a sorted extract dataset using LOOP, you can access two automatically-generated fields cnt(f) and sum(g). These fields contain the number of different values and the sums of the numeric fields respectively. The system fills these fields at the end of a control level and after reading the last record of the dataset as follows:

·        cnt(f)

If f is a non-numeric field of the header field group and the system sorted the extract dataset by f, cnt(f) contains the number of different values f assumed within the control level or entire dataset respectively.

·        sum(g)

If g is a numeric field of the extract dataset, sum(g)contains the total of the values of g  within the control level or entire dataset, respectively.

You can access these fields either within the processing blocks following AT END OF or in the processing block following AT LAST, after reading the entire dataset. If you try to access the fields cnt(f) and sum(g) without first sorting the dataset, a runtime error may occur.

Example

REPORT demo_extract_cnt_sum.

DATA: t1(4) TYPE c, t2 TYPE i.

FIELD-GROUPS: header, test.

INSERT t2 t1 INTO header.

t1 ='AABB'. t2 = 1. EXTRACT test.
t1 ='BBCC'. t2 = 2. EXTRACT test.
t1 ='AAAA'. t2 = 2. EXTRACT test.
t1 ='AABB'. t2 = 1. EXTRACT test.
t1 ='BBBB'. t2 = 2. EXTRACT test.
t1 ='BBCC'. t2 = 2. EXTRACT test.
t1 ='AAAA'. t2 = 1. EXTRACT test.
t1 ='BBBB'. t2 = 1. EXTRACT test.
t1 ='AAAA'. t2 = 3. EXTRACT test.
t1 ='AABB'. t2 = 1. EXTRACT test.

SORT BY t1 t2.

LOOP.

  WRITE: /20 t1, t2.

  AT END OF t2.
    ULINE.
    WRITE: 'Sum:', 20 sum(t2).
    ULINE.
  ENDAT.

  AT END OF t1.
    WRITE: 'Different values:', (6) cnt(t1).
    ULINE.
  ENDAT.

  AT LAST.
    ULINE.
    WRITE: 'Sum:', 20 sum(t2),
           / 'Different values:', (6) cnt(t1).

  ENDAT.

ENDLOOP.

This program creates a sample extract, containing the fields of the header field group only. After sorting, the system outputs the contents of the dataset, the number of the different t1 fields, and the totals of the t2 fields at the end of each control level and at the end of the loop:

This graphic is explained in the accompanying text

 

 

 

Leaving content frame