1. ABAP statements
- They consist of command, any variables and options.
- They end with a period (.).
- There is no restriction on the layout of statements in ABAP. We can place multiple statements on a single line or stretch a single statement across multiple lines.
- Statements can begin anywhere on the line. ABAP does not care where a statement begins on a line. We should take advantage of this fact to improve the readability of our program by using indentation to indicate blocks of code.
-
We can place blank lines anywhere in the program. Blank lines should be used to separate blocks of code.
WRITE 'statement 1'. WRITE 'statement 2'.
WRITE 'statement can begin anywhere on the line'.
2. Commenting code
- Comments are those statements that are not processed by the system.
- Programmers use comments to document the purpose of various statements in a program.
- Comments play a very important role as they can save time and money when the code needs to be modified.
- It is the responsibility of the programmer to leave behind well documented code.
- Most companies do not consider a programming job complete until it is fully documented.
-
Comments in ABAP are of 2 types:
1. Full-line comments
They are created by placing an asterisk (*) at the first position of the line. In this case, entire line is considered to be a comment by the system. They do not need to be terminated by the period because they may not extend across more than one line:
* display balance amount on the screen
WRITE balance_amt.
2. Partial-line comments
They are created by placing a double quote (“) after a statement. All text following the double quote is considered to be a comment by the system. They do not need to be terminated by the period because they may not extend across more than one line:
WRITE balance_amt. "display balance amount on the screen
3. Colon notation
- This notation is used to chain the consecutive statement if the beginning of each statement is identical.
- This notation saves the programmer’s time spent in repetitive typing. This notation uses colon (:) operator and comma (,).
- Commas are used to end individual statements like periods end normal statements.
- The final identical statement is terminated with a period (.).
- Consider the following code :
WRITE 'Welcome'.
WRITE user_name.
WRITE 'to the ABAP world'.
Using colon notation, it could be rewritten as :
WRITE: 'Welcome',
user_name,
'to the ABAP world'.
Above statement can also be written as :
WRITE: 'Welcome', user_name, 'to the ABAP world'.
- Consider the following code :
MOVE sy-subrc TO lv_one.
MOVE sy-subrc TO lv_two.
MOVE sy-subrc TO lv_three.
Using colon notation, it could be rewritten as :
MOVE sy-subrc TO: lv_one,
lv_two,
lv_three.
4. Defining Programs(Report)
Report is the first non-comment line in the program. It names the program and can also be used to format the output.
Following is the syntax for the report statement:
REPORT program_name
<NO STANDARD PAGE HEADING>
<LINE-SIZE number_of_columns>
<LINE-COUNT number_of_lines[(number_of_lines_reserved_for_footer)]>
<MESSAGE-ID message_id>.
SAP automatically generates the REPORT statement whenever a new program is created, although it has no options.
These options are described below:
<NO STANDARD PAGE HEADING>
It disables the default page heading generated by SAP. It allows the programmer to define their own page header.
REPORT ZTEST NO STANDARD PAGE HEADING.
<LINE-SIZE number_of_columns>
It defines the size of line(maximum number of characters that a line can contain). The size can be upto 255 characters.
REPORT ZTEST LINE-SIZE 40.
<LINE-COUNT number_of_lines[(number_of_lines_reserved_for_footer)]>
It defines number of lines for each page of the output. The second option is meant for reserving the specific number of lines for footer. If we want a page footer, we must reserve the space for the footer using this option because SAP does not provide default page footer.
REPORT ZTEST LINE-COUNT 30(2).
<MESSAGE-ID message_id>
It defines the set of messages that can be used in the program when the MESSAGE command is used.
REPORT ZTEST MESSAGE-ID Z1.
Example:
REPORT ZTEST
NO STANDARD PAGE HEADING
LINE-SIZE 40
LINE-COUNT 30(2)
MESSAGE-ID Z1.
5. Variables
Like other programming languages, ABAP also allows programmers to use variables in a program. A variable is a symbolic reference to a memory location that stores data temporarily.
They persist only during the execution of the program in which they are declared and the data they contain is lost when the execution of program is completed.
Consider the following rules when using variables in your program:
- All variables must be declared before they are used in the program.
- Variables can be defined at any point in a program but it is a good practice to define variables at the starting of the program.
- A variable name can be up to 30 characters in length. Variable name should clearly specify the function of variable.
- A variable name should begin with a letter and then any mixture of letters and numbers. It should not contain special symbols(*,%,<,>,? Etc.). An underscore can be used to separate different words in a variable.
- ABAP keywords cannot be used as a variable name.
- All variables defined at the program level are considered global and are visible to all subroutines defined in that program. Whereas variables defined within a subroutine are visible only to that subroutine.
Predefined datatypes in ABAP:
There are 2 main categories of datatypes in ABAP: character and numeric
1. Character data types-
Data type
|
Description
|
Default Internal Length
|
Maximum
Internal
Length
|
Valid
Values
|
Default Initial
Value
|
c
|
Character
|
1
|
65535
|
Any
char
|
Blank
|
n
|
Numeric text
|
1
|
65535
|
0-9
|
0
|
d
|
Date
|
8(fixed)
|
-
|
0-9
|
00000000
|
t
|
Time
|
6(fixed)
|
-
|
0-9
|
000000
|
x
|
Hexadecimal
|
1
|
65535
|
Any
|
|
Note:
- Internal length is given in bytes.
- A dash is given in maximum internal length field for the fixed length datatypes.
- Default initial value indicates the value assigned to the variable by default. This default value is specified using the ‘Value’ addition (discussed below).
- Date and time are fixed length data types. We should not specify the length for date and time variables while declaring them using data statement (discussed below).
- Values for data and time variables are stored internally as YYYYMMDD and HHMMSS respectively.
2. Numeric data types-
Data type
|
Description
|
Default Internal Length
|
Maximum
Internal
Length
|
Maximum
Decimals
|
Default Initial
Value
|
i
|
Integer
|
4(fixed)
|
-
|
0
|
0
|
f
|
Floating-point
|
8
|
8
|
15*
|
0.0
|
p
|
Packed
decimal
|
8
|
16
|
14
|
0
|
Note:
- A dash is given in maximum internal length field for the fixed length datatypes.
- An asterisk(*) indicates that attribute is machine dependent.
Defining variables using DATA statement:
Variables can be defined using data statement as follows-
- DATA var<(length)> <TYPE type> <DECIMALS number> <VALUE intial_value>
- DATA var LIKE table-field <VALUE initial_value>
Note:
- If no length is specified, the variable uses default length for that datatype.
- If no datatype is specified, the variable defaults to character(c) datatype.
Form 1: DATA var<(length)> <TYPE type> <DECIMALS number> <VALUE intial_value>
This form explicitly defines the datatype and length of variable. This form should be used when variable is not dependent on the values stored in the SAP database such as counters or flags.
Example:
DATA: unit_price(5) TYPE F DECIMALS 2 VALUE ‘1.00’.
This statement creates a variable named ‘unit_price’ which is a floating point of length 5 with 2 spaces reserved for decimals and a default value of 1.00.
Note:
Decimal value must be surrounded by single quotes in ABAP as in above example just like the string is surrounded by single quotes. Numbers without a decimal can be written without the single quotes.
Form 2: DATA var LIKE table-field <VALUE initial_value>
In this form, length and datatype of variable is determined dynamically at runtime. The sap system looks for the datatype and length of the specified field and then it uses this information to define the variable.
Example:
DATA: pur_docno LIKE EKKO-EBELN VALUE 450000000. ”EBELN is a field in table EKKO for storing Purchasing Document number
This statement creates a variable pur_docno that has the same datatype and length as the field EBELN with a default value of 4500000000.
Record
A record is a group of several variables. It is used when we have a group of related data that needs to be kept together. A record is declared by using a group of data statements. Variables in a record are referenced as record_name – field_name.
Following is the syntax for declaring variables:-
DATA BEGIN OF record_name.
DATA field_1.
DATA field_2.
.
.
DATA field_N.
DATA END OF record_name.
The individual fields within the record can be defined using any form of DATA statement as in the following example:-
DATA BEGIN OF purchase_order.
DATA po_number LIKE EKPO-EBELN.
DATA item_number LIKE EKPO-EBELP.
DATA short_text LIKE EKPO-TXZ01.
DATA flag(1) TYPE C VALUE 'N'.
DATA item_price(9) TYPE p DECIMALS 2.
DATA END OF purchase_order.
Note:
- EKPO is a standard table in SAP for storing information regarding items in a purchase order.
- EBELN is a field in EKPO for storing purchasing document number.
- EBELP is a field in EKPO for storing purchasing document item number.
- TXZ01 is a field in EKPO for storing short text of item.
In above example, if you have to access the field “po_number”, then you have to use the notation: purchase_order-po_number.
With colon notation, the above example can be rewritten as follows:
DATA: BEGIN OF purchase_order,
po_number LIKE EKPO-EBELN,
item_number LIKE EKPO-EBELP,
short_text LIKE EKPO-TXZ01,
flag(1) TYPE C VALUE ‘N’,
item_price(9) TYPE P DECIMALS 2,
END OF purchase_order.
Now you observe that readability of the code is improved.
Defining new data types using TYPES statement:
In addition to 8 predefined data types(C,D,T,X,P,F,I,N) in ABAP,we can create new data types in ABAP using the “types” statement. New data types are created to meet the business needs and are called as “user defined types”.
Suppose a financial program needs to make a reference to bank account numbers. As we know there are several types of bank account numbers like customer bank account number, merchant bank account number etc.
So, separate variables can be declared for these bank account numbers as follows:
DATA: CUST_ACCNO(18) TYPE N,
MERC_ACCNO(18) TYPE N.
In this example, we have defined 2 variables CUST_ACCNO and MERC_ACCNO in terms of standard ABAP ‘numeric’ data type. Instead, we could define these variables in terms of our own data type say BANK_ACCOUNT.
TYPES: BANK_ACCOUNT(18) TYPE N.
DATA: CUST_ACCNO TYPE BANK_ACCOUNT,
MERC_ACCNO TYPE BANK_ACCOUNT.
In this case, if the length of bank account number gets changed from 18 to 25. Then, we would need to modify only the BANK_ACCOUNT variable not the CUST_ACCNO and MERC_ACCNO variables.
“Types” statement is used to declare new types and has 3 possible forms:-
1. TYPES name<(length)> <TYPE type> <DECIMALS number>
2. TYPES name LIKE table-field
3. TYPES BEGIN OF record_type.
........
TYPES END OF record_type.
Form 1: TYPES name<(length)> <TYPE type> <DECIMALS number>
The syntax of TYPES statement is similar to the data statement. Form1 defines a data type explicitly. This statement declares only a data type not a variable. We still need to declare the variable by using the DATA statement.
Example:
TYPES NAME_TYPE(15) TYPE C.
TYPES PRICE_TYPE(8) TYPE P DECIMALS 3.
TYPES QTY_TYPE TYPE I.
Using colon notation, it can also be written as:
TYPES: NAME_TYPE(15) TYPE C,
PRICE_TYPE(8) TYPE P DECIMALS 3,
QTY_TYPE TYPE I.
Form 2: TYPES name LIKE table-field
In this form, the data type and length is determined dynamically at runtime. The system determines the data type and length of the specified field of table. Then system uses this information to define the data type.
Example:
TYPES FLAG_TYPE LIKE SY-SUBRC.
TYPES TEXT_TYPE LIKE EKPO-TXZ01.
Above example creates 2 user-defined data types. FLAG_TYPE whose length and data type is similar to that of SY-SUBRC. TEXT_TYPE whose length and data type is similar to that of TXZ01 field in table EKPO.
Note:
SY-SUBRC is a system field in SAP for storing the return code of the statement.
Form 3:
TYPES BEGIN OF record_type.
........
TYPES END OF record_type.
A record type is similar to the variable record. A variable record is a group of several variables. Similarly, a record type is a group of several data types. Using colon notation, the syntax can also be written as:
TYPES: BEGIN OF record_type,
.....
.....
END OF record_type.
Example:
TYPES: BEGIN OF PART_LIST,
PART_NAME(20) TYPE C,
MATERIAL_CODE LIKE EKPO-MATNR,
DESC LIKE EKPO-TXZ01,
PRICE TYPE P DECIMALS 2,
END OF PART_LIST.
The above example creates a user-defined record type PART_LIST with types: PART_NAME of length 20 and data type C, MATERIAL_CODE with length and data type similar to that of MATNR field in table EKPO and DESC with length and data type similar to that of TXZ01 field in table EKPO. A variable record can be created from above record type as follows:-
DATA: OLD_PART TYPE PART_LIST,
NEW_PART TYPE PART_LIST.
Defining constant using CONSTANTS statement:
Constants are those data elements whose value does not change throughout the execution of program. Their value remains fixed. They must be assigned an initial value when they are declared and this initial value never changes. If you try to change the value of a constant, a syntax error or runtime occurs.
Constants are defined using CONSTANTS statement. The syntax of CONSTANTS statement is similar to that of the DATA statement, with an exception that “VALUE” addition is always required and “OCCURS” option is never allowed.
The syntax for CONSTANTS statement is as follows:-
1. CONSTANTS name<(length)> <TYPE type> VALUE value <IS INITIAL>
2. CONSTANTS name LIKE table-field VALUE value <IS INITIAL>
3. CONSTANTS: BEGIN OF rec,
.........
END OF rec.
Example:
CONSTANTS PI(3) TYPE P DECIMALS 2 VALUE ‘3.14’.
CONSTANTS C1 TYPE I VALUE IS INITIAL.
CONSTANTS ITEM_NUM LIKE EKPO-EBELP VALUE 45000000.
CONSTANTS: BEGIN OF raj_address,
name(10) TYPE C VALUE ‘RAJ SHARMA’,
street(20) TYPE C VALUE ‘HT PARK ROAD’,
city(10) TYPE C VALUE ‘KASHIPUR’,
postal_code(6) TYPE N VALUE ‘244713’,
END OF raj_address.
The components of this constant can be accessed as raj_address-name, raj-address-street and so on, but they cannot be changed.
6. Using Parameters:
Parameters are used to obtain data from the user and pass that data to the ABAP program when the program is executing. There are 2 types of parameters in ABAP. One parameter allows user to enter a single value whereas second parameter allows user to enter range of values.
Type1: Accepting Single value from user using Parameters statement
The PARAMETERS statement is used to define parameter for accepting single value from the user. It has the following syntax:-
PARAMETERS para_name<(length)> <TYPE type> <LIKE field> <DEFAULT val> <LOWER CASE> <AS CHECKBOX> <RADIOBUTTON GROUP num> <OBLIGATORY>.
Like variables, data type of PARAMETERS can be specified by using TYPE or LIKE options. But unlike the variables, a parameter can have up to 8 characters or digits. A parameter cannot use the DECIMALS option. Also, a parameter cannot be part of a record.
The description of various options used in parameters statement is as follows:-
- ‘DEFAULT val’ option is used to assign a default value to the parameter just like the VALUE option of the DATA statement. This default value is seen by the user at the runtime and can be overwritten by the user.
PARAMETERS: p1 type i DEFAULT 100.
- ‘LOWER CASE’ option allows entry of lower case data. If this option is not specified, all data is converted into uppercase.
- ‘AS CHECKBOX’ option is used only with a parameter one character in length. This option creates a push button that the user can click to be on or off. If the user clicks the push button to ON, then the parameter is set to ‘X’.
- ‘RADIOBUTTON GROUP’ option is used only with a parameter one character in length just like the AS CHECKBOX option. It also creates a push button that the user can click to on or off. The difference is several push buttons can be grouped together and the user can select one of them.
- ‘OBLIGATORY’ option forces user to enter a value. The program does not execute until the user specifies a value.
Examples of parameters statement:-
PARAMETERS P_NAME1(20) TYPE C OBLIGATORY. "defines a mandatory field
PARAMETERS P_NAME2(20) TYPE C. "defines a normal field
Notice the symbol in P_NAME1 field. It indicates an obligatory field. If you try to execute the program without entering data in P_NAME1 field, you get the following message:-
The above statements creates an obligatory parameter named P_NAME1 of length 20 and data type character and another parameter P_NAME2 of length 20 and data type character. The user must enter a value in P_NAME1 in order to execute the program.
PARAMETERS P_FILENM(30) TYPE C DEFAULT 'file1.txt' OBLIGATORY LOWER CASE.
The above statement creates a parameter named P_FILENM of length 30, data type character, default value file1.txt. The parameter allows only lowercase letters and must be filled in before the program will execute.
PARAMETERS: P_CHECK1 AS CHECKBOX DEFAULT 'X',
P_CHECK2 AS CHECKBOX,
P_CHECK3 AS CHECKBOX.
The above set of statements creates 3 checkboxes out of which P_CHECK1 is set to ON by default. The user choose either of them or all of them.
PARAMETERS: P_MALE RADIOBUTTON GROUP 001,
P_FEMALE RADIOBUTTON GROUP 001 DEFAULT ‘X’.
The above set of statements creates 2 radio buttons out of which P_FEMALE is set to ON by default. The user choose anyone out of these 2.
Type2: Accepting multiple values from user using SELECT-OPTIONS statement
SELECT-OPTIONS
statement creates 2 input fields that allows user to enter range of values. The syntax of
SELECT-OPTIONS
statement is as follows:-
SELECT-OPTIONS <so_name> FOR <table-field>. "This creates 2 input fields with extension.
SELECT-OPTIONS <so_name> FOR <table-field> NO INTERVALS."This creates 1 input field with extension
SELECT-OPTIONS <so_name> FOR <table-field> NO EXTENSION."This creates 2 input fields without extension
SELECT-OPTIONS <so_name> FOR <table-field> NO INTERVALS NO-EXTENSION."This creates one input field without extension
Some examples of
SELECT-OPTIONS
statement are as follows:-
1)TABLES: MARA.
SELECT-OPTIONS S_MATNR FOR MARA-MATNR.
The above statement creates a select-option with intervals and extension.
Note:
While using SELECT-OPTIONS statement, TABLES statement is used to mention the name of table for which you are creating select-option.
2)
TABLES: MARA.
SELECT-OPTIONS S_MATNR1 FOR MARA-MATNR NO INTERVALS.
The above statement creates a select-option without intervals.
3)
TABLES: MARA.
SELECT-OPTIONS S_MATNR1 FOR MARA-MATNR NO-EXTENSION.
The above statement creates a select-option without extension.
4)
TABLES: MARA.
SELECT-OPTIONS S_MATNR FOR MARA-MATNR NO INTERVALS NO-EXTENSION.
The above statement creates a select-option without intervals and without extensions.