Вы находитесь на странице: 1из 75

ABAP/4 Programming

Lesson 1

Introduction
Click here to start
Table of Contents Author: Roger Hayen

Introduction Email: roger.hayen@cmich.edu

Lesson 1 Topics Home Page: Contents

Lesson Objectives

ABAP/4 Programming Language

ABAP/4 Development Workbench

ABAP/4 Development Workbench


Architecture

Reporting and Dialog Programming

ABAP/4 Interfaces

Sample ABAP/4 Program

Running an ABAP/4 Report

Sample ABAP/4 Report

More Complex ABAP/4 Report

Balance Sheet/P+L Report

ABAP/4 Program Objects

Typical Repository Requests

ABAP/4 Repository Information


System

SAP Application Hierarchy


Development Workbench Initial Screen

Object Browser Initial Screen

Object Browser Initial Screen

Object Browser Development Class

ABAP/4 Editor

Editor Settings

Editor in PC Mode

Marking Lines and Setting Markers

Where-used List

Where-used Example

ABAP/4 Syntax

Data Objects

Chain Statements

Comments

Tables and Table Work Areas

SELECT Statement

SELECT with WHERE Clause

System Fields

SHOW SY Fields Screen

WRITE Statement

Format Specifications

Example List Report


Page Header

Text Symbols

Rules for Naming Programs

Program Naming Conventions

Creating an ABAP/4 Program

Creating Program Object

Attributes

Setting Attributes

Writing Program Code

Displaying or Changing a Program

Starting an ABAP/4 Report

Testing the Program

Online Help

Using Online Help

Summary

Summary

Summary

Summary

Quick Check

Quick Check

Quick Check
Lesson Purpose

The purpose of this lesson is to introduce the ABAP/4 programming language and the
ABAP/4 development workbench for creating ABAP/4 programs. Overall, understanding
ABAP/4 programs helps you comprehend processing in the SAP R/3 System.
Lesson Topics

This lesson consists of the following main topics:

 ABAP/4 Development Workbench


 A Sample ABAP/4 Program
 Program Objects
 Object Browser
 ABAP/4 Editor
 ABAP/4 Syntax
• Comments
• SELECT statement
• WRITE statement
 Creating and ABAP/4 Program
Slide
3 of
60

Lesson Objectives

Upon completion of this lesson, you should be able to:

• Describe the purpose and use of an ABAP/4 program


• Describe the use of the ABAP/4 Development Workbench in creating ABAP/4
programs
• Describe the use of the ABAP/4 Repository System
• Use the ABAP/4 Editor in the PC Mode to create or change an ABAP/4 program
• List several of the ABAP/4 programming statements
• Describe the use of the SELECT statement in processing data from a database
table
• Describe the steps in executing an ABAP/4 program
• Create and test an ABAP/4 program
Slide
4 of
60

The ABAP/4 Programming Language

ABAP/4 is a programming language developed by SAP for the interactive development


of application programs.

• ABAP/4 is a fourth-generation programming language that is the backbone of the


R/3 System.
• The language is event-driven with user actions and system events controlling the
execution of the applications.
• You can use the ABAP/4 programming language to create entirely new
client/server applications, as well as extend existing R/3 modules,which is the
most common use of the ABAP/4 language.
Slide
5 of
60

ABAP/4 Development Workbench

The ABAP/4 programming language is the central tool in the Development Workbench
that is integral to the R/3 Basis and available with all the R/3 application modules. In
addition, the Development Workbench consists of a set of tools including the ABAP/4
programming language that support the efficient development of large-scale enterprise
applications across a distributed client/server environment.
Slide
6 of
60

ABAP/4 Development Workbench Architecture

The ABAP/4 Development Workbench provides access to SAP’s development tools


which cover the entire software development cycle.

• The Development Workbench is the environment used to create and edit ABAP/4
programs.
• These tools can be used both for customer-specific developments and
enhancements to R/3 applications supplied by SAP.
• All applications created with the ABAP/4 Development Workbench can run
without further modifications on any platforms, database systems and graphical
user interfaces supported by SAP.

ABAP/4 in the SAP R/3 System is a shared development environment. This means that
all programs reside in the R/3 Repository. Therefore, other programmers are working
with programs that reside in the common storage of the R/3 Repository. You must
exercise care to make sure that your program follows established naming conventions and
are unique within the R/3 System.
Slide
7 of
60

Reporting and Dialog Programming

ABAP/4 programs are divided into the two primary categories of reporting and dialog
programming.

