Beruflich Dokumente
Kultur Dokumente
Data declaration
Type declaration
Value assignm ents
String operations
Field sym bols
Control statem ents
SAP AG
Chapter 04 Objectives
SAP AG
ABAP/4 Data Types and their Attributes
Data type M eaning Initial value Standard Permitted
length length
I integer 0 * -
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
SAP AG
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.
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.
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.
SAP AG
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
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
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)
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:
operators functions
+ - / * SQRT EXP LOG
SIN COS STRLEN
DIV M OD
TAN ...
SAP AG
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.
REPORT RSAAA04I.
DATA: ONE(1) VALUE 'I', TWO(4) VALUE 'LIKE',
THREE(6) VALUE 'ABAP/4', TARGET(20).
WRITE: / TARGET.
.
.
CLEAR: ONE, TWO, THREE.
.
SPLIT TARGET AT SPACE INTO ONE TWO THREE.
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'.
*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'.
.
.
.
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
CHECK
EXIT
SAP AG
Operator M eaning
EQ = EQual
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
statements
statements
ELSE.
statements
statements
ENDIF
SAP AG
CASE <field>.
WHEN <value>.
statements
WHEN <value2>.
statements
WHEN <value3>.
statements
WHEN OTHERS.
statements
ENDCASE.
SAP AG
DO <n> TIMES.
statem ents
ENDDO.
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
ENDW HILE.
SAP AG
WHILE COUNTER GT 0
statements
statements
ENDWHILE.
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.
DO
statem ents
statements
IF COUNTER GE 100.
EXIT.
ENDIF.
statem ents
statements
ENDDO.
statem ents
statements
IF ... .
EXIT.
ENDIF.
statements
statem ents
SAP AG
SAP AG
Exercise Chapter 4: ABAP/4 Data and Statements
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.
SKIP 2.
WRITE: DAT1,
´ +´,
(5) DAYS,
TEXT-001
SKIP 1.
WRITE: TEXT-002.
WRITE: 40 DAT2.
2. REPORT RSAAA042.
OPERATOR,
PAR2 TYPE I.
CASE OPERATOR.
WHEN ´+´.
WHEN ´-´.
WHEN ´*´.
WHEN ´/´.
WHEN OTHERS.
WRITE: TEXT-001.
EXIT.
*D: operator´
ENDCASE.