0% found this document useful (0 votes)
2 views

Chapter 04_ABAP Data Declarations

The document provides an overview of ABAP data declarations, including elementary and user-defined data types, variable declaration, and assignment of values. It covers the use of constants, field symbols, arithmetic expressions, and dynamic assignments in ABAP programming. Additionally, it includes practical demonstrations and exercises to reinforce the concepts presented.

Uploaded by

Aniruddha_Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter 04_ABAP Data Declarations

The document provides an overview of ABAP data declarations, including elementary and user-defined data types, variable declaration, and assignment of values. It covers the use of constants, field symbols, arithmetic expressions, and dynamic assignments in ABAP programming. Additionally, it includes practical demonstrations and exercises to reinforce the concepts presented.

Uploaded by

Aniruddha_Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

IBM Global Services

ABAP Data Declarations

ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Objectives

 The participants will be able to :


 List ABAP elementary data types
 Declaring variables
 List user-defined data types
 Use arithmetic expressions
 List field-symbols

2 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

ABAP Elementary Data Types

C: Character Text N: Numeric Text

I: Integer D: Date

P: Packed # T: Time

F: Floating Point # X: Hexadecimal #

STRING XSTRING

3 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

ABAP Elementary Data Types (Contd.)

C: Character Text N: Numeric Text

I: Integer D: Date

P: Packed # T: Time

F: Floating Point # X: Hexadecimal #

STRING XSTRING

4 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Declaring Variables

DATA: PLAYER(35) TYPE C,


NICKNAME(35),
POINTS TYPE I,
GAMES TYPE I VALUE ‘10’,
AVERAGE(5) TYPE P,
STARTER,
ACQUIRED TYPE D.

5 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Initial Values

C: (blank) N: zeroes

I: zero D: 00000000

P: zero T: 000000

F: zeroes X: 00

The “CLEAR” statement sets a field back to its


initial value, not its default value.

6 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Assigning Default Values

DATA: PLAYER(35) TYPE C,


NICKNAME(35) VALUE ‘Dr. J’,
POINTS TYPE I VALUE ‘255’,
GAMES TYPE I VALUE 10,
AVERAGE(5) TYPE P VALUE ‘25.5’,
STARTER VALUE ‘Yes’,
ACQUIRED TYPE D VALUE ‘19760610’.

7 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Declaring “Like” Fields

DATA: PLAYER(35) TYPE C VALUE ‘Julius Erving’,


NICKNAME(35),
ACQUIRED TYPE D.

Use the “LIKE” addition to declare fields with


the same format (i.e., data type and length)

DATA: PLAYER(35) TYPE C VALUE ‘Julius Erving’,


NICKNAME LIKE PLAYER,
ACQUIRED LIKE SY-DATUM.

8 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Declaring Constants

The “VALUE” addition is required.

CONSTANTS: TEAM1(20) TYPE C VALUE ‘76ers’,


TEAM2 TYPE TEAM1 VALUE ‘Celtics’,
TOT_GAMES TYPE I VALUE 82.

If you attempt to change the value of a


constant, a syntax error will occur.

9 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

User-Defined Data Types

TYPES: NAME(35) TYPE C,


TEAMS(20) TYPE C.
DATA: PLAYER TYPE NAME VALUE ‘Troy Aikman’,
NICKNAME TYPE PLAYER.
CONSTANTS: TEAM1 TYPE TEAMS VALUE ‘Cowboys’,
TEAM2 TYPE TEAM1 VALUE ‘Packers’.

A user-defined data type created with the “TYPES”


statement is used to specify a field’s data type in the “TYPE”
addition of the “DATA” or “CONSTANTS” statements.

10 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Output Characteristic for Data Types

Standard Length Justification

C = defined length C = left-justified


I = 12 I = right-justified
P = (2 * defined length) + 1 P = right-justified
F = 22 F = right-justified
N = defined length N = left-justified
D = 10 D = left-justified
T =8 T = left-justified
X = (2 * defined length) X = left-justified

11 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Output for Numeric Fields

DATA: FLOAT TYPE F VALUE ‘98.7654321E2’,


PACK TYPE P VALUE 12,
INT TYPE I VALUE 32.

WRITE: / FLOAT,
/ FLOAT EXPONENT 1 DECIMALS 3,
/ FLOAT EXPONENT 0 DECIMALS 2,
/ PACK,
/ PACK DECIMALS 1,
/ INT DECIMALS 2.
9.876543210000000E+03
987.654E+01
9876.54
These fields are not aligned 12
because of the different 12.0
standard output lengths of
32.00
the numeric type fields.

12 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Demonstration

 Declaring variables and constants.

13 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Practice

 Declaring variables and constants.

14 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Assigning Values to Fields

MOVE <value> TO <field>.

[COMPUTE] <field> = <value or expression>.

ADD <value> TO <field>.


SUBTRACT <value> FROM <field>.
MULTIPLY <field> BY <value>.
DIVIDE <field> BY <value>.

DATA: TITLE(25),
SALARY TYPE P,
CNVSALARY TYPE SALARY,

MOVE ‘President’ TO TITLE.


COMPUTE SALARY = 5000000.
CNVSALARY = SALARY * 3.
ADD 1000 TO SALARY.