• A report program in ABAP/4 is a program which reads and analyzes data from
database tables without modifying the database. Usually, the result of such a
report program is in the form of a list which is output to the screen or sent to a
printer. This list is a report in the usual sense of the word. Therefore, a report
program is a program which creates a report based on the evaluation of one or
more database tables. In reporting you use the ABAP/4 Open SQL to read data
from the R/3 database. A report consists of a selection screen, on which you
define the dataset you wish to display, and a list, which displays the data itself. In
interactive reporting, the user can navigate from the (basic) list to details lists at
runtime. Typically, reports merely read data from the database. However, it is also
possible to make changes in the database using a report.
• In dialog programming you use the Screen Painter to create screens and program
the sequence in which they appear. You write an ABAP/4 program (ABAP/4
module pool) for your screens. This is a collection of dialog modules which are
called by the flow logic of your screens. You use dialog programs for both
reading and changing database tables.

Slide
8 of
60

ABAP/4 Interfaces
When you use the ABAP/4 data interfaces, a sequential dataset is generated in the source
system. This is then read into the target system by the data transfer program and imported
automatically into the R/3 database using batch input, Call Transaction or direct input.

• ABAP/4 contains statements for the implementation of communication programs


such as Remote Function Call (RFC) and Object Linking and Embedding (OLE).

Slide
9 of
60

Sample ABAP/4 Program

This is a simple report program that displays the letters “SAP” when the user executes or
runs the program in the R/3 System.
The program is stored in the R/3 Repository which makes it available for execution by a
user.

Slide
10 of
60

Running an ABAP/4 Report

You can run or execute an ABAP/4 report in several different ways. One method is
described here, other methods are presented later. This method is one used frequently by
an end user of a report. You start from the main SAP R/3 menu.

To run an ABAP/4 program:

1. Click System on the SAP R/3 Menu bar, point to Services, then click Reporting
to display the ABAP/4 Execute Program screen.
2. Type the program name in the Program text box, e.g. ZADEMO01.
3. Click the Execute button on the Application toolbar to cause the program to be
executed or run. The results of the program execution display on your SAP R/3
screen.

Slide
11 of
60

Sample ABAP/4 Report

This is the report generated on your screen when the ABAP/4 program is executed. Of
course, reports can be more complex than this.
Slide
12 of
60

More Complex ABAP/4 Report

This is a typical user-created report that is generated from data in an R/3 database table
by selecting desired records or rows for display. The fields or columns of the report are
fields from the database table. You can specify any fields from a table in any sequence
that you want them displayed across your listings. Also, this report has column titles or
report headers that identify the data in each column for the user. As you learn about
ABAP/4 programming, you will learn how to create a report like this.
Slide
13 of
60

Balance Sheet/P+L Report

The Balance Sheet/P+L Report (Program RFBIL00) is a standard report of the Financial
Accounting (FI) module of the R/3 System. You should notice the similarities between
this standard report and the previously illustrated Airline Bookings report. This Balance
Sheet/P+L report has a program title, report title, and column headers that identify the
contents displayed in the report. When you write ABAP/4 programs you prepare similar
reports, such as that for Airline Bookings. A significant portion of ABAP/4 programming
is concerned with the development of special purpose reports that list data from the R/3
database files in special arrangements that are NOT provided by the standard reports
included with in the best business practices of the R/3 System application modules. It is
for these additional reporting requirements that organizations desire the creation of
special ABAP/4 report programs.
Slide
14 of
60

ABAP/4 Program Objects

You use the ABAP/4 Development Workbench to create ABAP/4 programs that are one
of the objects in the R/3 System.

• You call objects from the object list generated from the Object Browser initial
screen.
• The object view is a hierarchical structure in which the development class is the
root node.
• Once you have generated an object list, it is maintained automatically and
dynamically by the system.
• You can create, change or display the objects of an ABAP/4 program from the
object list.
• Process Before Output (PBO) modules and Process After Input (PAI) modules are
programs that are used with an ABAP/4 dialog program.
• These objects reside in the R/3 Repository.
Slide
15 of
60

Typical Repository Requests

The ABAP/4 Repository Information System allows you to obtain information about
objects that include tables, fields, and programs in the ABAP/4 Repository. It consists of
a range of reports, each of which realizes a different view in the ABAP/4 Repository and
displays the required information either directly or as a result of other calls.
Slide
16 of
60

ABAP/4 Repository Information System

The Repository Information System displays a tree structure that can be expanded when
looking for objects in the R/3 System. This tree structure provides an alternative method
of opening an existing program in the ABAP/4 Editor. The following are a few typical
requests which you might make to the ABAP/4 Repository Information System with
regard to the ABAP/4 Dictionary:

• Search by object attributes (list all objects of type X with attribute(s) Y).
• Where-used lists (lists all objects of type X which use object of type Y or list all
ABAP/4 programs which use particular tables).
• Information about the relationships between tables (foreign key relationships).
• Modified objects (list all objects of type X changed by user Y or, for example,
which were changed after a particular date).
• Display data (display all data records in table X with attribute Y).
Slide
17 of
60

SAP Application Hierarchy

• The SAP Application Hierarchy displays an overview of the applications in tree


form, sub-divided into work areas.
• The lowest level of the hierarchy is the development class. You can branch
directly from the Object Browser from the Application Hierarchy via this level.
• Customer projects can be displayed in a separate application hierarchy.
• Where-used allows you to display programs that use an object, such as a table or
field.
Slide
18 of
60

