Open In App

SAP ABAP | Basic Syntax & Statements

Last Updated : 29 Nov, 2023
Improve
Improve
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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads