Basic Operation
Basic Operation
Chained Statement:
If you want to make a part of the line as comment, then enter double quote (“) before
the comment.
SAP Transaction code is a short cut key attached to a screen. Instead of using SAP easy access menu
we can also navigate to a particular screen in SAP by entering the transaction code (T-code for
short) in the command field of the standard toolbar.
T coDe description
This is the ABAP editor’s initial screen. Enter the name of the program you want to create and
press create. All the customer programs must begin with “Y” or “Z”.
In the next popup screen(Program attributes) enter the title for your program, select Executable
program as type and press save.
Press Local Object to store the program in the temporary folder.
This is the screen where you can write the ABAP code.
Successful syntax check message will be displayed in the status bar. Then activate( Ctrl + F3 )
the program.
In the following screen select your program and press continue. Then run(F8) the program.
A variable is a data object whose value can be changed at runtime. To declare a variable,
you use the data keyword with the following syntax:
Following the data keyword is the variable name. The variable name must follow the
these rules:
After the variable name is the type keyword followed by the data type of the variable.
Static varibles
Static variables have fixed-length types and reserve a data range in memory. For
example:
In this example, gv_name is a static variable. It has a data type c whose length is 50.
Note that the variable name starts with gv_ and its description. This is a common
practice to make the code more readable. By convention, g means global, v stands for
the variable. So gv_name indicates that it is a global variable. To declare local variables,
you use lv_ prefix e.g., lv_name.
You can specify an initial value for a static variable by using the optional value keyword
like this:
In this example, the gv_name takes the literal characters 'John Doe' as the initial value. If you
do not specify the initial value for gv_name, its defaults value is blank.
ABAP Data Types
Summary: in this tutorial, you will learn about the ABAP data types including
elementary types, complex types, and reference types.
ABAP has three main data types: elementary types. complex types, and reference types.
Elementary types
The elementary type is further divided into the fixed-length type and variable-length
type.
The fixed-length types in ABAP are C, D, N, T, F, I, P, and X. These types consist of:
Numeric types: I, F, P.
Character types: C, D, N, T.
Hexadecimal types: X
When you declare a data object that has a fixed-length type, you must specify its length
in the variable declaration. For example:
In this example, the length of the gv_name variable is 50. You cannot store more than 50
characters in the gv_name variable.
The variable-length types are string and xstring. When you declare a variable whose type is
a variable-length type, you don’t need to declare its length. Its length varies during the
execution of the program as you assign values to it. For example:
In this example, the gv_job_title has the type of string. Its length will change once you
assign data to it.
In this case, the length of gv_job_title is 13, which is the length of the string 'SAP Developer'.
F 8 bytes 8 bytes 0
Structured data type is grouping of several simple data types under one name.
Constants
Constants are used to store a value under a name. We must specify the value when we declare
a constant and the value cannot be changed later in the program.
ABAP system variables is accessible from all ABAP programs. These fields are filled by the runtime
environment. The values in these fields indicate the state of the system at any given point of
time.
The complete list of ABAP system variables is found in the SYST table in SAP. Individual fields of
the SYST structure can be accessed either using “SYST-“ or “SY-“.
Output
DATA: a TYPE i,
b TYPE i,
c TYPE i,
d TYPE i.
a = 10.
b = a.
MOVE 20 TO c.
MOVE c TO d.
WRITE:/ a, b, c, d.
Output
DATA: a TYPE i,
b TYPE i,
c TYPE i,
d TYPE i.
*Using Keywords
add 10 to a.
subtract 5 from b.
multiply c by 2.
divide d by 2.
Output
ABAP constants
Summary: in this tutorial, you will learn how to use the ABAP constants keyword to declare
constants in the program.
Constants are data objects whose values cannot be changed by the program at runtime
using assignments. Their contents are constants.
In this syntax:
First, specify the constant name after the constants keyword. The name of a constant has
at most 30 characters and starts with a letter.
Second, select a data type for the constant after the type keyword.
Third, specify the value of the constant after the value keyword. This is only place where
you can specify the constant value.
The following shows how to use the constant gc_tax_rate to calculate the net price:
constants:
constant_name_1 type data_type value constant_value_1,
constant_name_2 type data_type value constant_value_2,
...
constant_name_n type data_type value constant_value_n.
Summary
Use the keyword constants to declare data object whose values does not change in the
program.
Control flow
ABAP if else
Summary: in this tutorial, you will learn how to use the ABAP if else statement to execute
a block of code based on the result of one or more logical expressions.
The if statement
To execute a block of code based on a logical expression, you use the if statement as
follows:
if logical_expression.
" code block
endif.
In this syntax, the code block inside the if statement will execute if
the logical_expression evaluates to true.
For example:
if gv_status = 1.
write: 'The status is 1'.
endif.
Output:
The status is 1
If you change the value of gv_status to 2, the expression gv_status = 1 evaluates to a false, the
code does not output anything to the screen.
if gv_status = 1.
write: 'The status is 1'.
endif.
if logical_expression.
" if block
else.
" else block
endif.
For example:
if gv_status = 1.
write: 'The status is 1'.
else.
write: 'The status is not equal to 1'.
endif.
Output:
The if elseif else statement allows you to execute a code block based on the results of
multiple boolean expressions:
if logical_expression1.
" if block
elseif logical_expression2.
" elseif block
elseif logical_expression3.
"...
else.
" else block
endif.
In this syntax, ABAP evaluates the logical expressions from top to bottom. If it finds a
logical expression that evaluates to true, it will execute the code block in that branch.
If there is no logical expression that evaluates to true, the code block in the else branch
will execute.
write gv_price.
Output:
15
How it works.
Summary
Use the if statement when you want to execute a code block based on a logical
expression.
Use the if else statement in case you want to execute another code block when the logical
expression in the if branch is false.
Use the if elseif else statement when you want to execute a code block based on the
results of multiple logical expressions.
ABAP case
Summary: in this tutorial, you will learn how to use the ABAP case statement.
case operand.
when expression_1.
[code_block_1]
when expression_2.
[code_block_2]
...
when expression_n.
[code_block_n]
when others.
[code_block_other]
endcase.
If the first match is found, the corresponding code block is executed. In case no match
found, the code_block_other in the when others branch is executed.
The when others branch is optional. If you omit it and there is no match, the processing
continues after endcase.
case gv_command.
when 'SAVE'.
gv_message = 'The document has been saved successfully.'.
when 'CANCEL'.
gv_message = 'Are you sure that you want to cancel?'.
endcase.
write gv_message.
How it works.
Summary
Use the case statement to control which code blocks to execute based on the value of an
expression.
ABAP do
Summary: in this tutorial, you will learn how to use the ABAP do statement to carry an
unconditional loop.
The do statement allows you to execute a code block in a specified number of times. The
following illustrates the syntax of the do statement:
do [n times]
[code_block]
enddo.
The n times limits the number of iterations in a do loop. The n can be a literal number like
1, 2, 3,… or a numeric expression of type i. If n is less than or equal to 0, the code_block will
not execute at all.
The n times is optional. If you omit it, you need to terminate the loop by the exit statement
in the code_block. Otherwise, the loop executes endlessly until the runtime environment
terminates it.
Within the code_block, you can access the system field sy-index that stores the number of
loops has been passed, which include both previous and the current iterations.
Typically, you use the do statement to fill an internal table with data or to construct
values.
ABAP do example
The following example uses the do loop to calculate and shows the first five square
numbers:
do 5 times.
gv_square = ipow( base = sy-index exp = 2 ).
write: / sy-index, gv_square.
enddo.
Output:
1 1
2 4
3 9
4 16
5 25
How it works.
Summary
ABAP while
Summary: in this tutorial, you will learn how to use the ABAP while statement to perform
a conditional loop.
while logical_expression.
[code_block]
endwhile.
In this syntax, the while loop evaluates the logical_expression first. If the result is true, the loop
is entered.
Before each new iteration, the while loop evaluates the logical expression again to
determine whether to execute the next iteration.
Because the while loop evaluates the condition before each iteration, the while loop is
often called a pretest loop.
Similar other loop statements such as do, you can access the system variable sy-index that
stores the current loop index.
Output:
Counter: 5 Iteration: 1
Counter: 4 Iteration: 2
Counter: 3 Iteration: 3
Counter: 2 Iteration: 4
Counter: 1 Iteration: 5
How it works.
Summary
ABAP exit
Summary: in this tutorial, you will learn how to terminate a loop immediately using the
ABAP exit staement.
Sometimes, you may want to terminate a loop prematurely. To do this, you use
the exit statement in the code block of the loop:
exit.
Typically, you use the exit statement in conjunction with an if statement inside a loop. For
example:
do n times.
" ...
" terminate the loop
if condition.
exit.
endif.
enddo.
while condition.
" ...
if condition.
exit.
endif.
endwhile.
If you use the exit statement in a nested loop, it will terminate the innermost loop.
do 5 times.
write / sy-index.
if sy-index = 2.
exit.
endif.
enddo.
Output:
1
2
How it works.
ABAP continue
Summary: in this tutorial, you will learn how to use the ABAP continue statement to skip
the current iteration of a loop.
continue.
do n times.
if condition.
continue.
endif.
" other code
enddo.
In this syntax, the continue statement will start a new loop iteration immediately. It ignores
all the code below the continue between do and enddo.
while condition.
if another_condition.
continue.
endif.
" other code
endwhile.
When you use continue statement in a nested loop, it skips the current iteration of the
innermost loop.
do 10 times.
if sy-index mod 2 = 1.
continue.
endif.
write / sy-index.
enddo.
Output:
2
4
6
8
10
How it works.
Summary
Use the continue statement to skip the current loop iteration and start a new one.