Sie sind auf Seite 1von 30

Chapter 04 ABAP/4 Data and Statements

Data declaration
Type declaration
Value assignm ents
String operations
Field sym bols
Control statem ents

SAP AG
Chapter 04 Objectives

ABAP/4 data and types and how to declare them


How to assign values to a field
How to use sub-fields and field symbols
String operations and date calculations
How to declare fields with the PARAMETERS
statement
How the program flow is affected by control
statements

SAP AG
ABAP/4 Data Types and their Attributes
Data type M eaning Initial value Standard Permitted
length length

P packed number 0 8 1-16

I integer 0 * -

F floating point no. 0.000... * -

N numeric text 00...0 1 1-max.

C text blank 1 1-max.

D date (YYYYMMDD) 00000000 8 8


T time (HHMMSS) 000000 6 6

X hexadecimal X'00' 1 1-max.

* machine-dependent max.: ca. 64 KB

SAP AG

With packed fields, two digits are stored in one byte. The other half byte contains the sign.
A type I fields is created as a binary number with a guaranteed value range from -2147483648 to
+2147483647.
Recommended uses for numeric data types:
I: integer values (e.g. counters)
P: for commercial problems (decimal numbers, large value ranges)
F: for mathematical problems (large number area, double accuracy)
The entire data area of an ABAP/4 program is restricted to 64 KB.
Constants

Number literal

715, -431
Sequence of digits, negative numbers
Text literal with a preceding sign

'ABC', 'This is a text' Type Constant


Sequence of up to 255 characters
enclosed in quotation marks
P 579, -713, '2.17'
I 917, -882
F '25.873', '1.213E15', '17E-6', '0.556E7'
N '778', '87931'
C 'AXZ', 'B123', 'AB"CD'
D '19920221'
T '105743'
X '0FFF'

SAP AG

ABAP/4 recognizes two types of constants:


There are number literals for the data types P and I, and text literals for the data types C and N. Other
data types have no type-specific constants. For these, you should always use a text literal.

Example:

If you want to assign a fraction to a type P field (defined with decimal places), you must specify it as
a text literal (e.g. '2.17').
Internally, ABAP/4 generates a value which is correct for each type (see also Type Adjustment later in
this chapter).
If a text literal itself contains a quotation mark, you must enter the quotation mark twice.
You can create constants with the ABAP/4 key word CONSTANTS. After the VALUE option, you
assign a value to a constant and it can then no longer be changed (see also the online documentation
for CONSTANTS).
ABAP/4 Types

Data types:
Pure type description
Do not occupy their own mem ory space
Characterize the technical attributes of all data
objects that have a particular type
Either "built-in" or defined by TYPES

Data objects:
Concrete representation of types
Each data object has a particular type and occupies its own
m emory space
Declared with a data defining statement:
statically declared
dynamically generated

SAP AG

In ABAP/4, the type information belonging to a data object is always inseparably linked to the data
object.
Data types and data objects can always be clearly distinguished in ABAP/4.
Since data types and data objects each have their own naming space, you can use the same identifier
as the name of a data type and the name of a data object.
The TYPES statement allows you to create user-defined data types which you can also use when
assigning types to formal parameters and field symbols.
ABAP/4 Types
Data types
T1 types T2(5) types
type I. type P.

Data objects

DATA D3
type T1.
VALUE 123. DATA D5
DATA D1
type T2.
type T1.
VALUE '1.23'.

DATA D2 DATA D6
type T1. DATA D4 type T2.
type T1.
VALUE 999.

SAP AG

Apart from defining data types directly in the ABAP/4 program, you can also specify them as TYPE-
POOLs in the ABAP/4 Dictionary.
These types are declared by the key word TYPE-POOLS <type pool>, where <type pool> is the name
of the pool (no more than 5 characters) in the ABAP/4 program.

You can only maintain TYPE-POOLS via the ABAP/4 Dictionary.

