Skip to main content

SAP ABAP Control Break Statements – Complete Guide with AT FIRST, AT NEW & AT END OF Examples

📘 SAP ABAP Control Break Statements – Complete Guide with Example

Learn about Control Break Statements in SAP ABAP including AT FIRST, AT NEW, AT END OF, AT LAST, and ON CHANGE OF. Includes a real-time example program with explanation and output. Perfect for SAP ABAP beginners and developers.

  1. SAP ABAP Control Break Statements
  2. AT NEW ABAP example
  3. AT END OF ABAP tutorial
  4. ABAP loop control
  5. SAP ABAP report programming
  6. AT FIRST, AT LAST in ABAP
  7. SAP ABAP internal table sorted


📘 Introduction

In SAP ABAP, control break statements are used to trigger events inside a loop. They are essential for creating grouped or hierarchical reports, especially when working with sorted internal tables.

There are 5 main control break statements in ABAP:

  1. AT FIRST – ENDAT
  2. AT NEW – ENDAT
  3. AT END OF – ENDAT
  4. AT LAST – ENDAT
  5. ON CHANGE OF FIELD.

SAP ABAP Loop Control Tutorial: 

How to Use AT NEW, AT END OF, AT FIRST, AT LAST, & ON CHANGE OF  FIELD.

🧠 Key Control Break Statements Explained

Statement

Triggered When

AT FIRSTTriggered at the first record of the loop
AT LASTTriggered at the last record of the loop
AT NEWTriggered at the first occurrence of a new value in a field
AT END OFTriggered at the last occurrence of a value in a field
ON CHANGE OFTriggered whenever the specified field value changes; can also be used outside loops

📌 Detailed Explanation

1. AT FIRST – ENDAT

  1. Executes only once at the first record of a loop.
  2. Useful for printing headers or initializing counters.

2. AT LAST – ENDAT

  1. Executes only once at the last record of a loop.
  2. Often used to print footers or totals.

3. AT NEW <field> – ENDAT

  1. Executes when a new value of a field is encountered.
  2. Example: Display Material Type heading when the value changes in sorted table.

4. AT END OF <field> – ENDAT

  1. Executes at the last occurrence of a field value.
  2. Useful for calculating totals or counts for grouped data.

5. ON CHANGE OF <field>

  1. Executes whenever a field value changes.
  2. Can be used inside or outside loops.
  3. Often used in reporting to detect changes in key fields.


📌 Notes

  1. Works only with sorted internal tables.
  2. Very useful for grouped reports by key fields.
  3. Supports nested levels, e.g., an AT NEW inside another AT NEW.


🟦 Example Program – SAP ABAP Control Break Statements

Requirement

Control Break Statements in SAP ABAP – Step-by-Step Example Program with Output

Display Material Master Data grouped by Material Type (MTART) and count total materials for each type.



📌 Notes :

To learn more on basic information types decleration, constant,
parameters click on the below.

REPORT zmm_type_str_2. TYPES: BEGIN OF ty_mara, mtart TYPE mtart, matnr TYPE matnr, ersda TYPE ersda, END OF ty_mara. DATA: lt_mara TYPE SORTED TABLE OF ty_mara WITH UNIQUE KEY matnr, wa_mara TYPE ty_mara. " Step 1: Select Material Data SELECT mtart, matnr, ersda INTO TABLE @lt_mara FROM mara UP TO 50 ROWS WHERE mtart IN ('FERT', 'HALB', 'ROH'). " Step 2: Display grouped data DATA: lv_count TYPE i VALUE 0. LOOP AT lt_mara INTO wa_mara. AT FIRST. WRITE: / 'Material List Grouped by MTART'. ULINE. ENDAT. AT NEW mtart. WRITE: / 'Material Type:', wa_mara-mtart. lv_count = 0. ENDAT. WRITE: / ' ->', wa_mara-matnr, wa_mara-ersda. lv_count = lv_count + 1. AT END OF mtart. WRITE: / ' Total Materials:', lv_count. ULINE. ENDAT. AT LAST. WRITE: / '*** End of Report ***'. ENDAT. ENDLOOP.

🎯 Summary

  • Control Break Statements help in grouping, totaling, and formatting reports.

  • Use AT FIRST and AT LAST for headers/footers.

  • Use AT NEW and AT END OF to group by key fields.

  • ON CHANGE OF is flexible and can be used inside or outside loops.

  • Essential for real-time SAP ABAP reporting scenarios..


Comments

Popular posts from this blog

SAP ABAP SE11 Data Dictionary Tutorial | Step-by-Step Explanation with Real-Time Examples

    SAP ABAP Data Dictionary (SE11) – Complete Guide to Tables, Views, Domains & Data Elements    What is the ABAP Data Dictionary? The SAP ABAP data dictionary serves as a primary and structured source of data for creating objects. It is an independent database DDL (Data Definition Language) that primarily deals with creating, editing, and dropping database tables. The data dictionary allows you to define and maintain database-related items. The ABAP data dictionary is readily connected with the ABAP workbench, allowing all of the workbench's components to simply access the definitions contained in the dictionary. Functions of Data Dictionary The important functions of data dictionary objects are as follows. 1. Database tables 2. Domains 3. Data elements 4. Views 5. Search helps 6. Lock Objects 1. Database Tables: –  Database tables are collections of fields that store physical data. It is an object that holds data in the form of rows and columns. So databa...

SAP ABAP ALV Report Using REUSE_ALV_GRID_DISPLAY – Complete Tutorial with Examples

  📘 SAP ABAP ALV Report Using REUSE_ALV_GRID_DISPLAY – Complete Step-by-Step Guide (With Real Examples) SAP ABAP developers frequently use ALV (ABAP List Viewer) to present data in a clean and interactive format. One of the most commonly used ALV function modules in classical reporting is: This function module allows developers to create professional ALV reports with features such as sorting, filtering, totals, layout saving, and exporting to Excel. In this guide, I will explain: What REUSE_ALV_GRID_DISPLAY is How field catalogs work How to build an ALV report using MARC and KNA1 tables Real-time business examples Clean, fully working ABAP code This post is written from my personal learning and hands-on experience while working on SAP ABAP reports. 🧩 What is REUSE_ALV_GRID_DISPLAY? REUSE_ALV_GRID_DISPLAY is a function module-based ALV used in SAP ECC and compatible with S/4HANA systems. It displays internal table data in a grid-style output , which is more user...