Basics of ABAP
Basics of ABAP
Basics of ABAP
You can also see the use of Predefined Data type and reference
type.
The initial Screen appear. Here you can enter a Program name directly or
list of all programs of a certain development class.
Enter the Program name directly
Enter a program name as per naming conventions into the object list and
choose display.
If the program does not exist, a dialogue screen appears, asking you
whether to create the program.
Otherwise object browser opens the specified screen.
To open ABAP programs directly using the ABAP Editor, choose ABAP
Editor in the ABAP Workbench screen or start Transaction SE38, and
enter a program name.
Maintaining an existing program
•Declarative keywords:
These keywords define data types or declare data objects.
Example: TYPES, DATA, TABLES
•Modularization keywords:
These keywords define the processing blocks in an ABAP
program. The modularization keywords can be further divided into:
* Event Keywords:
Example:
AT SELECTION SCREEN, START-OF-SELECTION, AT USER-COMMAND.
* Defining keywords:
Example:
FORM ..... ENDFORM, FUNCTION ... ENDFUNCTION,
MODULE ... ENDMODULE.
Keywords
•Control keywords:
You use these keywords to control the flow of an ABAP program
within a processing block.
Example: IF, WHILE, DO, FOR, CASE.
•Calling keywords:
You use these keywords to call processing blocks that you have
already defined.
Example: PERFORM, CALL, SET USER-COMMAND, SUBMIT, LEAVE TO
•Operational keywords:
These keywords process the data that you have defined using
declarative statements.
Example: WRITE, MOVE, ADD.
Comments:
Comments are text elements which you can write between the
statements of your ABAP/4 Program to explain it’s purpose to the
reader.Comments are flagged by special characters which causes
system to ignore them. You should comment the document your
program internally. Comments help other users to understand or
change the program.
Structure of comments:
There are two ways to insert comment in the program
A) For entire line to be comment enter (*) at the beginning of the Line.
B) If you want to enter part of line as comment, enter a double quotation
mark before the comment.
Syntax Structure
Example:
Example:
WRITE: SPFLI-CITYFROM, SPFLI-CITYTO, SPFLI-AIRPTO.
Types of ABAP/4 Programs
• Report programs
• Dialog programs
Report programs:
Dialog programs
Dialog programs read and change database tables. They are also
called as Module pool programs. Dialog programs accept user
information,process the information and update the database. For this
reason module pool programs cannot be executed in background.
Workbench Tools
• Object browser:
This utility offers a hierarchical overview of all R/3 repository objects.
•ABAP/4 Dictionary:
The ABAP Dictionary centrally describes and manages all the data
definitions used in the system.
•ABAP/4 Editor:
You use the ABAP Editor to create and edit your programs and their
components.
Workbench tools
•Function library:
The function library allows you to create and maintain the
function modules.
•Screen painter:
This ABAP Workbench tool allows you to create screens
for your transactions. SAP R/3 provides two modes (graphical
and alphanumeric modes) of the Screen Painter.
•Menu painter:
The Menu Painter is a tool with which you design user
interfaces.
ABAP/4 Dictionary : Initial screen
Screen Painter - Initial Screen
Menu Painter - Initial screen
Data Types
The following list is an overview of the main features of data types and
objects:
•Data types: A data type describes the technical attributes of all the
objects with that type. There is no memory associated with data types.
Data types Predefined User-defined
Elementary C,D,F,I,N,P,T,X ABAP/4 Contains User defined
eight predefined elementary data elementary data
types typesare based on the
predefined
Elementary data types
User-defined:
elementary data types are based entirely on predefined
elementary data types. To define your own elementary data types, you
use the TYPES statement
Example:
TYPES: NUMBER TYPE I.
DATA: NO_FLIGHTS TYPE NUMBER
Structured Data Types:
Structured Data types are made up of other types and generally are user
defined. There are two kind of structured data types.
•Field Strings:
A field string is a collection of other data types.You Define Field
string with the TYPES statement or with the DATA statement. structures
as components.
Internal Tables:
Internal tables consists of a several lines that all have the same
data type. Unlike field strings, which extends only horizontally, Internal
Table also extends vertically.You define Internal Table with occurs
parameter of the types or Data statement
Internal tables are characterized by:
The line type
The key and
The access method.
ABAP/4 Program Layout
Data objects contain the data with which ABAP programs work at runtime.
A) Literal
They are not created by declarative statements. Instead, they exist
in the program code. They have fixed technical attributes but no name.
Hence they are also referred to as unnamed data objects.
Text symbols are pointers to texts in the text pool of the ABAP
program.
Variables are data objects whose contents can be changed using
ABAP statements.
Constants are data objects whose contents cannot be changed.
Interface work areas are special variables that serve as interfaces
between programs, screens, and logical databases.
Predefined Data Objects
They do not have to be declared explicitly - they are always
available at runtime.
Dynamic Data Objects
You create them dynamically using data references. They do
not have a name.
Internal Data objects
•Internal data objects are created for use in one particular program. They
have no validity outside that program.
Space:
The data Object SPACE is a constant of type C. It is one character
long and contains a space.
System fields:
All System fields have names with the format SY-<name>, where
<name> specifies an individual field. To display a list of available system
fields in the ABAP/4 Editor, type SHOW SY in the command line.
Example:
SY-UNAME : login name of the user
SY-DATUM : current date
SY-UZEIT : current time
Special data objects
- Parameters
Parameters are variables which are linked to a selection screen. They can
accept values after a program is started.
- Selection criteria
Selection criteria are special internal tables used to specify value ranges.
They are linked to a selection screen.
Creating Data Objects and Types
Apart from literals, you must declare each data object with a declarative
statement . In declarative statements, you must specify the data type of all
data objects
You define the data type of an object in the declarative statement, either
directly, using <declaration>…………TYPE<datatype>…………..
Indirectly, using <declaration>like<dataobject>…………..
•Type and like are optional additions to most of the data declaration
statements listed below.
•With the TYPE option, you assign the data type<datatype>directly to the
declared data object.
•With the like option,you assign the data type of another data object<data
object> to the declared data object.
Creating Data Objects and Data Types:
ABAP/4 includes the keywords for creating Data objects and Data types
statically:
The Data statement for creating variables
The Constants statement for creating constants
The Statics statement for creating variables which exist as long
as the program runs, but are
only visible in a procedure
The Tables system for creating table work areas
The Type statement for creating user-defined data types
In the context of Internal Tables, you use the operational statements
APPEND,COLLECT and INSERT to create lines of an Internal Tables
dynamically.
In case of selection screens,you use the additional statements
PARAMETERS SELECT-OPTIONS to create Data objects with special
function
Basic Form of the DATA Statement
•Data Statement
•Syntax
In its basic form, the keyword DATA has the following parameters:
Syntax: Constants<c>[<length>]<type><value>[<decimals>]
Statics Statement
If you want to retain the value of variable beyond the runtime of a
procedure, you define the variable with STATICS Statement in that
procedure.
Syntax: Statics<s>[<Length>]<type>[<Value>][<decimals>]
Tables statement
With the TABLES statement, you can create a data object called a
table work area. A Table work area is a field string which refers to
ABAP/4 dictionary objects.
Syntax: Tables<dbtab>
The Types Statement
Syntax: Type-Pools<name>
Determining the Decimal Places
To determine the number of decimals for a Type P field, you use the
DECIMALS parameter with the DESCRIBE FIELD statement as follows:
•The variable name <f> may be up to 30 characters long. You can use
any alphanumeric characters except those listed below.
- plus sign +
- period .
- comma ,
- colon :
- parentheses ( )
Syntax: WRITE<f>.
This statement outputs the field <f> to the current list in its standard
output format.
•To write a value (literal) or the contents of a source field to a target field,
you use the WRITE TO statement:
Syntax
Example
You can position the output of a write statement on the screen by making
a format specification before the field name as follows:
Syntax
WRITE AT [/][<pos.>][(<len>)] <f>.
Syntax: Write……..<f><option>
Option Purpose
LEFT-JUSTIFIED Output is left-justified.
CENTERED Output is centered.
RIGHT-JUSTIFIED Output is right-justified.
UNDER <g> Output starts directly under the field <g>.
NO-GAP The blank after the field <f> is omitted.
•You can output symbols or R/3 icons on the screen by using the
following syntax:
Syntax
Example:
INCLUDE <LIST>.
SKIP.
The R/3 System includes a useful facility for trying out all the options and
output formats of the write statement and inserting them into your
program.
Syntax:
Edit ===> Insert statement…… in the ABAP/4 Editor
Then select WRITE in the relevant dialogue box.
When you have confirmed your selection with a enter, you see the screen
where you can:
A) determine the output format of an internal field by entering its name or
a literal in the field FLD.
B) generate the WRITE statements for symbols,icons,and checkbox
simply by selecting the appropriate field
C)generate the write statements for components of structures defined in
the data dictionary.
Working with text elements and concept
B)List headings and column headings for page headers of output lists
A)Titles:
You enter the program title when specifying the program attributes. You
may change this title as you wish.You can enter a title of up to 70
characters in the Title field.
Save your changes by choosing save.
B)Headers:
You can create or change the header line for the output list of your
program and the column headers for the different columns in the list..
To create or change the titles in the output screen,select titles and
headers from the ABAP/4 Text Elements screen and choose change.
You can enter a list characters up to 70 characters in the List header field
and column headers up to 255 characters in the four lines of the Column
Header Field.
Save your changes by choosing save.
Selection Texts
+ Addition
- Subtraction
* Multiplication
/ Division
DIV Integer division
MOD Remainder of integer division
** Exponentiation
SUMMARY
10 1 XX 1
20 2 BB 2
30 3 XX 3
40 4 DD 4
50 5 XX 5
Solution
REPORT ZSHU017 .
data: begin of it occurs 3,
f1(2) type n,
f2 type i,
f3(2) type c,
f4 type p,
end of it.
sort it by f1.
Example 1 Solution
do 5 times.
it-f1 = sy-index * 10.
it-f3 = 'XX'.
it-f2 = it-f4 = sy-index.
read table it
with key f1 = it-f1
binary search
transporting no fields.
if sy-subrc <> 0.
insert it index sy-tabix.
endif.
enddo.
loop at it.
write: / it-f1, it-f2, it-f3, it-f4.
endloop.
Example 2
Input string = 20
Output string = 02
The given text is not Palindrome
Example 2 - Solution
Solution
report z10393_10 .
* Variables declaration.
data : v_text1(100) type c.
data : v_text2(100) type c.
data : v_text3(100) type c.
data : v_length type i.
************** **********************************************
* Selection screen
parameters : p_input(100) type c.
*************************************************************
* Start of selection....
v_text1 = p_input.
Example 2 - Solution
* lastletter+last-1letter+.....
concatenate v_text3 v_text2 into v_text3. ...
v_length = v_length - 1.
endwhile.
Example 2 - Solution
* Output....
write :/1 'Input string = ', 20 v_text1.
write :/1 'Reverse string = ', 20 v_text3.
if v_text3 = v_text1.
write :/ 'The given text is palindrome'.
else.
write :/ 'The given text is not a palindrome'.
endif.
Example No. 3
END OF I_MSEG,
BEGIN OF I_MAKT OCCURS 0,
MATNR LIKE MAKT-MATNR,
MAKTX LIKE MAKT-MAKTX,
END OF I_MAKT.
SELECTION-SCREEN BEGIN OF BLOCK B1.
SELECT-OPTIONS: S_MATNR FOR MARA-MATNR,
S_MTART FOR MARA-MTART,
S_MATKL FOR MARA-MATKL.
SELECTION-SCREEN END OF BLOCK B1.
LOOP AT I_MARA.
AT NEW MATNR.
WRITE :/5 'Material Number:',I_MARA-MATNR.
ENDAT.
LOOP AT I_MAKT WHERE MATNR = I_MARA-MATNR.
WRITE : 35 'Desc:', I_MAKT-MAKTX.
ENDLOOP.
SKIP.
WRITE :/5 'MBLNR', 30 'MJAHR', 55 'ZEILE'.
LOOP AT I_MSEG WHERE MATNR = I_MARA-MATNR.
WRITE :/5 I_MSEG-MBLNR , 30 I_MSEG-MJAHR, 55 I_MSEG-ZEILE.
ENDLOOP.
ULINE.
ENDLOOP.
Example 4
REPORT Z31212_LEARN .
*-----------------------------------------------------------------------
*-----------------------------------------------------------------------
Solution-Example 4
Example 5
Name of your report: ZSHU006
ID number :
1 2 3 4 5 99
2 4 6 8 10 6
Solution-Example 5
REPORT ZSHU006 .
data: f1 type i,
begin of s,
c1 type i value 1,
c2 type i value 2,
c3 type i value 3,
c4 type i value 4,
c5 type i value 5,
c6 type i value 6,
end of s.
field-symbols <f>.
write / ''.
Solution - Example 5
write / ''.
do 6 times varying f1 from s-c1 next s-c2.
write f1.
enddo.