The names of these types must begin with the name of the type group followed by an underscore.
Data Declaration
DATA: COUNTER TYPE I ,
NAME1(25) VALUE 'SAP',
START_DATE TYPE D,
FIRST_LINE TYPE I
VALUE 1,
SUM1(5) TYPE P DECIMALS 2.

Rules for assigning field names


A name can consist of up to 30 characters
(incl. letters, digits and special characters).
The following characters are not allowed:
( ) + . , :

SPACE is a predefined field.

In an ABAP/4 statem ent, you cannot use the


same nam e for a param eter and a field.

SAP AG

Naming recommendations:

- Always use a letter as the first character. The remaining characters can be letters and/or digits.
- The only special character you should use is the underline (when specifying names which consist of
more
than one word).
If no type is specified, the field is treated as type C.
If no length is specified, the standard length for the type applies.
You can use the VALUE parameter to determine a start value for a field. By default, each field is set
to an appropriate initial value for the type. Constants are not allowed here.
You can use the DECIMALS option to specify the number of decimal places (this only makes sense
with type P where up to 14 decimal places are possible).
Type Declaration
TYPES: MYTYPE TYPE I,
NAME1(25),
DATE1 LIKE SY-DATUM,
SUM_FIELD TYPE P DECIMALS 2.

Rules for assigning type names


A name can consist of up to 30 characters
(incl. letters, digits and special characters)
The following characters are not allowed:
( ) + . , : - < >

A nam e cannot cannot consist of only digits.

Recom mendation: Always use a letter as the


first character and join individual words in a
name with an underline.

SAP AG

The naming conventions are the same as for Data Declaration.


Declaring Fields with Similar Attributes

TYPES: NAMETYPE1 LIKE SCUSTOM-NAME,


NAMETYPE2(25).
DATA: NAME1 TYPE NAMETYPE1,
NAME2 TYPE NAMETYPE2,
NAME3 LIKE NAME1.

CLEAR <field>.
initializes a field with
an appropriate value
for the type.

SAP AG

The LIKE parameter allows you to declare fields which have the same attributes as a previously
defined field.
The reference field can be both an internal field and a database field.
The LIKE parameter helps you to create programs which are easy to modify because you then only
have to change the data attributes in one place.
You can also use the LIKE option when defining types with the TYPES statement. In the above
example, the type NAMETYPE is defined by the type of the field NAME2. NAME2 can be a
database field and an internal field.
The CLEAR statement sets a field to an initial value appropriate for its type. CLEAR does not store
any value specified by VALUE.
Type-specific Output

Type Standard output length Output

P 2* Ig right-justified

I (*) right-justified

F 22 right-justified

N Ig left-justified

C Ig left-justified

D 8 left-justified
T 6 left-justified

X 2 *Ig left-justified

lg = length of field (in bytes)


(*) depends on largest number which can be displayed

SAP AG

If no length is specified for an output field, the value is displayed in its type-specific length.
If the output length specified in the output format is greater than needed, the value is displayed left-
justified or right-justified, according to the type.
When displaying fields of type P and I, leading zeros are converted to blanks and thousands separators
are set.
If the output length specified when displaying numeric values (data types P, I and F) is too shortt, the
value is displayed in truncated form with a preceding asterisk.
Value Assignment

REPORT RSAAA04A.

DATA: NAME1(25),
SORT_NAME(10),
FIRST_LINE(4) TYPE N,
NEXT_LINE TYPE I.
.
.
.
MOVE 'SAP' TO NAME1.
MOVE 5 TO FIRST_LINE.
COMPUTE NEXT_LINE = FIRST_LINE + 1.
SORT_NAME = NAME1.
ADD 1 TO NEXT_LINE.
.
.
.

SAP AG

There are several ABAP/4 statements which assign values to fields. The most important of these are
MOVE and COMPUTE. COMPUTE is optional.
If there is a type or length mismatch between the field and the assigned value, the system performs an
automatic adjustment.
For simple arithmetic operations, you can also use the ADD, SUBTRACT, MULTIPLY and DIVIDE
statements.
Type Adjustment (Examples)

Source field Target field

Type Length Value Type Length Value

C 1 A C 4 A____

