Intro to ABAP Chapter 04

background image

BC170_2.04.1

Data Declarations

background image

BC170_2.04.2

Objectives

• The participants will be able to:

– Define the Data Types used in ABAP Programming
– Declare Variables within an ABAP Program
– Assign Default Values to variables within an ABAP

Program

– Create user defined Data Types
– Assign values to fields within an ABAP Program
– Use Arithmetic Expressions in an ABAP Program
– Create Report Parameters in an ABAP Program
– Use Field Symbols in an ABAP Program

background image

BC170_2.04.3

C: Character Text

I: Integer

P: Packed #

F: Floating Point #

N: Numeric Text

D: Date

T: Time

X: Hexadecimal #

ABAP Elementary Data Types

background image

BC170_2.04.4

DATA

: PLAYER(35) TYPE C,

NICKNAME(35),
POINTS

TYPE I,

GAMES

TYPE I

VALUE ‘10’,

AVERAGE(5) TYPE P,
STARTER,
ACQUIRED

TYPE D.

Declaring Variables

background image

BC170_2.04.5

C: (blank)

I: zero

P: zero

F: zeroes

N: zeroes

D: 00000000

T: 000000

X: 00

The CLEAR statement sets a field back to its initial

value, not its default value.

Initial Values

background image

BC170_2.04.6

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’

.

Assigning Default Values

background image

BC170_2.04.7

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

NICKNAME(35),
ACQUIRED

TYPE D.

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

NICKNAME

LIKE PLAYER

,

ACQUIRED

LIKE SY-DATUM

.

Use the LIKE addition to

declare fields with the same

format (i.e., data type and

length)

Declaring Like Fields

background image

BC170_2.04.8

CONSTANTS

:C_TEAM1(20)

TYPE C VALUE ‘76ers’,

C_ TEAM2

LIKE TEAM1 VALUE

‘Celtics’,

C_ TOT_GAMES TYPE I VALUE 82.

If you attempt to change the value of a constant,

a syntax error will occur.

The VALUE addition

is required.

Declaring Constants

background image

BC170_2.04.9

TYPES: NAME(35)

TYPE C,

TEAMS(20) TYPE C.

DATA: PLAYER

TYPE NAME

VALUE ‘Troy

Aikman’,

NICKNAME LIKE PLAYER.

CONSTANTS:C_TEAM1

TYPE TEAMS

VALUE

‘Cowboys’,

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

User-Defined Data Types

background image

BC170_2.04.10

Standard Length

Standard Length

C

C

=

=

defined length

defined length

I

I

=

=

12

12

P

P

=

=

(2 * defined length) +

(2 * defined length) +

1

1

F

F

=

=

22

22

N

N

=

=

defined length

defined length

D

D

=

=

10

10

T

T

=

=

8

8

X

X

=

=

(2 * defined length)

(2 * defined length)

Justification

Justification

C

C

=

=

left-justified

left-justified

I

I

=

=

right-justified

right-justified

P

P

=

=

right-justified

right-justified

F

F

=

=

right-justified

right-justified

N

N

=

=

left-justified

left-justified

D

D

=

=

left-justified

left-justified

T

T

=

=

left-justified

left-justified

X

X

=

=

left-justified

left-justified

Output Characteristic for Data

Types

background image

BC170_2.04.11

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

12

12.0

32.00

These fields are not

aligned because of the

different standard

output lengths of the

numeric type fields.

Output for Numeric Fields

background image

BC170_2.04.12

DATA: TITLE(25),

SALARY TYPE P,
CNVSALARY LIKE

SALARY.

MOVE ‘President’ TO TITLE.
COMPUTE SALARY = 5000000.
CNVSALARY = SALARY * 3.
ADD 1000 TO SALARY.

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

Assigning Values to Fields

background image

BC170_2.04.13

COUNTER = COUNTER + 1.

SALARY = BASE * BONUS_PERCENT.

LENGTH = STRLEN( NAME ).

ANSWER = ( 10 + SQRT( NUM1 ) ) / ( NUM2 -
10 ).

