The Wayback Machine - https://web.archive.org/web/20240727032716/https://www.geeksforgeeks.org/sap-abap-basic-syntax-statements/
Open In App

SAP ABAP | Basic Syntax & Statements

Last Updated : 29 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

The German software company SAP created the high-level programming language, ABAP (Advanced Business Application Programming) primarily, this language serves as a tool for developing applications within the SAP R/3 system. Designed with simplicity and ease of learning in mind, ABAP syntax allows efficient processing of large volumes of data. Similar to COBOL, it offers a concise set of statements; these aid in the efficient management of large datasets. Typically, one writes ABAP code in the ABAP Editor, a component of SAP GUI (Graphical User Interface).

Basic Syntax of SAP ABAP:

REPORT [ Program_Name ].

[Statements.....]

Example:

REPORT Z_HELLO_WORLD.
WRITE 'Hello World'.

This simple example presents a SAP ABAP program. its function is to print ‘Hello World‘ on the screen. The first line in this example serves as a REPORT statement, precisely specifying the program’s name; meanwhile, the second ABAP statement, which writes ‘Hello World’ on-screen forms its subsequent line.

syntax

SAP ABAP | Basic Syntax & Statements

SAP ABAP Satatements:

SAP ABAP provides a set of statements and keywords for performing various tasks within SAP applications. Each statements in SAP ABAP begins with a keyword and ends with a period(.).

How to format SAP ABAP Statements:

In SAP ABAP, your code’s readability and maintainability enhance significantly through the critical utilization of indentation and colon notation. Visually organizing your code’s structure becomes easier with proper indentation; moreover, simplifying output formatting is achievable via the use of colon notation. Follow these guidelines to effectively employ indentation and colon notation in ABAP:

SAP ABAP Indentation:

SAP ABAP statement has no nay restriction in writing the statements , the statements can be written in any format, we can write the whole statements in a single line or we can write each statement in each line.

PROGRAM GFG_PROBLEM
WRITE ' Hello World! '

The above code could also be written as:

PROGRAM GFG_PROBLEM  WRITE  ' Hello World! '

Or could also be written as:

PROGRAM
GFG_PROBLEM
WRITE ' Hello World! '

SAP ABAP colon notation:

In SAP ABAP, the colon notation is often used to define data objects within a program. The colon notation helps make code more concise and easier to read, as we don’t need to explicitly define the data type for each variable.

Here’s how the colon notation is used to define variables and constants:

1. Variable Declaration:You can use the colon notation to declare variables. Variables declared with a colon are local to the current program or subroutine.

DATA: lv_variable TYPE i,               * Integer variable
lv_text TYPE string, * String variable
lv_date TYPE d. * Date variable

2.Constant Declaration:

Constants can also be declared using the colon notation. Constants are values that do not change during the execution of a program.

CONSTANTS: c_max_value TYPE i VALUE 100,
c_app_name TYPE string VALUE 'My Application'.

Creating Your First SAP ABAP Program:

To become proficient in ABAP programming, you should start by creating a basic ABAP report. These reports usually perform straightforward tasks or show simpler data. Here’s a step-by-step guide to help you to begin your first SAP ABAP report:

Step 1: First, we must define the ABAP Report.

Define the name and attributes of your ABAP report by utilizing the REPORT statement.

REPORT Z_FIRST_ABAP_REPORT.

Step 2: You must declare data variables and then write the essential logic to execute operations.

DATA: lv_name TYPE string.
lv_name = 'John Doe'.
WRITE: 'Hello', lv_name.
Output:
Hello John Doe

Step 3: Using the appropriate transaction code in the SAP GUI. specifically SE38 or SA38 and execute the report.

The execution of the report will display ‘Hello John Doe’ on-screen.

Starting your first ABAP report is an important step when you begin ABAP programming. It helps you to be familiar of ABAP program rules and structure, making it easier to do basic data tasks.

Adding Comments in SAP ABAP:

To make ABAP programming easier to understand and improve how your code looks, there are two important things to do: adding comments and getting rid of extra spaces. Comments are like notes that help explain your code, and removing spaces helps control how the output appears. Let’s see how to use comments properly in ABAP programming.

Adding Comments in SAP ABAP:

  • Full Line comments: In SAP ABAP, you can add full-line comments to document and explain your code. These comments are ignored by the compiler and are only meant for human readers to understand the code better. Full-line comments start with an asterisk (*) in the first position of a line. Here’s an example of how to add full-line comments in SAP ABAP:

Example:

* This is a full-line comment.
* You can write detailed explanations about your code here.
  • Partial Line comments: In SAP ABAP, you can add partial-line comments to add comments in your code. Partial-line comments are typically added at the end of a line of code and are preceded by a double quotation mark ("). Here’s how to add partial-line comments in SAP ABAP:

Example:

DATA: lv_variable TYPE i, " This is a partial-line comment
lv_text TYPE string.

Suppressing Blanks in SAP ABAP

In SAP ABAP, you can control the display of extra spaces and blank lines to make the output appearance more predictable and neat. This is often referred to as “suppressing blanks.” Here’s how you can suppress blanks in ABAP:

  • Leading Blanks: To remove leading spaces at the beginning of a line when using the WRITE statement, you can use the NO-GAP addition:
WRITE: NO-GAP 'This will not have leading spaces.'.
  • Trailing Blanks: To remove trailing spaces at the end of a line when using the WRITE statement, you can use the NO-GAP addition as well:
WRITE: 'This will not have trailing spaces.' NO-GAP.
  • Suppressing Blank Lines: If you want to prevent blank lines from being inserted in the output, you can use the NO-TITLE addition with the WRITE statement:
WRITE: / 'Line 1', NO-TITLE, 'Line 2'.

This will ensure that there is no blank line inserted between “Line 1” and “Line 2.”

Blank Lines in SAP ABAP:

In SAP ABAP, “blank lines” refer to empty lines or spaces that can be inserted into the output of a program or report to improve its readability and structure. The SKIP command is used to insert Blank Lines in SAP ABAP.

Example:

WRITE: 'First line of text',
SKIP , " This inserts two blank lines
'Second line of text'.

The SKIP command allows us to control the number of blank lines inserted for better formatting.

INSERT Lines in SAP ABAP:

THe ULINE command is used to insert a horizontal lines in the output. The syntax is as below of the code:

WRITE : 'GFG is the Best'
ULINE.

Output:

GFG is the Best
(and a horizontal line is drawn. below this.)

Handling Messages in SAP ABAP Code:

Handling messages in ABAP (Advanced Business Application Programming) code is crucial for providing feedback to users, handling errors, and managing the flow of an SAP application. Messages can be informational, warning, or error messages, and they help communicate with users and make your ABAP programs more user-friendly. following are the characters for use with the message command.

Message

Type

E

Error

W

Warning

I

Information

A

Abend

S

Success

X

Abort

When users effectively handle messages in ABAP, they inform about the program execution status and any potential issues that might emerge during the process.

Conclusion:

In this article, we have learnt about Basic Syntax & Statements of SAP ABAP. SAP ABAP (Advanced Business Application Programming) is not case-sensitive when it comes to keywords, identifiers, and data declarations. This means that the language does not distinguish between uppercase and lowercase letters in these parts of the code. For example, “IF,” “If,” and “if” are all considered the same in ABAP.



Previous Article
Next Article

Similar Reads

SAP ABAP: A complete Guide to Events in ABAP Report
SAP created the high-level programming language known as SAP ABAP (Advanced Business Application Programming) to be used for creating business applications. Reports are an essential tool for extracting and presenting data in SAP development. To manage the execution flow and improve the functionality of their systems, developers must comprehend the
5 min read
SAP S/4 HANA Vs SAP HANA | Difference between SAP S/4 HANA , SAP HANA
SAP S/4HANA is the most recent ERP suite, with advanced architecture allowing for real-time processing. SAP HANA is an in-memory database that supports S/4HANA, whereas SAP ECC is S/4HANA's traditional ERP predecessor. The distinctions are in the streamlined architecture and enhanced capabilities of S/4HANA. What is SAP S/4 HANA?SAP, which stands f
3 min read
SAP HANA Vs SAP ECC | Difference between SAP HANA and SAP ECC
SAP HANA is an in-memory database that supports S/4HANA, whereas SAP ECC is S/4HANA's traditional ERP predecessor. The distinctions are in the streamlined architecture and enhanced capabilities of S/4HANA. What is SAP HANA?SAP HANA stands for High-performance analytic appliance, which is faster than any other database management system that we use
2 min read
SAP MM Vs SAP SRM | Difference Between SAP MM and SAP SRM
SAP MM helps companies optimize their material and logistics management procedures, guaranteeing effective and economical handling of items. SAP SRM stands for Supplier Relationship Management and is an efficient tool that helps companies optimize procurement operations and helps to improve relationships with suppliers and other business vendors. W
2 min read
SAP EWM Vs SAP WM | Difference Between SAP EWM and SAP WM
SAP EWM is a version created by enhancing the SAP Warehouse Management (SAP WM) solution, one of the world’s most popular warehouse management applications, with new features. What is SAP EWM?The SAP EWM solution was created to organize and manage goods storage in warehouses, distribution centers, and logistical centers. Its primary goal is to impr
3 min read
SAP ABAP | Decision Control Statements
SAP ABAP, or Advanced Business Application Programming, provides a robust set of tools for developing business applications within the SAP environment. Decision control statements are a crucial aspect of programming logic, allowing developers to make choices and guide the flow of a program based on certain conditions. Decision Control statement hav
3 min read
Diffrence Between SAP and SAP ABAP(Advanced Business Application Programming)
In this article, we are going to learn the difference between an SAP and SAP ABAP. SAP ABAP is created by SAP which is a domain-specific language, it is 4th generation programming language. It is also called ABAP/4(Fourth Generation Language” or 4GL). What is SAP?SAP stands for Systems, Applications, and Products in Data Processing. It’s a German m
2 min read
Difference Between SAP ERP and SAP ABAP
In this article, we are going to learn the difference between an SAP ERP and SAP ABAP. SAP ABAP is created by SAP which is a domain-specific language, it is 4th generation programming language. It is also called ABAP/4(Fourth Generation Language” or 4GL). SAP Enterprise Resource Planning (ERP) systems are essential for simplifying and maximizing a
2 min read
Difference between SAP S/4 HANA , SAP HANA and SAP ECC
SAP S/4HANA is the most recent ERP suite, with advanced architecture allowing for real-time processing. SAP HANA is an in-memory database that supports S/4HANA, whereas SAP ECC is S/4HANA's traditional ERP predecessor. The distinctions are in the streamlined architecture and enhanced capabilities of S/4HANA. What is SAP S/4 HANA?SAP, which stands f
3 min read
SAP ABAP | Loop Control
Introduction to Loop Control in SAP ABAPIn SAP ABAP programming, loop control is an esse­ntial concept that allows you to execute­ a block of code multiple times. This is e­specially useful when proce­ssing data in an efficient manner. Loops help automate repetitive­ tasks and handle large datasets effectively. In ABAP, there are various types of l
4 min read
three90RightbarBannerImg