C 4 ABCD C 2 AB
C 7 -47110 P 4 47110-
P 3 12345- C 7 12345-

SAP AG

Type C fields are stored left-justified and, if necessary, are padded with blanks on the right or
truncated.
When assigning a type C field to a type P field, only digits, (plus and minus) signs and decimal points
are allowed.
When assigning a packed (type P) field to a type C field, the leading zeros are converted to blanks
(see also the WRITE statement).
If you do not want this formatting, you can perform the assignment with the UNPACK statement.

If the packed field is defined with decimal places, the appropriate number of digits after the decimal
point is transferred from the type C field:

For example: C field P field (DECIMALS 2)


-47.110 47.11-
Arithmetic Expressions
PROGRAM RSAAA04B.
DATA: COUNTER TYPE I,
AMOUNT TYPE P VALUE 1000,
SUM TYPE P,
RESULT TYPE P,
NUMBER(2) TYPE C VALUE '99',
ROOT TYPE F.
COMPUTE: COUNTER = COUNTER + 2,
RESULT = AMOUNT * COUNTER.
SUM = RESULT + NUMBER - COUNTER.
RESULT = COUNTER * (AMOUNT + NUMBER).
ROOT = SQRT(NUMBER + COUNTER).

operators functions
+ - / * SQRT EXP LOG
SIN COS STRLEN
DIV M OD
TAN ...

SAP AG

The key word COMPUTE is optional.


You can nest parenthetical expressions as deeply as you like.
Each operator and parenthesis is interpreted as a word, so you must separate them with blanks.
However, the opening parentheses of functions must appear right next to the name, e.g. SQRT( ... )

Incorrect: a = (B+EXP(C))*E/SIN(3-G).
Correct: a = ( B + EXP( C ) ) * E / SIN( 3 - G ).
Non-numeric values are automatically converted to numeric values before any arithmetic operations
are performed.
The operators DIV and MOD return integers as results (see the online documentation for
COMPUTE).
The function STRLEN returns the length of a character string.
You can only use arithmetic expressions in conjunction with the COMPUTE statement.
Sub-fields

REPORT RSAAA04C.
DATA: ACCOUNT(10) TYPE C,
START LIKE SY-DATUM.
ACCOUNT = '1234567890'.
START = '19950715'.
WRITE: ACCOUNT+8(2), '**',
START(4).

*Beginning of month
START+6(2) = '01'.
ACCOUNT+6 = '9999'.
WRITE: / ACCOUNT, '****',
START.
90 ** 1995
1234569999 **** 01.07.1995

SAP AG

You can address and change field components by making offset and length specifications.
Only a few statements (ASSIGN, actual parameters and WRITE ... TO) support variable offset and
length specifications.
String Operations
F1 F2 F3 G
CONCATENATE F1 F2 F3 INTO G.

SPLIT G AT SPACE INTO F1 F2 F3.

REPORT RSAAA04I.
DATA: ONE(1) VALUE 'I', TWO(4) VALUE 'LIKE',
THREE(6) VALUE 'ABAP/4', TARGET(20).

CONCATENATE ONE TWO THREE INTO TARGET SEPARATED BY SPACE.

WRITE: / TARGET.
.
.
CLEAR: ONE, TWO, THREE.
.
SPLIT TARGET AT SPACE INTO ONE TWO THREE.

WRITE: / ONE, TWO, THREE.


SAP AG

The key words CONCATENATE and SPLIT are used to concatenate and split strings.
Operands are treated like type C fields, regardless of their actual type, i.e. no type conversions are
performed.
Return code values for CONCATENATE:
SY-SUBRC = 0: Result fits into target field
SY-SUBRC = 4: Result is too long for target field; only the defined length is
transferred
Return code values for SPLIT:
SY-SUBRC = 0: All target fields were long enough
SY-SUBRC = 4: One of the target fields was not long enough
Other string handling commands include SEARCH, SHIFT, REPLACE, TRANSLATE and
CONDENSE (see also the online documentation).
Date Calculations

