SAP ABAP Basic Concepts
SAP ABAP Basic Concepts
5. Data Types
▪ Data types are templates for creating data objects.
▪ A data type defines the technical attributes of data
objects.
▪ Data types do not use any memory space.
▪ Data types can be defined independently in the ABAP
program or in ABAP dictionary.
▪ Data Objects
▪ A data object is an instance of a data type.
▪ A data object holds the contents or data.
▪ It occupies the memory space based upon the data type
specified.
▪ Example - DATA lv_empid(20) TYPE n.
lv_empid = 10.
▪ In the above example, DATA = keyword, lv_empid is the
name of the data object,
TYPE = keyword, n(numeric) = is a data type of that data object.
Categories of Data Types
There are three categories of data types.
Elementary Types
▪ They are predefined data types.
▪ They are single data types. They are not composed of
other data types.
▪ Elementary datatypes are of 2 types:
▪ Fixed length data types - C(character) , N(numeric) ,
I(Integer) , P(packed number) , F(floating point) , D(date),
T(time) , X(hexadecimal).
▪ Variable length data types - String , Xstring.
▪ Complex Data Types
▪ There is no pre-defined complex data type in ABAP.
▪ They are the combination of elementary data types.
▪ There are 2 types of complex data types.
▪ Structure type
▪ Table type
▪ Reference Data Types
▪ There is no pre-defined reference data type.
▪ It describes data objects that contain references to other
objects.
▪ They are of 2 types of reference data type.
▪ Data reference
▪ Object reference
▪ Example : DATA lo_object TYPE REF TO zclass.
▪ In the above syntax - DATA = keyword , lo_object = name
of data object, TYPE REF TO = keyword , zclass = name of
already existing class.
▪ Types of Data Objects
The Data objects are of 2 types.
▪ Literals(unnamed data objects)
▪ Named data objects
▪ Literals (Unnamed Data Objects)
▪ Literals don’t have any name that’s why they are called as
unnamed data objects.
▪ They are fully defined by their value.
▪ There are 2 types of literals.
▪ Numeric literals - Numeric literals have sequence of
numbers. Examples - 123 , -4567 etc.
▪ Character literals - Character literals are sequences of
alphanumeric characters in single quotation marks.
Examples - ‘Test 123’ , ‘SAP ABAP’ etc.
▪ Named Data Objects
▪ Data objects that have a name are called as named data
objects.
▪ The various types of named data objects are as follows :
▪ Variables
▪ Constants
▪ Text symbols
▪ Variables
▪ Variables are data objects whose contents can be
changed.
▪ Variables are declared using the DATA, CLASS-DATA,
STATICS, PARAMETERS, SELECT-OPTIONS, and RANGES
keyword.
▪ Example
DATA lv_empid(20) TYPE n.
lv_empid = 10.
lv_empid = 20.
Constants
▪ Constants are data objects whose contents cannot be
changed.
▪ Constants are declared using the CONSTANTS keyword.
▪ Example
CONSTANTS lc_pi TYPE P DECIMALS 3 VALUE
‘3.141’.
▪ Text Symbols
▪ A text symbol is a data object that is not declared in the
program itself.
▪ It is defined as a part of the text elements of the program.
▪ Character and Numeric Data Types
▪ The C(character) , N(numeric), D(date), T(time) are
considered as character(non-numeric) data types.
▪ The I(Integer) , P(packed decimal), F(floating point) are
considered as numeric data types.
Write Statement
▪ The basic ABAP statement for displaying data on the
screen is write.
▪ Example
DATA: number TYPE i VALUE 1,
name(25) TYPE c VALUE 'Leena'.
CASE Statement
▪ It is a conditional statement.
▪ Every case statement ends with endcase.
▪ Multiple statements blocks are there, depends upon the
condition one block executes.
▪ If none of the conditions are satisfied, it goes to others
part.
Difference between Case & IF
▪ If we have multiple IF conditions , IF check all the
conditions, until it gets a true condition whereas in CASE ,
it directly jumps to the true condition.
▪ Case is performance effective as compared to IF.
▪ Loop
▪ Loop allows us to execute a group of statements multiple
times.
▪ The various loops are as follows:
▪ Do loop
▪ While loop.
▪ Loop at <itab> where itab stands for Internal table.
▪ Do Loop
▪ Do loop is called as a unconditional loop.
▪ Every do loop ends with enddo.
▪ Syntax: Do <n> TIMES.
<statement block>.
ENDDO.
While Loop
▪ While loop is called as a conditional loop.
▪ Every while loop ends with endwhile.
▪ Syntax : WHILE<condition>.
<statement block>.
ENDWHILE.
Loop Statements
▪ Exit - is used to exit from the loop.
▪ Continue - skip the current processing of the record and
then process the next record in the loop statement.
▪ Check - if the check condition is not true, loop will skip
the current loop pass and move to next loop pass.
▪ System Variables
▪ System variables are pre-defined variables in SAP.
▪ SYST is the structure for the system fields.
▪ All system fields are addressed using SY field name.
▪ The various system variables are as follows :
▪ SY-SUBRC - System variable for return code (successful =
0, not successful = other than 0).
SY-TABIX - It returns the current line index inside a loop.
➢ SY-INDEX - It returns the current line index inside do and
while Loop.
➢ SY-DATUM - System variable for current date (internal
format - yyyymmdd).
➢ SY-UNAME - It returns the logon name of the user.
➢ SY-UZEIT - It returns the current system time(internal
format - hhmmss)
➢ SY-UCOMM - System variable for user command.
➢ String
➢ A string is a collection of characters.
➢ String is an elementary data type of variable length.
➢ Imp point : String operations are applicable to data
objects having C(character),N(numeric),D(date),T(time)
and string data types.
➢ String Operations - Concatenate
➢ The purpose of concatenate is to combine the strings.
Syntax:
CONCATENATE <c1> <cn> INTO <c> SEPARATED
BY BY <s>.
In the above syntax: CONCATENATE = keyword , <c1>-----
--<cn> = individual strings , INTO = keyword , <c> = final
result string , SEPERATED BY = keyword, <s> = separator.
DATA: lv_1(10) type c VALUE 'NEC',
lv_2(10) type c VALUE 'Software',
lv_3(10) type c VALUE 'Solutions',
Substring Processing
▪ Substring is a part of the string or small set of characters
from the string.
▪ Depends upon the requirement we need to process the
substring.
▪ Syntax : Target variable = Source variable[+][starting
position of substring](length of the substring).
DATA : lv_value(30) type c VALUE '91-040-
6789990004',
lv_county(2) type c,
lv_city(3) type c,
lv_number(10) type c.
lv_county = lv_value+0(2).
WRITE : /'The Country Code - ' , lv_county.
lv_city = lv_value+3(3).
WRITE : / 'The City Code - ', lv_city.
lv_number = lv_value+7(10).
WRITE : / 'The Phone Number - ', lv_number.
IF lv_1 co lv_2.
WRITE : 'TRUE' , Sy-fdpos.
else.
WRITE : 'FALSE' , Sy-fdpos.
ENDIF.
Output :- TRUE 20
CA (contains any)
DATA : lv_1(20) TYPE c VALUE 'Sathi@1234',
lv_2(20) TYPE c VALUE '0123456789'.
IF lv_1 CA lv_2.
WRITE : sy-fdpos.
ELSE.
WRITE : sy-fdpos.
ENDIF.
Output:- 6
NA (contains not any)
IF lv_1 NA lv_2.
WRITE : 'TRUE', sy-fdpos.
ELSE.
WRITE : 'FALSE' , sy-fdpos.
ENDIF.
Output: - FALSE 6
CS(contains string)
ATA : lv1(30) TYPE c VALUE 'Sathish Derangula',
lv2(20) TYPE c VALUE 'Sathish'.
IF lv1 CS lv2.
WRITE : 'True' , sy-fdpos.
ELSE.
WRITE : 'False', sy-fdpos.
ENDIF.
Output :- TRUE 0
NS(contains no string)
IF lv1 NS lv2.
WRITE : 'True' , sy-fdpos.
ELSE.
WRITE : 'False', sy-fdpos.
ENDIF.
Output :- TRUE 30
CP (contains pattern)
DATA : lv1(30) TYPE c VALUE 'Sathish Derangula',
lv2(20) TYPE c VALUE '*Dera*'.
IF lv1 CP lv2.
WRITE : 'True' , sy-fdpos.
ELSE.
WRITE : 'False', sy-fdpos.
ENDIF.
Output :- TRUE 8
NP(contains no pattern)
IF lv1 NP lv2.
WRITE : 'True' , sy-fdpos.
ELSE.
WRITE : 'False', sy-fdpos.
ENDIF.
Output :- TRUE 30
▪ Internal Tables
▪ Internal table is a temporary storage of data on
application layer.
▪ Internal table stores any number of records at run time.
▪ A very important use of internal tables is for storing and
formatting data from a database table within a program.
▪ Work Area in Internal Tables
▪ Work area is also a temporary storage of data on
application layer.
▪ Work area are single rows of data.
It is used to process the data in an internal table, one line at a time.
Internal Table Operations
1. Append → It is used to insert the data at the last of the internal table.
SYNTAX:- APPEND ls_emp to lt_emp.
2. Loop → It is used to read the records one by one from the internal
table.
SYNTAX:-LOOP AT lt_emp INTO ls_emp.
ENDLOOP.
6. Read table → It is used to read the first matching record from the
internal table.
SYNTAX:-
READ TABLE lt_emp INTO ls_emp WITH KEY emp_id
= 9.
1) They are the special type of internal table which works on HASH
algorithms. We need to specify the unique key while declaring the
internal table.
2) They are not the index based Internal tables.
3) Records can be inserted.
4) There is no impact of SORT, as we read the record based upon the
hashed algorithm.
5) We Read a Record using Key, index is not applicable.
6) Hashed algorithm is used to search a record.
7) Response time is faster as compared to Standard and sorted
internal tables, as response time is independent of number of
entries. It is well suited for tables, where a table has huge
number of records and we want to search based upon unique key.
Selection Screen
▪ Selection screen is also called as Input screen.
▪ With the help of selection screen user provides a input to
the program.
▪ There are 2 ways to provide the input to the program.
▪ Parameters
▪ Select-options
Parameters
▪ Parameters are used to pass the single input.
▪ The various parameters variations are as follows:
▪ PARAMETERS <p> ...... DEFAULT <f> ......
▪ PARAMETERS <p> ...... OBLIGATORY ......
▪ PARAMETERS <p> ...... AS CHECKBOX ......
▪ PARAMETERS <p> ...... RADIOBUTTON GROUP <radi>......
▪ Select-Options
▪ Select-options are used to pass a range of inputs.
▪ The various Select-options variations are as follows:
SELECT-OPTIONS <seltab> FOR <f> ... DEFAULT <g> [TO
<h>]
SELECT-OPTIONS <seltab> FOR <f> ... NO-EXTENSION
Hiding Multiple selections
SELECT-OPTIONS <seltab> FOR <f> ... NO INTERVALS
Hiding High values
SELECT-OPTIONS <seltab> FOR <f> ... OBLIGATORY
A select-option has 4 parts.
▪ Sign - I/E(include/exclude)
▪ Option - Relational operator (EQ, BT, LT etc.)
▪ Low - Low value
▪ High - High value
Selection Screen Block
LINE COMMENT - Starting position of the comment + length
SELECTION-
SCREEN : BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT
-001.