AT - Control Level Processing

AT - Control breaks with internal tables

Variants:

1. AT NEW f.
2. AT END OF f.
3. AT FIRST.
4. AT LAST.

Effect

In a LOOP which processes an internal table, you can use special control structures for control break processing. All these structures begin with AT and end with ENDAT . The sequence of statements which lies between them is then executed if a control break occurs.

You can use these key words for control break processing with internal tables only if a loop is actively processing an internal table and reference is to the innermost currently active loop.

The control level structure with internal tables is static. It corresponds exactly to the sequence of columns in the internal table (from left to right). In this context, the criteria according to which you sort the internal table are unimportant.

At the start of a new control level (i.e. immediately after AT), the following occurs in the output area of the current LOOP statement:

Between AT and ENDAT, you can use SUM to insert the appropriate control totals in the numeric fields (see also ABAP Number Types) of the LOOP output area (on the right) after the current control level key. Summing is supported both at the beginning of a control level ( AT FIRST, AT NEW f) and also the end of a control level (AT END OF f, AT LAST).

At the end of the control level processing (i.e. after ENDAT), the old contents of the LOOP output area are restored.

When calculating totals, you must ensure that the totals are inserted into the same sub-fields of the LOOP output area as those where the single values otherwise occur. If there is an overflow, processing terminates with a runtime error.

Effect

f is a sub-field of an internal table processed with LOOP. The sequence of statements which follow it is executed if the sub-field f or a sub-field in the current LOOP line defined (on the left) before f has a different value than in the preceding (AT NEW) or subsequent (AT END OF) table line.

Example

DATA: BEGIN OF COMPANIES OCCURS 20, 
		NAME(30), 
		PRODUCT(20), 
		SALES TYPE I, 
	END   OF COMPANIES. 
... 
LOOP AT COMPANIES. 
  AT NEW NAME. 
	NEW-PAGE. 
	WRITE / COMPANIES-NAME. 
  ENDAT. 
  WRITE: / COMPANIES-PRODUCT, COMPANIES-SALES. 
  AT END OF NAME. 
	SUM. 
	WRITE: / COMPANIES-NAME, COMPANIES-SALES. 
  ENDAT. 
ENDLOOP.

The AT statements refer to the field COMPANIES-NAME.

If a control break criterion is not known until runtime, you can use AT NEW (name) or AT END OF (name) to specify it dynamically as the contents of the field name. If name is blank at runtime, the control break criterion is ignored and the sequence of statements is not executed. If name contains an invalid component name, a runtime error occurs.

Effect

Executes the appropriate sequence of statements once during the first (AT FIRST) or last (AT LAST) loop pass.

Example

DATA: BEGIN OF COMPANIES OCCURS 20, 
		NAME(30), 
		PRODUCT(20), 
		SALES TYPE I, 
	END   OF COMPANIES. 
... 
LOOP AT COMPANIES. 
  AT FIRST. 
	SUM. 
	WRITE:	'Sum of all SALES:', 
		 55 COMPANIES-SALES. 
  ENDAT. 
  WRITE: / COMPANIES-NAME, COMPANIES-PRODUCT, 
		55 COMPANIES-SALES. 
ENDLOOP.