REPORT RSAAA04D.
DATA: DIFF TYPE P.
DATE_1 LIKE SY-DATUM,
DATE_2 LIKE SY-DATUM.
DATE_1 = SY-DATUM.
DATE_2 = SY-DATUM.

*Beginning of month
DATE_2+6(2) = '01'.

*Last day of previous month


DATE_2 = DATE_2 - 1.

*Difference in days
DIFF = DATE_1 - DATE_2.

SAP AG

When you assign a date field to a packed field, ABAP/4 calculates the number of days since the
beginning of 0000 AD.
Conversely, when you assign a packed field to a date field, the packed value is interpreted as the
number of days since the beginning of 1900.
The values of date fields are converted to packed values (number of days since the beginning of 1900)
before any arithmetic operations are performed.
Declaring Fields with PARAMETERS

PARAMETERS <field>.

REPORT RSAAA04E.
PARAMETERS:PAR1 TYPE P,
PAR2 DEFAULT 'X'.
.
.
.

MOVE PAR1 TO ... .

WRITE: PAR1, PAR2.


.
. .
.
. .
PAR1 -
PAR2 X

selection
screen

SAP AG

The PARAMETERS statement is a declarative key word for declaring internal fields. In contrast to
the DATA statement, fields declared with PARAMETERS are displayed on the selection screen for
input.
The PARAMETERS statement offers the same parameters as the DATA statement (i.e. TYPE, LIKE,
DECIMALS, etc.). While using the VALUES option with the DATA statement allows you to assign
initial values to fields, you can use the DEFAULT option of the PARAMETERS to set a default
value.
Overview of Control Statements

Case distinction

IF ENDIF
CASE ENDCASE

Loops

DO ENDDO
WHILE ENDWHILE

Other control statements

CHECK
EXIT

SAP AG

ABAP/4 has no jump statement (GOTO).


Besides the above control statements, there are other statements such as LOOP ... ENDLOOP, which
is used to read an internal table and the case distinction AT ... ENDAT for processing control levels
within the LOOP statement.
Relational Operators

Operator M eaning

EQ = EQual

NE < > >< Not Equal

GT > Greater Than

GE >= => Greater than or Equal

LT < Less Than

LE <= =< Less than or Equal


DATA: START TYPE D,
SUM1 TYPE F,
BETW EEN f1 and f2 Range
SUM2 TYPE P.
.
. IS INITIAL Initial value
.
IF START IS INITIAL ... .
IF SUM1 GT SUM2 ... .
IF SUM1 BETWEEN 0 AND 100 ... .

SAP AG

If the operands are not of the same data type, the system performs a type adjustment. For details about
which type is converted to which, please refer to the online documentation (see HELP IF).
If one field turns out to be shorter than the other when you compare two type C fields, the length of
the shorter field is adjusted to the longer field and padded with blanks on the right.
There are several other operators for performing character string comparisons (see HELP IF).
IF Statement
IF <logical expression>.
statem ents
statements
ENDIF.

IF <logical expression>.

statements
ELSE.
statements
ENDIF.

IF <logical expression>.
statements
statements

ELSEIF <logical expression>.


statements
statements
ELSEIF <logical expression>.

statements
statements
ELSE.
statements
statements
ENDIF

SAP AG

The ELSE and ELSEIF statements are optional.


If the logical expression is satisfied, the subsequent statement sequence is executed.
If the logical expression is not satisfied, the ELSE or ELSEIF part is processed. If there is no ELSE or
no other ELSEIF statement, the program processing continues after ENDIF.
You can place as many ELSEIF statements as you like between IF and ENDIF. No more than one of
the statement sequences is executed.
Control statements can be nested.
CASE Statement

CASE <field>.

WHEN <value>.
statements

WHEN <value2>.
statements

WHEN <value3>.
statements

WHEN OTHERS.
statements

ENDCASE.

SAP AG

Only one of the statement sequences is processed.


The WHEN OTHERS statement is optional.
Only one value can follow WHEN. This value can be either a literal or the name of the field which
contains the value.
DO Loop