15 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Arithmetic Expressions

Operators Functions
+ - * / ** SQRT, EXP, LOG,
DIV and MOD SIN, COS, STRLEN, . . .

COUNTER = COUNTER + 1.
SALARY = BASE * BONUS_PERCENT.
LENGTH = STRLEN( NAME ).
ANSWER = ( 10 + SQRT( NUM1 ) ) / ( NUM2 - 10 ).

Spacing is very important when using arithmetic expressions!!!

16 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Sub-Fields in ABAP

DATA: CUSTOMER(10) TYPE C,


INV_DATE TYPE SYDATUM.
Use an offset and length to
CUSTOMER = ‘1234567890’. display or change portions of a
INV_DATE = ‘19960626’. field.

WRITE: / CUSTOMER+8(2), ‘xx’,


INV_DATE(4).

* Start of Month
INV_DATE+6(2) = ‘01’. 90 xx 1996
CUSTOMER+6(4) = ‘ABCD’. 123456ABCD ----- 06/01/1996
WRITE: / CUSTOMER, ‘------’, INV_DATE.

17 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Date Calculations in ABAP

DATA: DAYSOLD TYPE P,


DOB TYPE D,
TODAY TYPE SYDATUM.
Date fields store values as
DOB = ‘19621230’. “YYYYMMDD”.
TODAY = SY-DATUM.

DAYSOLD = TODAY - DOB.

WRITE: ‘You are’, DAYSOLD, ‘days old.’

You are 12410 days old.

18 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Demonstration

 Assigning values to the fields and doing calculations.

19 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Practice

 Assigning values to the fields and doing calculations.

20 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Declaring Fields with PARAMETERS

PARAMETERS: NUM TYPE I,


NAME(20) DEFAULT ‘AARON’.

ADD 10 TO NUM.

WRITE: / NUM,
‘----’,
NAME.

“Selection Screen”

21 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Selection Texts These selection texts


will be used on the
selection screen
instead of the
parameter names.

Selection Screen With the parameter description

22 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Demonstration

 Creation of a selection screen and displaying the value in the report.

23 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Practice

 Creation of a selection screen and displaying the value in the report.

24 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Working with Field Symbols in ABAP

DATA: NUM TYPE I VALUE 12.


FIELD-SYMBOLS: <F1>,
<F2> TYPE I,
<F3> TYPE NUM.
A field symbol is a
ASSIGN: NUM TO <F1>, “pointer” that assumes a
NUM TO <F2>, field’s address, not a
NUM TO <F3>. field’s value.

WRITE: / ‘Line 1:’, NUM, <F1>, <F2>, <F3>.

<F1> = 32.

WRITE: / ‘Line 2:’, NUM, <F1>, <F2>, <F3>.

Line 1: 12 12 12 12
Line 2: 32 32 32 32

25 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Demonstration

 Working with Field Symbols.

26 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Practice

 Working with Field Symbols.

27 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Dynamic Assignment of Partial Strings

DATA: TEXT_LINE(30) VALUE ‘ABCDEFGHIJK’.


FIELD-SYMBOLS <FSYMBOL>.
ASSIGN TEXT_LINE+2(5) TO <FSYMBOL>.
* this assigns 5 characters of text_line starting at
* position 3 to the field string.
WRITE: / ‘Text Line =‘, TEXT_LINE.
ULINE.
WRITE: / ‘Field Symbol=‘, <FSYMBOL>.
ULINE.
<FSYMBOL> = ‘1234567890’.

WRITE: / ‘Field Symbol =‘, <FSYMBOL>.


ULINE.
WRITE: / ‘Text Line =‘, TEXT_LINE.

28 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Dynamic Field Assignment

PARAMETERS FIELD(8) DEFAULT ‘SY-UZEIT’.


FIELD-SYMBOLS <FSYMBOL>.

ASSIGN (FIELD) TO <FSYMBOL>.

IF SY-SUBRC = 0.
WRITE: / ‘The contents of field’, FIELD, <FSYMBOL>.
ELSE.
WRITE: / ‘Failure assigning field’, FIELD, ‘to field symbol’.
ENDIF.

FIELD SY-UZEIT Selection Screen

The contents of field SY-UZEIT 12:32:06 List

29 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Demonstration

 Dynamic assignment with the Field Symbol.

30 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Practice

 Dynamic assignment with the Field Symbol.

31 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Summary

 “DATA” statement is used to declare a variable (or field) in an ABAP program


 "LIKE" and "TYPE" statements are used to declare fields with reference to other
variables or fields
 “CONSTANTS” statement is used to declare a constant
 Text elements can be used to maintain a program’s title and column headings,
selection texts, and text symbols. These text elements can be maintained in
multiple languages
 A field symbol does not reserve space for the field, but points to the field
 “ASSIGN” statement associates a field to a field symbol at runtime
 The system carries out automatic type conversion if variables are not of the same
type
 ABAP allows to display and change portions of a field by specifying an offset and
length

32 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation


IBM Global Services

Questions

 What are the different types of data types?


 What is a field symbol ? What is the advantage of using it in the program?
 How to create a selection screen with the selection screen text?

33 ABAP Data Declarations | Dec-2008 © 2005 IBM Corporation

You might also like