Creating Reports in ABAP: A Step-by-Step Guide

August 29, 2024

Creating Reports in ABAP: A Step-by-Step Guide

In this post, we will walk you through the process of creating a simple report in ABAP. You will learn how to format output, use selection screens, and present data to end-users.

Understanding Reports in ABAP

Reports are one of the most common types of programs in ABAP. They allow you to retrieve data from the database and present it in a user-friendly format. Reports can be either classical or interactive. In this guide, we will focus on classical reports.

Step 1: Creating a New ABAP Program

To create a report, start by opening the ABAP Workbench (Transaction SE80) and creating a new program. Name your program (e.g., Z_SIMPLE_REPORT) and set its type to ‘Executable Program’.

Step 2: Defining the Selection Screen

Selection screens allow users to enter parameters to filter the data displayed in the report. To define a selection screen, use the following code:

REPORT Z_SIMPLE_REPORT.

PARAMETERS: p_matnr TYPE MATNR.

In this example, we have created a parameter field for material number (MATNR). Users will be able to input a material number when running the report.

Step 3: Retrieving Data from the Database

Next, we will retrieve data from the database using the SELECT statement. The data will be fetched based on the user input from the selection screen:

DATA: lt_materials TYPE TABLE OF mara,
      ls_material  TYPE mara.

SELECT * FROM mara INTO TABLE lt_materials WHERE matnr = p_matnr.

In this code, we are selecting all fields from the MARA table where the material number matches the user input.

Step 4: Formatting the Output

Now, we will format the output of our report using the WRITE statement. This is how we can display the retrieved data:

LOOP AT lt_materials INTO ls_material.
  WRITE: / ls_material-matnr, ls_material-maktx.
ENDLOOP.

In this example, we are looping through the internal table lt_materials and writing the material number and its description (MAKTX) to the output.

Step 5: Enhancing the Report

You can enhance your report further by adding additional features such as sorting, totals, or even exporting the data to Excel. For instance, to sort the materials by description, you could add:

SORT lt_materials BY maktx.

Step 6: Testing the Report

Once you have completed the coding, it’s time to test your report. Activate the program and execute it using Transaction SE38. Enter a material number on the selection screen and check if the expected results are displayed correctly.

Conclusion

In this guide, we have covered the essential steps to create a simple report in ABAP. You learned how to define a selection screen, retrieve data from the database, format the output, and enhance your report. With these skills, you can start building more complex reports tailored to your business needs.

Keep practicing, and soon you’ll be able to create advanced reports that provide valuable insights to your users!