Development Workbench Initial Screen

From the Development Workbench Initial Screen you can access a program object using
either the Object Browser button or the ABAP/4 Editor button. If you know the name of
the program you want to edit, then the ABAP/4 Editor is usually the most convenient
method of accessing a program.
Slide
19 of
60

Object Browser Initial Screen

The Object Browser is a navigation tool for accessing R/3 Repository objects. This
browser assists in drilling down in the object hierarchy. Development classes are
categories of SAP objects that you can access. Before you create a program, you specify
its development class. A common transport route is defined for all objects within a
development class. Customer-specific development classes begin with Y or Z.

• If the development class does not exist, you will be prompted to create it. If the
development class does exist, the next screen will display the development object
list for the specified development class.
• Select the development class by positioning the cursor on the development class
name, and pressing the Create icon on the application tool bar. A pop-up window
will be displayed with classifications of development objects. Select the radio
button in front of the Program objects classification.
• A pop-up window for program objects will be displayed. Enter the program name,
beginning with a Y or Z. Verify that the radio button in front of the Program entry
in the list is selected , and press the Create icon at the bottom of the window.
• A dialog box then appears, asking you if you want to create the program with a
TOP include. If you choose to do so, the system generates an include program in
which you can store all declarations (only if you keep to the naming conventions,
e.g. program names in module pools begin SAPM...)
• If you are creating a test object which you do not want to be transported, choose
Local object. The development class is then set to $TMP.
• You then specify the name of the new program. To proceed, click the radio button
Program and choose Display. You will then continue as described above

Slide
20 of
60

Object Browser Initial Screen

A list of the objects included in the Development class can be displayed.


To display a list of objects for the specified Development class:

1. Type the development class in the Development class text box.


2. Click the Display button to display a tree structure of the objects.

Slide
21 of
60

Object Browser Development Class

Development classes support the distributed development of large applications. A set of


development objects such as programs make up the development class. In practice, within
a development class, objects of an application area are strongly related to each other, but
those objects are only loosely connected to the rest of the system.

Objects within a development class can be displayed in the Object Browser by entering
the name of the class. The object categories and names are displayed in a tree structure
that can be expanded or collapsed as you review the list of available objects.
By expanding the Programs object category, a list of the programs belonging to the
development class is displayed. You can then click the name of a desired program to
select it.

Slide
22 of
60

ABAP/4 Editor

The ABAP/4 Editor can be used in any one of three available modes:

PC Mode
Provides word-processing style copy, cut, and paste commands.
PC Mode with Line Numbers
Provides word-processing style copy, cut, and paste commands but with the
addition of line numbers. This is the default mode.
Command Mode
Supplies the same functionality associated with the R/2 version of the SAP
enterprise software.

• The various sample programs illustrated in these lessons assume that you are in
the Editor in PC Mode with Line Numbering.
• In the PC Mode, you can execute all functions with the mouse.
• Once you have accessed the ABAP/4 Editor, you can switch between editor
modes by choosing Settings Editor mode.

Slide
23 of
60

Editor Settings

The Editor Settings dialog box allows you to select the desired edit mode. The PC mode
with line numbering is a frequently used editor mode.
Slide
24 of
60

Understanding Editor Basics

You use your computer’s keyboard to enter text into the Editor. To enter text, simply
place your cursor on a line and begin typing. The Editor does NOT automatically wrap
your text to the next line. If you press the Enter key at the end of a line, the Editor inserts
a new line and places your cursor at the beginning of that line. Operations like Cut, Copy,
and Concatenate all work on individual lines or blocks of lines. You can also use the
Windows clipboard to cut and paste lines.

Editor in PC Mode

In PC mode (with or without line numbering), the program editor is similar to other PC
editors.

• You use pushbuttons (or function keys) and menu functions rather than line or
header commands.
• If you right-click (press the right mouse button) with the pointer located anywhere
in the edit area, the system displays a pop-up window with a list of all available
editor commands.
• Pressing the Enter key splits the line at the current cursor position. Pressing Enter
at the beginning or end of a line inserts a new line.
• The F8 function key is a convenient method for executing a program displayed in
the Editor. Right-click anywhere on the edit area of the screen for the pop-up list
of function keys you can use with the editor.

Slide
25 of
60

Marking Lines and Setting Markers


Markers provide a convenient method of accessing a particular line within a program.
This is most useful with larger programs where you want to quickly locate a particular
section of the program in the editor.

 The Mark function allows you to mark lines in the editor. You click the line to
select it, then you can mark it for future reference.
• In PC mode, use the Mark line pushbutton when displaying (that is the
Display mode), or the menu path Edit Edit line Mark line when
editing (that is the Change mode). In command mode, use the Mark
pushbutton.
 If you choose Markers, you get a list of all the lines you have marked and can
