1-Saphub Abap Basics
1-Saphub Abap Basics
1-Saphub Abap Basics
ABAP stands for Advanced Business Application Programming. It is a programming language developed by
SAP.
ABAP language syntax
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.
WRITE 'COMMENT'.
"Start of comment
On the logon pad select the system you want to login to and press log on button.
This is SAP easy access screen. All the tools required by ABAP developer is under the node Tools.
DESCRIPTION
SE11
SE16
Data Browser
SE37
Function Builder
SE38
ABAP Editor
SE41
Menu Painter
SE51
Screen Painter
SE71
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 editors initial screen. Enter the name of the program you want to
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.
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 continue. Then run(F8) the program.
DESCRIPTION
DEFAULT LENGTH
DEFAULT VALUE
Character
Numeric
Date
00000000
Time
000000
Hexa Decimal
X0
Integer
Packed
Float
User
defined
data types
TYPE p DECIMALS 2,
counter
TYPE i,
id(5)
TYPE n.
TYPE n,
name(10)
TYPE c,
dob
TYPE d,
place(10) TYPE c,
END OF student.
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.
Use keyword DATA to declare a variable.
DATA: firstname(10) TYPE c,
index
TYPE i,
student_id(5) TYPE n.
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.
DATA: firstname(10) TYPE c,
lastname(10)
Structured Variable
Similar to structured data type, structured variable can be declared using BEGIN OFand END OF keywords.
DATA: BEGIN OF student,
id(5)
TYPE n,
name(10)
TYPE c,
dob
TYPE d,
place(10) TYPE c,
END OF student.
We can also declare a structured variable by referring to an existing structured data type.
TYPES: BEGIN OF address,
name(10)
TYPE c,
street(10) TYPE c,
place(10)
TYPE c,
pincode(6) type n,
phone(10)
type n,
END OF address.
Data: house_address
type address,
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.
Character is the default data type.
DATA: true.
Output
Basic Operations
Assigning values to ABAP
Use = or MOVE keyword to assign a value to a variable.
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
variables
*Using Keywords
add 10 to a.
subtract 5 from b.
multiply c by 2.
divide d by 2.
Output
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.
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 andENDIF is executed if the condition is False.
DATA: a TYPE i VALUE 1.
IF a > 5.
WRITE:/ 'Condition True'.
ELSE.
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
ENDDO.
Output
Output
IF sy-index = 2.
CONTINUE.
ENDIF.
WRITE sy-index.
ENDDO.
Output
Output
Output
String Operations
CONCATENATE Combines 2 or more strings into one string.
DATA: s1(10) VALUE 'Hello',
s2(10) VALUE 'ABAP',
s3(10) VALUE 'World',
result1(30),
result2(30).
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.
SPLIT Splits a string into 2 or more smaller strings.
DATA: s1(10), s2(10), s3(10),
source(20) VALUE 'abc-def-ghi'.
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.
DATA: string(30) VALUE 'SAP ABAP Development',
str(10) VALUE 'ABAP'.
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.
DATA: string(30) VALUE 'SAP ABAP Development',
str(10) VALUE 'World'.
Output