ABAP Basics: ABAP Stands For Advanced Business Application Programming. It Is A Programming
ABAP Basics: ABAP Stands For Advanced Business Application Programming. It Is A Programming
ABAP Basics: ABAP Stands For Advanced Business Application Programming. It Is A Programming
ABAP Language
Chained statements. If consecutive statements have identical part at the beginning, then
ABAP allows you to chain these statements into a single statement. First write the
identical part once and then place a colon (:). Then write the remaining parts of the
individual statements separated by commas. Normal Statements:
WRITE 'Hello'.
WRITE 'ABAP'.
Chained Statement:
Comments. If you want to make the entire line as comment, then enter asterisk (*) at the
beginning of the line.
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
SE11 ABAP Data Dictionary
SE16 Data Browser
SE37 Function Builder
SE38 ABAP Editor
SE41 Menu Painter
SE51 Screen Painter
SE71 SAP Script Layout
SE80 ABAP Workbench
SE91 Message Maintenance
SE93 Maintain Transaction
Double click on “ABAP Editor” to open the editor. ABAP editor can also be opened by entering
t-code SE38 in the command field.
This is the ABAP editor’s initial screen. Enter the name of the program you want to create and
press creates. 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.
This is the screen where you can write the ABAP code.
Write the code. Press save, then syntax check (Ctrl + F2).
If there are any syntax errors, it will be displayed at the bottom of the screen as shown above.
Correct the errors and again check the syntax.
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 continues. Then run (F8) the program.
The output will be displayed as shown above.
Data Type describes the technical characteristics of a Variable of that type. Data type is just
the blue print of a variable.
Structured data type is grouping of several simple data types under one name.
Use the keywords BEGIN OF and END OF to create a structured data type.
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 Variables
ABAP Variables are instances of data types. Variables are created during program execution
and destroyed after program execution.
While declaring a variable we can also refer to an existing variable instead of data type. For that
use LIKE instead of TYPE keyword while declaring a variable.
Structured Variable
Similar to structured data type, structured variable can be declared using BEGIN OFand END
OF keywords.
We can also declare a structured variable by referring to an existing structured data type.
Each individual field of the structured variable can be accessed using hyphen (-). For example,
name field of the house_address structure can be accessed using housing_address-name.
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
Basic Operations
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
DATA: a TYPE i,
b TYPE i.
a = 10 + 20.
b = 20 - 10.
clear: a, b.
Output
Control Statements
To control the flow of the ABAP program use the following statements.
IF – Branching Conditionally
IF statement – The code between IF and ENDIF is executed only if the condition is true.
DATA: a TYPE i VALUE 10. " We can assign a value in the declaration
IF a > 5.
WRITE:/ 'Condition True'.
ENDIF.
Output
IF-ELSE statement – The code between IF and ELSE is executed if the condition is true, the
code between ELSE and ENDIF is executed if the condition is False.
IF a > 5.
WRITE:/ 'Condition True'.
ELSE.
WRITE:/ 'Condition False'.
ENDIF.
Output
IF a > 5.
WRITE:/ a, 'Greater Than', 5.
ELSEIF a > 4.
WRITE:/ a, 'Greater Than', 4.
ELSEIF a > 3.
WRITE:/ a, 'Greater Than', 3.
ELSE.
WRITE:/ a, 'Less Than', 3.
ENDIF.
Output
CASE a.
WHEN 3.
WRITE:/ a, 'Equals', 3.
WHEN 4.
WRITE:/ a, 'Equals', 4.
WHEN OTHERS.
WRITE:/ 'Not Found'.
ENDCASE.
Output
Loops
DO – ENDDO – Unconditional Loop
DO can be used to execute a certain lines of codes specific number of times.
DO 5 TIMES.
WRITE sy-index. " SY-INDEX (system variable) - Current loop pass
ENDDO.
Output
WHILE can be used to execute a certain lines of codes as long as the condition is true.
Output
After continue the control directly goes to the end statement of the current loop pass ignoring the
remaining statements in the current loop pass, starts the next loop pass.
DO 5 TIMES.
IF sy-index = 2.
CONTINUE.
ENDIF.
WRITE sy-index.
ENDDO.
Output
If the condition is false, the control directly goes to the end statement of the current loop pass
ignoring the remaining statements in the current loop pass, starts the next loop pass.
DO 5 TIMES.
CHECK sy-index < 3.
WRITE sy-index.
ENDDO.
Output
After EXIT statement the control goes to the next statement after the end of loop statement.
DO 10 TIMES.
IF sy-index = 2.
EXIT.
ENDIF.
WRITE sy-index.
ENDDO.
Output
String Operations
WRITE / result1.
WRITE / result2.
Output
If the the concatenated string fits in the result string, then the system variable sy-subrc is set to 0.
If the result has to be truncated then sy-subrc is set to 4.
Output
If all target fields are long enough and no target fields has to be truncated then sy-subrc is set to
0, else set to 4.
SEARCH – Searches for a sub string in main string. If found then sy-subrc is set to 0, else set to
4.
Output
REPLACE – Replaces the sub string with another sub string specified, in the main string. If
replaced successfully then sy-subrc is set to 0, else set to 4.
Output