DO <n> TIMES.
statem ents

ENDDO.

SY-INDEX loop index

SAP AG

You can specify the number of loop passes in a constant or through a field.
The <n> TIMES parameter is optional. If you do not use it, you must formulate a termination
statement in the loop (see EXIT statement).
You can nest loop statements.
You can modify the number of loop passes in the system field SY-INDEX.
W HILE Loop

WHILE <logical expression>.


statements
statements

ENDW HILE.

SY-INDEX loop index

WHILE COUNTER > 0.


.
.
.
SUBTRACT 1 FROM COUNTER.
ENDWHILE.

SAP AG

Provided the logical expression is satisfied, the statement sequence is executed.


You can nest any number of loop statements.
CHECK Statement
CHECK <logical expression>.

1 CHECK within a loop structure

WHILE COUNTER GT 0
statements
statements

CHECK FLAG NE SPACE.


statements
statem ents

ENDWHILE.

2 CHECK outside a loop structure

statements
statem ents

CHECK <condition>.
statements
statem ents

SAP AG

If you have specified CHECK in a loop and the logical expression is not satisfied, the statements in
the current loop pass are not executed and processing continues with the next loop pass.
If a CHECK condition in a subroutine is not satisfied, the subroutine is terminated.
If there is a CHECK statement outside a loop and the logical expression is not satisfied, the
statements in the current processing block are not executed.
EXIT Statement
EXIT.

1 EXIT within a loop structure.

DO
statem ents
statements
IF COUNTER GE 100.
EXIT.
ENDIF.
statem ents
statements

ENDDO.

2 EXIT outside a loop structure.

statem ents
statements
IF ... .
EXIT.
ENDIF.
statements
statem ents

SAP AG

An EXIT statement in a loop terminates the processing of the current loop.


An EXIT statement in a subroutine terminates the processing of the current subroutine.
If you have specified the EXIT statement outside a loop or subroutine, the report processing
terminates after the EXIT statement has been executed and the list is displayed.
Chapter 04 Summ ary

ABAP/4 distinguishes between data types and data


objects.
Special data types are available for solving many
problems.
You use control statements to direct the program flow.

SAP AG
Exercise Chapter 4: ABAP/4 Data and Statements

1. Name of your report: ZBCA##D1


##: Group number
Development class: $TMP (local)

Task:
Write a program which adds a number of days to a date.
Output the result as a date.
The date and number of days are to be entered as parameters
on the selection screen.
2. Name of your report: ZBCA##D2
##:
Group number
Development class: $TMP (local)

Task:
Create a calculator to handle the four basic arithmetic
operations and two integer values.
Both values and the operator are to be entered as parameters
on the selection screen.
Output the result with two decimal places.
Solutions Chapter 4: ABAP/4 Data and Statements

1. REPORT RSAAA041.
PARAMETERS: DAT1 LIKE SY-DATUM,

DAYS TYPE I.

DATA: DAT2 LIKE SY-DATUM.

DAT2 = DAT1 + DAYS.

SKIP 2.

WRITE: DAT1,

´ +´,

(5) DAYS,

TEXT-001

*D: TEXT-001: ´days´

SKIP 1.

WRITE: TEXT-002.

*D: TEXT-002: That is the -->

WRITE: 40 DAT2.
2. REPORT RSAAA042.

PARAMETERS: PAR1 TYPE I,

OPERATOR,

PAR2 TYPE I.

DATA: RESULT TYPE P DECIMALS 2.

CASE OPERATOR.

WHEN ´+´.

RESULT = PAR1 + PAR2.

WHEN ´-´.

RESULT = PAR1 - PAR2.

WHEN ´*´.

RESULT = PAR1 * PAR2.

WHEN ´/´.

RESULT = PAR1 / PAR2.

WHEN OTHERS.

WRITE: TEXT-001.

EXIT.

*D: TEXT-001: ´Invalid or missing

*D: operator´

ENDCASE.

WRITE: PAR1, OPERATOR, PAR2, ´=´, RESULT.

Das könnte Ihnen auch gefallen