6 Columns with Active Background
6 Columns with Active Background
Active List Item Background

24DDIC0408 – Control Break Statements in SAP ABAP Unlocking the Power of Internal Table Events

What Are Control Break Statements?

Control break statements are special constructs that allow developers to execute code at specific points when processing internal tables. These points, known as control breaks, occur when there is a change in the value of a specified field within a sorted internal table. Control break statements make it easier to handle grouped data, calculate totals, and generate structured reports.

Key Control Break Statements in ABAP

  1. AT FIRST
  2. AT LAST
  3. AT NEW <field>
  4. AT END OF <field>

 

AT FIRST

The AT FIRST statement is used to perform operations at the very beginning of the loop, before any rows are processed. This is useful for initializing variables or setting up the environment for data processing.

Example:

LOOP AT itab INTO wa.

  AT FIRST.

    WRITE: / ‘Start of processing’.

  ENDAT.

  ” Process the row

  WRITE: / wa-field1, wa-field2.

ENDLOOP.

In this example, the message “Start of processing” is printed once, before any rows of the internal table itab are processed.

 

AT LAST

The AT LAST statement is used to perform operations at the very end of the loop, after all rows have been processed. This is often used to finalize totals or clean up resources.

Example:

LOOP AT itab INTO wa.

 “Process the row

  WRITE: / wa-field1, wa-field2.

  AT LAST.

    WRITE: / ‘End of processing’.

  ENDAT.

ENDLOOP.

Here, the message “End of processing” is printed once, after all rows of the internal table have been processed.

 

AT NEW <field>

The AT NEW <field> statement triggers whenever there is a change in the value of the specified field within a sorted internal table. This is particularly useful for handling grouped data, where you want to perform an action at the start of each new group.

Example:

LOOP AT itab INTO wa.

  AT NEW group_field.

 WRITE: / ‘New group:’, wa-group_field.

  ENDAT.

  ” Process the row

  WRITE: / wa-group_field, wa-detail_field.

ENDLOOP.

In this example, the message “New group” followed by the group field value is printed each time the value of group_field changes.

 

AT END OF <field>

The AT END OF <field> statement is used to perform operations when the value of the specified field is about to change, or at the end of the group. This is commonly used for summarizing or finalizing data within each group.

Example:

DATA: lv_total TYPE i.

LOOP AT itab INTO wa.

  AT NEW group_field.

    lv_total = 0.

  ENDAT.

  ” Process the row

  WRITE: / wa-group_field, wa-detail_field.

  lv_total = lv_total + wa-amount.

  AT END OF group_field.

    WRITE: / ‘Total for group:’, lv_total.

  ENDAT.

ENDLOOP.

In this scenario, the total for each group is calculated and printed at the end of each group.

 

Practical Applications

 

Control break statements are invaluable in many practical scenarios:

 

Generating Reports: They help in creating structured reports where data is grouped and summarized, such as sales reports by region or product.

Data Aggregation: Useful for aggregating data, like summing up sales figures or counting occurrences within groups.

Data Validation: Ensuring data consistency and integrity by performing checks at the start and end of data processing.

Author : Aniket Pawar, 9373518385                                                     

24DDIC0408 – Control Break Statements in SAP ABAP Unlocking the Power of Internal Table Events

What Are Control Break Statements?

Control break statements are special constructs that allow developers to execute code at specific points when processing internal tables. These points, known as control breaks, occur when there is a change in the value of a specified field within a sorted internal table. Control break statements make it easier to handle grouped data, calculate totals, and generate structured reports.

Key Control Break Statements in ABAP

  1. AT FIRST
  2. AT LAST
  3. AT NEW <field>
  4. AT END OF <field>

 

AT FIRST

The AT FIRST statement is used to perform operations at the very beginning of the loop, before any rows are processed. This is useful for initializing variables or setting up the environment for data processing.

Example:

LOOP AT itab INTO wa.

  AT FIRST.

    WRITE: / ‘Start of processing’.

  ENDAT.

  ” Process the row

  WRITE: / wa-field1, wa-field2.

ENDLOOP.

In this example, the message “Start of processing” is printed once, before any rows of the internal table itab are processed.

 

AT LAST

The AT LAST statement is used to perform operations at the very end of the loop, after all rows have been processed. This is often used to finalize totals or clean up resources.

Example:

LOOP AT itab INTO wa.

 “Process the row

  WRITE: / wa-field1, wa-field2.

  AT LAST.

    WRITE: / ‘End of processing’.

  ENDAT.

ENDLOOP.

Here, the message “End of processing” is printed once, after all rows of the internal table have been processed.

 

AT NEW <field>

The AT NEW <field> statement triggers whenever there is a change in the value of the specified field within a sorted internal table. This is particularly useful for handling grouped data, where you want to perform an action at the start of each new group.

Example:

LOOP AT itab INTO wa.

  AT NEW group_field.

 WRITE: / ‘New group:’, wa-group_field.

  ENDAT.

  ” Process the row

  WRITE: / wa-group_field, wa-detail_field.

ENDLOOP.

In this example, the message “New group” followed by the group field value is printed each time the value of group_field changes.

 

AT END OF <field>

The AT END OF <field> statement is used to perform operations when the value of the specified field is about to change, or at the end of the group. This is commonly used for summarizing or finalizing data within each group.

Example:

DATA: lv_total TYPE i.

LOOP AT itab INTO wa.

  AT NEW group_field.

    lv_total = 0.

  ENDAT.

  ” Process the row

  WRITE: / wa-group_field, wa-detail_field.

  lv_total = lv_total + wa-amount.

  AT END OF group_field.

    WRITE: / ‘Total for group:’, lv_total.

  ENDAT.

ENDLOOP.

In this scenario, the total for each group is calculated and printed at the end of each group.

 

Practical Applications

 

Control break statements are invaluable in many practical scenarios:

 

Generating Reports: They help in creating structured reports where data is grouped and summarized, such as sales reports by region or product.

Data Aggregation: Useful for aggregating data, like summing up sales figures or counting occurrences within groups.

Data Validation: Ensuring data consistency and integrity by performing checks at the start and end of data processing.

Author : Aniket Pawar, 9373518385