then branch directly to one of these.
• In PC mode, use the Markers pushbutton or the menu path Goto
Markers. In command mode, use the Markers pushbutton.
• Each object list you pass is automatically logged as a temporary marker.
However, you can also specify that you want an object list to be marked as
a permanent marker (Save temp. marker).
 When you log off, temporary markers are deleted, but you can keep them by
specifying them as permanent markers.
 In the Object Browser, you can define the place where you want to enter the
ABAP/4 Development Workbench as an initial marker. To do this, choose
Settings Define init.position.
Slide
26 of
60

Where-used List

You can request a where-used list for any object where navigation is supported. This can
apply just to your current program or may be system-wide.

• To obtain a where-used list for one program from the ABAP/4 Editor, choose
Utilities Where-used list.
• You can get a system-wide list from the Object Browser by choosing the Single
objects radio button and then Edit, followed by Where-used list on the next
screen.
• The hit list is integrated into the navigation process.

A where-used list is always up-to-date because the program index is updated when you
save the source code.
Slide
27 of
60

Where-used Example

You can locate all the references to a table, such as SBOOK, within a program. This is
useful in examining the use of this object within the program.
Slide
28 of
60

ABAP/4 Syntax

An ABAP/4 program consists of individual statements that are English-like ( or natural


language-like) so they are easily readable by an individual interested in the application.

An ABAP/4 program consists of a series of statements that describe program actions.

• Each statement must end with a period.


• The first word of a statement is known as the keyword.
• Words are separated from each other by at least one blank.
• Statements can be indented.
• Statements can extend over several lines.

PROGRAM ZA01TEST.

WRITE 'First Program'.


This example contains two statements, one on each line. The keywords are PROGRAM
and WRITE. The program displays the output (known as a list) on the screen. In this
case, the list consists of the line "First Program".

Slide
29 of
60

Data Objects

The DATA statement is one method used to define a Data Object that is used with an
ABAP/4 program.

• A field in ABAP/4 is a variable, which cannot be decomposed into smaller parts.


It is the smallest unit of meaningful data in the R/3 System.
• The DATA statement defines a variable or field that is used by a program at
runtime and requires memory space during program execution.
• A variable is used to store and reference data under a certain name and in a certain
format.
• A variable is an internal data object that is created for use in one particular
program. Internal data objects have no validity outside their program.
• Data objects are described in more detail in a subsequent lesson.

Slide
30 of
60

Chain Statements

You can concatenate several consecutive statements with an identical first part as a chain
statement.

• When doing this, conclude the identical first part with a colon.
• After the colon, separate each concatenated part from the next with a comma.
• Blanks can appear before and after the separators (colon, comma or period).

For additional information see:

Concatenating Similar Statements

Concatenating Similar Statements

The ABAP/4 programming language allows you to concatenate consecutive statements


with an identical first part into a chain statement.

To concatenate a sequence of separate statements, write the identical part only once and
place a colon (:) after it. After the colon, list the remaining parts of the statements by
separating each part with a comma (,). Ensure that you place a period (.) after the last part
to inform the system where the chain ends.

Statement sequence:

WRITE SPFLI-CITYFROM.
WRITE SPFLI-CITYTO.
WRITE SPFLI-AIRPTO.

Chain statement:

WRITE: SPFLI-CITYFROM, SPFLI-CITYTO, SPFLI-AIRPTO.

In the chain, a colon separates the beginning of the statement from the variable parts. You
can insert as many spaces as you like before and after the colon (or comma).

For example, you could also write the same statement as follows:

WRITE: SPFLI-CITYFROM,
SPFLI-CITYTO,
SPFLI-AIRPTO.

In a chain statement, the first part (before the colon) is not limited to the keyword of the
statements.
Statement sequence:

SUM = SUM + 1.
SUM = SUM + 2.
SUM = SUM + 3.
SUM = SUM + 4.

Chain statement:

SUM = SUM + : 1, 2, 3, 4.

Slide
31 of
60

Comments

You can insert comments into a program in two ways:


