18
ABAP Data Declarations | 1.04 | Slide 1 IBM Global Services © Copyright IBM Corporation 2003 C: Character Text I: Integer P: Packed # F: Floating Point # N: Numeric Text D: Date T: Time X: Hexadecimal # ABAP Elementary Data Types

Intro to ABAP - Chapter 04_V1

Embed Size (px)

Citation preview

Page 1: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 1

IBM Global Services

© Copyright IBM Corporation 2003

C: Character Text

I: Integer

P: Packed #

F: Floating Point #

N: Numeric Text

D: Date

T: Time

X: Hexadecimal #

ABAP Elementary Data Types

Page 2: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 2

IBM Global Services

© Copyright IBM Corporation 2003

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

Page 3: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 3

IBM Global Services

© Copyright IBM Corporation 2003

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

Page 4: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 4

IBM Global Services

© Copyright IBM Corporation 2003

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

Page 5: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 5

IBM Global Services

© Copyright IBM Corporation 2003

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

Page 6: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 6

IBM Global Services

© Copyright IBM Corporation 2003

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

‘Celtics’,TOT_GAMESTYPE I VALUE 82.

If you attempt to change the value of a constant, a syntax error will occur.

The “VALUE” additionis required.

Declaring Constants

Page 7: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 7

IBM Global Services

© Copyright IBM Corporation 2003

TYPES:NAME(35) TYPE C,TEAMS(20) TYPE C.

DATA: PLAYER TYPE NAME VALUE ‘Troy Aikman’,

NICKNAME LIKE PLAYER.CONSTANTS: TEAM1 TYPE TEAMS VALUE ‘Cowboys’,

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

Page 8: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 8

IBM Global Services

© Copyright IBM Corporation 2003

Standard LengthStandard Length

CC ==defined lengthdefined lengthII ==1212PP ==(2 * defined length) + (2 * defined length) + 11FF ==2222NN ==defined lengthdefined lengthDD ==1010TT ==88XX ==(2 * defined length)(2 * defined length)

JustificationJustification

CC ==left-justifiedleft-justifiedII ==right-justifiedright-justifiedPP ==right-justifiedright-justifiedFF ==right-justifiedright-justifiedNN == left-justified left-justifiedDD == left-justified left-justifiedTT == left-justified left-justifiedXX == left-justified left-justified

Output Characteristic for Data Types

Page 9: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 9

IBM Global Services

© Copyright IBM Corporation 2003

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+03987.654E+01

9876.5412

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

Page 10: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 10

IBM Global Services

© Copyright IBM Corporation 2003

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

Page 11: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 11

IBM Global Services

© Copyright IBM Corporation 2003

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

Spacing is very important when using arithmetic expressions!!!

FunctionsSQRT, EXP, LOG,

SIN, COS, STRLEN, . . .

Operators+ - * / **DIV and MOD

Arithmetic Expressions

Page 12: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 12

IBM Global Services

© Copyright IBM Corporation 2003

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 MonthINV_DATE+6(2) = ‘01’.CUSTOMER+6 = ‘ABCD’.WRITE: / CUSTOMER, ‘------’, INV_DATE.

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

Use an offset and length to display

or change portions of a field.

Sub-Fields in ABAP

Page 13: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 13

IBM Global Services

© Copyright IBM Corporation 2003

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

Page 14: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 14

IBM Global Services

© Copyright IBM Corporation 2003

PARAMETERS: NUM TYPE I,NAME(20) DEFAULT

‘AARON’.

ADD 10 TO NUM.

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

32 ---- AARON

“Selection Screen”

Declaring Fields with PARAMETERS

Page 15: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 15

IBM Global Services

© Copyright IBM Corporation 2003

These selection texts will be used on the selection

screen instead of the parameter

names.

Selection Texts

Page 16: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 16

IBM Global Services

© Copyright IBM Corporation 2003

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 12Line 2: 32 32 32 32

Working with Field Symbols in ABAP

Page 17: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 17

IBM Global Services

© Copyright IBM Corporation 2003

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.

Dynamic Assignment of Partial Strings

Page 18: Intro to ABAP - Chapter 04_V1

ABAP Data Declarations | 1.04 | Slide 18

IBM Global Services

© Copyright IBM Corporation 2003

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.

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

FIELD SY-UZEIT Selection Screen

List

Dynamic Field Assignment