Spacing is very important when using arithmetic

expressions!!!

Functions

SQRT, EXP, LOG,

SIN, COS, STRLEN, . . .

Operators

+ - * / **

DIV and MOD

Arithmetic Expressions

background image

BC170_2.04.14

DATA: CUSTOMER(10) TYPE C,

INV_DATE LIKE SY-DATUM.

CUSTOMER = ‘1234567890’.
INV_DATE = ‘19960626’.

WRITE: / CUSTOMER

+8(2)

, ‘xx’,

INV_DATE

(4)

.

* Start of Month
INV_DATE

+6(2)

= ‘01’.

CUSTOMER

+6

= ‘ABCD’.

WRITE: / CUSTOMER, ‘------’, INV_DATE.

90 xx 1996
123456ABCD ----- 06/01/1996

Use an offset and

length to display or

change portions of a

field.

Sub-Fields in ABAP

background image

BC170_2.04.15

DATA: DAYSOLD TYPE P,

DOB TYPE D,
TODAY LIKE SY-DATUM.

DOB = ‘19621230’.
TODAY

= SY-DATUM.

DAYSOLD = TODAY - DOB.

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

You are 12410 days old.

Date fields store

values as

YYYYMMDD.

Date Calculations in ABAP

background image

BC170_2.04.16

PARAMETERS

:

P_NUM TYPE I,

P_NAME(20) DEFAULT

‘Aaron’.

ADD 10 TO P_NUM.

WRITE: / P_NUM,

‘----’,
P_NAME.

32 ---- Aaron

Selection Screen

Declaring Fields with PARAMETERS

background image

BC170_2.04.17

Selection Texts

These selection

texts will be used

on the selection

screen instead of

the parameter

names.

background image

BC170_2.04.18

DATA: NUM TYPE I VALUE 12.

FIELD-SYMBOLS

:

<F1>,

<F2> TYPE I,
<F3> LIKE NUM.

ASSIGN

:

NUM TO <F1>,

NUM TO <F2>,
NUM TO <F3>.

WRITE: /

‘Line 1:’, NUM, <F1>,

<F2>, <F3>.

<F1> = 32.

WRITE: /

‘Line 2:’, NUM, <F1>,

<F2>, <F3>.

A field symbol is a

“pointer” that

assumes a field’s

address, not a

field’s value.

Line 1: 12

12

12

12

Line 2: 32

32

32

32

Working with Field Symbols in

ABAP

background image

BC170_2.04.19

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 2
* (3

rd

letter, because count starts at 0) 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.

Dynamic Assignment of Partial

Strings

background image

BC170_2.04.20

PARAMETERS P_FIELD(8) DEFAULT ‘SY-UZEIT’.
FIELD-SYMBOLS <FSYMBOL>.

ASSIGN (P_FIELD) TO <FSYMBOL>

.

IF SY-SUBRC = 0.

WRITE: / ‘The contents of field’, P_FIELD,

<FSYMBOL>.
ELSE.

WRITE: / ‘Failure assigning field’, P_FIELD, ‘to

field symbol’.
ENDIF.

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

FIELD

SY-UZEIT

Selection Screen

List

Dynamic Field Assignment

background image

BC170_2.04.21

Summary

• The participants should be able to:

– Define the Data Types used in ABAP Programming
– Declare Variables within an ABAP Program
– Assign Default Values to variables within an ABAP

Program

– Create user defined Data Types
– Assign values to fields within an ABAP Program
– Use Arithmetic Expressions in an ABAP Program
– Create Report Parameters in an ABAP Program
– Use Field Symbols in an ABAP Program

background image

BC170_2.04.22


Document Outline


Wyszukiwarka

Podobne podstrony:
Intro to ABAP Chapter 13
Intro to ABAP Chapter 07
Intro to ABAP Chapter 12
Intro to ABAP Chapter 15
Intro to ABAP Chapter 08
Intro to ABAP Chapter 11

więcej podobnych podstron