• An asterisk (*) in column 1 flags the whole line as a comment.
• A quotation mark (") within a line flags the remainder of the line as a comment.

For additional information see:

Comment Alternatives

Comment Alternatives

You can insert comment lines anywhere in a program. There are two ways to indicate
comments in a program:

• If you want the entire line to be a comment, enter an asterisk (*) at the beginning
of the line.
• If you want part of a line to be a comment, enter a double quotation mark (")
before the comment. The system interprets comments indicated by double
quotation marks as spaces.

PROGRAM ZA01TEST.
* The following line contains a WRITE statement
WRITE 'First Program'. " Output on List

The second line of this program is a comment which is not executed. The comment is
indicated by an asterisk (*) at the beginning of the line.

On the third line, everything after the double quotation mark (") is a comment and is not
executed.

The rest of the program consists of executable statements with the keywords PROGRAM
and WRITE.

Program Documentation Comments

Program Documentation Comments

Comments are text elements which you can write between the statements of your
ABAP/4 program to explain its purpose to a reader. Comments are flagged by special
characters which cause the system to ignore them. You should use comments to
document your program internally. Comments help other users to understand or change
the program.
************************************************
* PROGRAM SAPMTZST *
* CREATED BY HERBIE SMITH 16/27/1998 *
* LAST CHANGE BY CARLA DELITE, 10/04/1999 *
* PURPOSE: DEMONSTRATION *
************************************************
PROGRAM ZA10TEST.
************************************************
* DECLARATION PART *
************************************************
DATA ......
......
************************************************
* OPERATION PART *
************************************************
......

All lines beginning with an asterisk (*) are comments and are ignored by the system.
Slide
32 of
60

Tables and Table Work Areas

The TABLES < name> statement declares an ABAP/4 Dictionary table in the ABAP/4
program and allocates a table work area with the same structure of as that of < name>.

• In the above example, the SELECT statement reads table SBOOK line by line and
places each line read in the table work area SBOOK.
• ENDSELECT is used to indicate the end point of a block of statements that are
used with the SELECT statement.
• The statements that are included between the SELECT and ENDSELECT are
executed for EACH record read from the specified table, e.g. SBOOK in this
example.
Slide
33 of
60

SELECT Statement

You use the SELECT statement to read the entries in a database table.

• You can display the table structure (field names, field characteristics) via the ‘i’
pushbutton. Also, if you double-click a table name in the Editor, the table
structure displays with a listing of the fields in the table.
• You address the fields of a table with the table name followed by the field name.
• This example contains only the simple basic form of the SELECT statement.
More comprehensive forms of the statement are examined in subsequent lessons.
• The SBOOK table is a table of flight bookings that is used in exploring ABAP/4
programming in these lessons and is included in the IDES training data
Slide
34 of
60

SELECT with WHERE Clause

You can use the WHERE clause to restrict the number of table entries to be read. The
above example reads only those table entries where the table field SBOOK-CARRID has
the value LH.

• The field specified in the WHERE clause to the left of the comparison operator
must be a field of the table to be read. In this context, it must be addressed
without the table name.
• If no table entry is found to satisfy the WHERE condition, the return code of the
system field SY-SUBRC is set to a value other than zero.

The SELECT statement is examined in more detail in Lesson 4.


Slide
35 of
60

System Fields

There is a range of system fields within ABAP/4. You can display a list of them with
their definitions by entering the header command SHOW SY. If you are in PC mode,
press the pushbutton and enter table structure SY.

• In programs, you address system fields by entering SY-<field name>.


• The SY-SUBRC or Return Code field is used frequently when selecting records
from a table because a non-zero value for this field indicates that NO records were
found in the specified table. This use of the SY-SUBRC field is illustrated
previously with the SELECT with WHERE clause.
Slide
36 of
60

SHOW SY Fields Screen

The SHOW SY fields screen list the available system fields and provides a description
for each field. About 200 system fields are used with the R/3 System.
Slide
37 of
60

WRITE Statement

WRITE outputs the contents of a field or constant in the format appropriate for the data
object.

• Consecutive WRITE statements output data on the same line. If there is no more
space on one line, the output continues on the next line. (Note: To increase the
line size beyond the size of the displayed window, use the LINE-SIZE parameter
of the REPORT keyword to extend the line up to 255 characters.
• SKIP will act as a line feed (one line). SKIP n, where n is a numeric value, will
produce n-1 blank lines and place the next line of output on the nth line. For
example, SKIP 3 will produce 2 blank lines and output the next line of output on
the 3rd line following the last produced line of output.
• The slash (/) is used within the WRITE statement to advance to a new line.
• The ABAP/4 processor generates a standard header for each line. This consists of
the list title, page number and an underline.

For additional information see:


WRITE Screen Output

WRITE Screen Output

The basic ABAP/4 statement for outputting data on the screen is WRITE.

Syntax

WRITE <f>.

This statement outputs the field <f> to the current list in its standard output format. By
default, the list is displayed on the screen.

The field <f> can be

• any data object (see Data Objects )


• a field symbol or formal parameter (see Working with Field Symbols)
• a text symbol (see Working with Text Elements)

You can print the current output list directly from the output screen by choosing Print.

If a selection screen is defined for the program (see Working with Selection Screens),
you can choose Execute and print on the selection screen. Then, the list is not output to
the screen, but sent directly to a printer.

PROGRAM ZA01ZTST.
WRITE 'Hello, here I am!'.

When you start this program, the system leaves the current screen (this may be the
ABAP/4 Editor: Initial Screen) and branches to the output screen:

ZA01ZTST 1

Hello, here I am!

The output screen has the same name as the title of the program specified in the program
attributes (see Specifying the Program Attributes). The first line on the screen contains
the list header. By default, the list header is the same as the title of the program.
However, you can maintain the list header independently from the program title outside
the actual program. For further information about this, see Working with Text Elements.
The current page number (1) appears on the right. The list header is followed by one
horizontal line and then the output is displayed.
On the screen, the output is normally left-justified. If you use several WRITE statements,
the output fields are displayed one after the other, each separated by one column (i.e. one
blank). If there is not enough space for an output field on the current line, a new line is
started.

PROGRAM ZA01TEST.
TABLES SPFLI.
.............
WRITE: 'COMPANY: ', SPFLI-CARRID.

Note the use of the colon and the commas (see Syntax Structure).

The program fragment in this example outputs two fields, the literal 'COMPANY: ' and
the component CARRID of the table work area SPFLI, to the screen:

COMPANY: AA

Positioning WRITE Output on the Screen

Positioning WRITE Output on the Screen

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>.

where

• the slash '/' denotes a new line,


• <pos> is a number or variable up to three digits long denoting the position on the
screen,
• <len> is a number or variable of up to three digits long denoting the output length.

If the format specification contains only direct values (i.e. no variables), you can omit the
keyword AT.

WRITE 'First line.'.


WRITE 'Still first line.'
WRITE / 'Second line.'
WRITE /13 'Third line.'

This generates the following output on the screen:

First Line. Still first line.


Second line.
Third line.

If you specify a certain position <pos>, the field is always output in that position
regardless of whether or not there is enough space available or whether other fields are
overwritten.

DATA: LEN TYPE I VALUE 10,


POS TYPE I VALUE 11,
TEXT(10) VALUE '1234567890'
WRITE 'The text ------------ appears in the text.'.
WRITE AT POS(LEN) TEXT.

This produces the following output on the screen:

The text -1234567890- appears in the text.

If the output length <len> is too short, fewer characters are displayed. Numeric fields are
truncated on the left and prefixed with an asterisk (*). All other fields are truncated on the
right, but no indication is given that the field is shorter.

DATA: NUMBER TYPE I VALUE 1234567890,


TEXT(10) VALUE 'abcdefghij'.
WRITE: (5) NUMBER, /(5) TEXT.

This produces the following output:

*7890
abcde
Slide
38 of
60

Format Specifications

You can assign a format to an output value. This allows you to specify a new line, the
starting column and the output length.

• The slash (/) causes a line advance or line feed for displaying output on the next
line.
• ULINE generates an underline.
• The formatting option UNDER allows you to display a value flush underneath a
previously output value (the new line must be programmed explicitly).

For additional information see:

Formatting Options
Formatting Options

You can use various formatting options with the WRITE statement.

Syntax

WRITE .... <f> <option>.

Formatting options for all data types

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.
USING EDIT MASK <m> Specifies a format template <m>.
USING NO EDIT MASK Deactivates a format template specified in the
ABAP/4 Dictionary.
NO-ZERO If a field contains only zeros, these are replaced by
blanks. For type C and N fields, leading zeros are
replaced automatically.

Formatting options for numeric fields

Option Purpose
NO-SIGN The leading sign is not output.
<d> defines the number of digits after the decimal
DECIMALS <d>
point.
EXPONENT <e> In type F fields, the exponent is defined in <e>.
Type P fields are multiplied by 10**(-r) and then
ROUND <r>
rounded.
CURRENCY <c> Format according to currency <c> in table TCURX.
UNIT <u> The number of decimal places is fixed according to
the unit <u> specified in table T006 for type P
fields.

Formatting options for date fields

Option Purpose
DD/MM/YY Separators as defined in user's master record
MM/DD/YY Separators as defined in user's master record
DD/MM/YYYY Separators as defined in user's master record
MM/DD/YYYY Separators as defined in user's master record
DDMMYY No separators.
MMDDYY No separators.
YYMMDD No separators.

For more detailed information on formatting options and the exclusion principles
within some of these options, see the keyword documentation of the WRITE
statement.

Below are some examples of formatting options. The decimal character and
thousands separators (period or comma) of numeric fields are defined in the user's
master record

ABAP/4 code Screen output


DATA: G(5) VALUE 'Hello',
F(5) VALUE 'Dolly'. Hello Dolly
WRITE: G, F. Hello
WRITE: /10 G, / F UNDER G. Dolly

WRITE: / G NO-GAP, F. HelloDolly


DATA TIME TYPE T VALUE '154633'.
WRITE: TIME, 154633
/(8) TIME USING EDIT MASK '__:__:__'. 15:46:33
WRITE: '000123', 000123
/ '000123' NO-ZERO. 123
DATA FLOAT TYPE F VALUE '123456789.0'.
WRITE FLOAT EXPONENT 3. 123456.789E+03

DATA PACK TYPE P VALUE '123.456'


DECIMALS 3. 123.46
WRITE PACK DECIMALS 2.
WRITE: / PACK ROUND -2, 12,345.600
/ PACK ROUND -1, 1,234.560
/ PACK ROUND 1,
/ PACK ROUND 2. 12.346
1.235
WRITE: SY-DATUM, 06/27/1998
/ SY-DATUM YYMMDD. 980627

Apart from the formatting options shown in the above tables, you can also use the
formatting options of the FORMAT statement. These options allow you to specify
the intensity and color of your output.

Lines and Blank Lines on the Output Screen

Lines and Blank Lines on the Output Screen

Horizontal lines

You can generate horizontal lines on the output screen by using the following syntax:

Syntax

ULINE [AT [/][<pos>][(<len>)]].

This is equivalent to

WRITE [AT [/][<pos>][(<len>)]] SY-ULINE.

The format specifications after AT are exactly the same as the format specifications
described for the WRITE statement in Positioning WRITE Output on the Screen.

If there are no format specifications, the system starts a new line and fills it with a
horizontal line. Otherwise, horizontal lines are output as specified.

Another way of generating horizontal lines is to type the appropriate number of hyphens
in a WRITE statement as follows:
WRITE [AT [/][<pos>][(<len>)]] '-----...'.

Vertical lines

You generate vertical lines on the output screen by using the following syntax:

Syntax

WRITE [AT [/][<pos>]] SY-VLINE.

or

WRITE [AT [/][<pos>]] '|'.

Blank lines

You can generate blank lines on the screen by using the following syntax:

Syntax

SKIP [<n>].

Starting on the current line, this statement generates <n> blank lines on the output screen.
If no value is specified for <n>, one blank line is output.

To position the output on a specific line on the screen use:

Syntax

SKIP TO LINE <n>.

This statement allows you to move the output position upwards or downwards.
Slide
39 of
60

Example List Report

When the program is executed, a screen with the selection options appears for specifying
the beginning and ending values for the matchcodes for the S_CARRIER field. After
these values are entered the Execute button is used to continue processing and requested
list is generated to the screen. You will learn bout the selection option feature in
subsequent lessons.

Notice how this example list report contains a report title, a two-line report header, and
specified system fields.
Slide
40 of
60

Page Header

You can maintain a list title and up to four column headers for your report list. You do
this from the ABAP/4 Editor menu for a particular program by choosing Goto Text
Elements Title and Headers .

• Another way of doing this is to select System List List header from the
created list itself. This has the advantage that the list is displayed on the screen,
making it easier for you to position the elements, especially the column headers.
• The next time you run the report, the header lines will appear automatically in
your list.
• If you have not specified a list title, the report title is used instead.
• You can translate the texts into other languages. To do this, from the ABAP/4
Development Workbench menu by choosing Utilities Translation.
Slide
41 of
60

Text Symbols

Text symbols are text constant specified and maintained outside a program. One of the
advantages of using text symbols is that the string is no longer hard-coded in the
program. This means that you can maintain the text without having to access the source
code. Such a procedure is particularly significant for software which exists in several
languages.

Instead of writing text directly in program code, you can use text symbols. Text
represented by text symbols is stored in the Repository and is always displayed in the
language of the user. The primary benefit of text symbols is that they can be translated
without affecting the source code of a program.

Text symbols are used instead of text literals in programs because the programs are then
independent of any particular language and remain easy to maintain.
• Each text symbol must have a three-character ID.
• There are two ways in which you can address text symbols in a program:
 TEXT-xxx (where xxx is a three-character string)
 'string' (xxx).
• To maintain text symbols, from the ABAP/4 Editor menu for a particular
program, choose Goto Text elements Text symbols
• You can translate texts into other languages (From the ABAP/4 Development
Workbench menu: Utilities Translation).

Slide
42 of
60

Rules for Naming Programs


It is best to use only letters and numbers in naming programs.

The special characters may cause problems, because they are wildcard symbols for SQL
statements. SAP recommends that you do NOT use them in program names.

Slide
43 of
60

Program Naming Conventions

Y and Z are used to identify used-defined programs in the SAP R/3 System.

• Report programs begin with a Y or a Z.


• Transaction or dialog programs begin with SAPMY or SAPMZ.
Slide
44 of
60

Creating an ABAP/4 Program

You create an ABAP/4 program by first creating the program object in the Repository
and specifying the attributes of the program including its name. Then you actually enter
the program code using the ABAP/4 editor. Finally, you test the program to make sure
that it works correctly.
Slide
45 of
60

Create the Program Object

The following description includes the steps for creating a program starting from the
main SAP R/3 menu:

To create the program object:

1. Click Tools on the SAP R/3 Menu bar, then click ABAP/4 Workbench to
display the ABAP/4 Development Workbench initial screen.
2. Click the ABAP/4 Editor button on the Application toolbar to display the Editor
Initial Screen for entering the program name.
3. Type the program name in the Program text box, then click the Create button. Be
sure to follow the rules for naming a program.
4. Type a program title in the Title text box. This will appear in the Windows Title
bar when the program is executed.
5. Type 1 in the Type text box in the Attributes section.
6. Type S in the Application text box in the Attributes section.
7. Click the Save button on the Standard toolbar to save the program’s attributes.
8. Click the Source Code button on the Application toolbar to open the program in
the ABAP/4 Editor for entering your program code.

The navigation alternatives of the ABAP/4 Development Workbench provide many other
options for creating an ABAP/4 program including creating a program from the Object
Browser.

Slide
46 of
60

Attributes

When you create an ABAP/4 program, you must first specify the program attributes.
Program attributes determine to which application (FI, CO, SD, etc.) a program belongs
and the logical database to which it is linked. You must enter the program attributes so
the R/3 System can process the program correctly.

• The attributes Type, Application and Development class are used to manage
ABAP/4 programs.
• If you are in the reporting or ABAP/4 development function, you can generate a
directory of ABAP/4 programs stored in the system. If you are in the reporting
function, only ABAP/4 programs with type ‘1’ (reporting) are listed. You can
also select the part of the directory you want to display.
• To generate an overview of programs in the system, select:
o From the ABAP/4 Development Workbench:
 ABAP/4 Editor, Utilities Find program
o From reporting:
 Choose System Services Reporting Utilities
Find program

Slide
47 of
60

Setting Attributes

You set the program’s attributes by typing the desired value or selecting it from a list.
When you select the Type or Application field, then a list arrow displays. By clicking the
list arrow or pressing the F4 function key, an associated list of values display in a dialog
box for your selection. Recall, that you can double-click the desired value in the dialog
box to select it and return to the Attributes screen.

Slide
48 of
60

Writing Program Code

Type the statements that comprise the program code using the ABAP/4 editor.

Check the syntax by clicking the Check button. R/3 then scans the program code for any
syntax errors and incompatibilities. If an error is detected, a message appears to report
this and, if possible, proposes a solution or correction. The cursor is placed in the
appropriate position for you to correct the first error in the program.

Click the Save button to save the code in the R/3 Repository. The source text is stored in
the program library.

Slide
49 of
60

Displaying or Changing a Program

In the PC Editor, you can display a program so it can only be viewed and no changes are
permitted, or you can allow changes to be made to the program in the editor. The Display
or Change button allows you to toggle between only viewing the program and permitting
changes to be made to the program. The dimmed background indicates the program is
displayed for viewing only, whereas a program that can be changed usually appears with
each one of program code on a white colored background.
The display mode is useful when you want to examine a program, but do not desire to
make any changes to it. This is the situation that occurs if you are examining a program
that is provided by SAP as a best business practice of the R/3 System. In fact, you are
prompted for an access key in order to open an SAP provided program in the Change
mode, whereas you can readily view the program in the Display mode.

Slide
50 of
60

Starting an ABAP/4 Report

The execution of an ABAP/4 Report Program can be started using several different menu
selection alternatives. Some methods are more appropriate for end users while others
better serve the needs of developers.
 End users can start reports by selecting System Services Reporting.
(This was illustrated previously in this lesson.) The programmer can also assign a
transaction code to a report, which the end user can then call via a menu.
 Developers can start ABAP/4 programs directly from the Editor by pressing the
F8 function key or by choosing Program Execute. (This is the method used
frequently in testing ABAP/4 programs.) Also, in the Object Browser you can run
a program by selecting its name and clicking the Test/Execute icon

Slide
51 of
60

Testing the Program

You test the program to check whether the code works correctly. The R/3 System
executes the program and produces the desired list.
To test the program by running it:

1. Press the F8 function key to execute the program. The result displays as a listing
in a new screen.
2. Click the Exit button on the Standard toolbar to return to the ABAP/4 Editor with
the program code you executed displayed.

If the desired list is not produced, then you need to use the Editor to make changes to the
logic in your program.

Slide
52 of
60

Online Help - Programming Language


This R/3 Extended Help provides details on the ABAP/4 programming language.

• ABAP/4 Editor - describes the details of using this editor.


• Function Library - describes the use of the ABAP/4 Development Workbench's
Function Library to manage existing library modules, develop new function
modules, and test modules in a built-in test environment.
• ABAP/4 User’s Guide - describes the components of the ABAP/4 programming
language including the syntax of the various ABAP/4 statements.
• Online Debugging - describes the use of the ABAP/4 Debugger to stop a program
during runtime and examine the flow and results of each statement during
execution. Stepping through a program with the Debugger helps you to detect and
correct errors in your code.

Slide
53 of
60
Using Online Help

The R/3 Extended Help feature allows you to lookup the syntax of the various ABAP/4
programming language statements.

The contents of the ABAP/4 User’s Guide is organized by the actions you would like to
carryout in your program, such as “Reading Data from Database Tables,” rather than as a
listing of the available statements of the ABAP/4 programming language.

As you create ABAP/4 programs you should use this online ABAP/4 User’s Guide as a
source of information on performing various programming activities.

Вам также может понравиться