33
IBM Global Services ABAP Data Declarations | 3. 04 March-2005 © 2005 IBM Corporation ABAP Data Declarations

ABAP Data Declarations

Embed Size (px)

DESCRIPTION

ABAP Data Declarations

Citation preview

ABAP Data DeclarationsList ABAP elementary data types
Declaring variables
ABAP Data Declarations | 3.04
In an ABAP program, you can use ten system-defined, elementary data types to declare data objects:
Character Text (standard length = 1; allowed length = no maximum)
Numeric Text (standard length = 1; allowed length = no maximum)
This data type should be used for numeric fields that will not be calculated (i.e., social security number and telephone number).
Integer (standard length is machine specific and cannot be changed)
This data type stores a binary number with a range of values from
-21477483648 to +2147483647.
Packed Number (standard length = 8; allowed length = 1 to 16)
With this data type, two digits are stored in a single byte and the last half byte is used to store the sign. This data type is recommended for commercial calculations.
Floating Point Number (standard length is machine specific and cannot
be changed)
With this data type, two digits are stored in a single byte and the last half byte is used to store the sign. This data type is recommended for mathematical/scientific calculations.
IBM Global Services
© 2005 IBM Corporation
C: Character Text
This data type stores data in the ‘YYYYMMDD’ format
Time (standard length = 6 and cannot be changed)
This data type stores data in the ‘HHMMSS’ format
HeXadecimal (standard length = 1; allowed length = no maximum)
String (similar to data type C and refers to a variable-length string ).
XString (similar to data type X and refers to a variable-length byte sequence. Strings can have any length).
It is not yet possible to use strings in screens or database tables. However, strings can be stored in database tables as clusters using EXPORT and transferred using IMPORT.
IBM Global Services
© 2005 IBM Corporation
AVERAGE(5) TYPE P,
ABAP Data Declarations | 3.04
To declare a variable (or field) in an ABAP program, use the “DATA” statement:
DATA <name>(<length>) TYPE <data type> VALUE <default>.
The only required parameter in this statement is the <name> of the variable.
A variable’s name can be up to 30 characters in length.
This name should begin with a letter and the remainder of the name should consist of letters, numbers, or an underscore.
“SPACE” is a reserved word in ABAP.
The variable’s <length> is specified in parentheses immediately after the <name> (no spaces in between the name and the length).
If no length is specified, the variable will be the standard length for the data type (see previous page).
The variable’s <data type> is specified after the “TYPE” addition.
If no data type is specified, the variable will be type ‘C’.
To define a default value for the variable, use the “VALUE” addition.
This default value is different than the initial value automatically assigned to a data object (see next page).
In the example code above, what is the data type and length of the “STARTER” variable?
The function, STRLEN, can be used to find the length of a character string
TIPS
Initial Values
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.
ABAP Data Declarations | 3.04
All data objects declared have an associated initial value. This initial value depends on the data type:
C,STRING: blank
N: 0…0 (the number of zeroes depends on the defined length)
I: 0
P: 0
F: 0…0E+00 (the number of zeroes depends on the system length)
D: 00000000
T: 000000
X,XSTRING: 00
The “CLEAR <field>” statement sets a field back to its initial value, not its
default value.
STARTER VALUE ‘Yes’,
ABAP Data Declarations | 3.04
Use the following rules when assigning default values to data objects:
Default values for the character-based data types (C and N) must be a text literals (i.e. enclosed in single quotes. ).
For example, see the “NICKNAME” variable above illustrates this rule.
Default integer values for the numeric data types (I, P, and F) may or may not be enclosed in single quotes.
For example, the “POINTS” variable above uses single quotes, but the “GAMES” variable does not.
Default fractional values for the numeric data types (P and F) must be a text literals (i.e. enclosed in single quotes); otherwise, the decimal point would indicate the end of the statement.
For example, the “AVERAGE” variable above illustrates this rule.
For a packed or floating point number to be stored with a decimal, it must be defined with the “DECIMALS” addition. For example, AVERAGE(5) TYPE P DECIMALS 2 VALUE ‘25.5’.
Default values for date (D) and time (T) variables must be enclosed in
single quotes.
For example, the “ACQUIRED” variable above illustrates this rule.
In the example code above, what is the value of the “STARTER” variable?
IBM Global Services
© 2005 IBM Corporation
Declaring “Like” Fields
NICKNAME(35),
NICKNAME LIKE PLAYER,
ACQUIRED LIKE SY-DATUM.
Use the “LIKE” addition to declare fields with the same format (i.e., data type and length)
ABAP Data Declarations | 3.04
To declare fields with the same format (i.e. data type and length), use the “LIKE <field>” addition instead of the “TYPE” addition to the data declaration.
Defining fields in this manner makes it easy to change the format of several fields because you only need to make a change to the data type or length once in the program.
In the “LIKE <field>“ addition, the <field> can be:
A previously-defined program field or field string (we will cover field strings in a future chapter)
For example, the “NICKNAME” field above is defined “LIKE PLAYER”.
An ABAP dictionary field
For example, the “ACQUIRED” field above is defined “LIKE SY-DATUM”.
Remember that structures are considered table types in the
ABAP Dictionary.
The “LIKE” addition only applies to the data type and length, not a default value assigned to the referenced field.
For example, the “NICKNAME” field above defined “LIKE PLAYER” will be a 35-length character field. However, the “NICKNAME” field will not have the “Julius Erving” default value assigned to it.
IBM Global Services
© 2005 IBM Corporation
TEAM2 LIKE TEAM1 VALUE ‘Celtics’,
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.
ABAP Data Declarations | 3.04
To declare a constant in an ABAP program, use the “CONSTANTS” statement. The basic syntax of this statement is:
CONSTANTS <name>(<length>) TYPE <data type> VALUE <value>.
A constant’s <name> can be up to 30 characters in length.
This name should begin with a letter and the remainder of the name should consist of letters, numbers, or an underscore.
“SPACE” is a reserved word in ABAP.
Like the constant’s <name>, the “VALUE” addition is required because a constant must have a value.
The constant’s <length> is specified in parentheses immediately after the <name> (no spaces in between the name and the length).
If no length is specified, the constant will be the standard length for the data type.
The constant’s <data type> is specified after the “TYPE” addition.
If no data type is specified, the constant will be type “C”.
You can use the “LIKE” addition instead of the “TYPE” addition as described on the previous page.
You cannot change the value of a constant in an ABAP program (syntax error).
Hard-coded literals should be avoided. Use CONSTANTS if this clarifies the code.
IBM Global Services
© 2005 IBM Corporation
User-Defined Data Types
NICKNAME LIKE PLAYER.
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.
ABAP Data Declarations | 3.04
To declare a user-defined data type in an ABAP program, use the “TYPES” statement. The basic syntax of this statement is:
TYPES <name>(<length>) TYPE <data type>.
A user-defined data type’s <name> can be up to 30 characters in length.
This name should begin with a letter and the remainder of the name should consist of letters, numbers, or an underscore.
“SPACE” is a reserved word in ABAP.
The “VALUE” addition cannot be used because this statement only defines a data type. This statement does not allocate any memory for a field and, therefore, cannot have a value.
The user-defined data type’s <length> is specified in parentheses immediately after the <name> (no spaces in between the name and
the length).
If no length is specified, the user-defined data type will be the standard length for the data type.
The user-defined data type’s <data type> is specified after the “TYPE” addition.
If no data type is specified, the user-defined data type will be type “C”.
You can use the “LIKE” addition instead of the “TYPE” addition.
IBM Global Services
© 2005 IBM Corporation
Standard Length
F = 22
ABAP Data Declarations | 3.04
With the “WRITE” statement, a data object written to the list will have the standard output length associated to its data type (see above slide). If you want to change the output length of a field written to the report, you must specify the length in parentheses before the field name. For example, to write out a length 20 character field named “VAR1” in only 10 spaces, use the following “WRITE” statement:
WRITE: /5 “The value is”, (10) VAR1.
For packed and integer fields, leading zeros are converted to blanks.
For packed and integer fields, commas are inserted between thousands.
With the “WRITE” statement, a data object written to the list will be justified within its field length according to the standard justification associated to its data type (see above slide). If you want to change a field’s justification, you must use the “LEFT-JUSTIFIED”, “RIGHT-JUSTIFIED”, or “CENTERED” additions after the field name. For example, to make an integer field left justified, use the following “WRITE” statement:
WRITE: /5 “The total count is”, COUNT LEFT-JUSTIFIED.
IBM Global Services
© 2005 IBM Corporation
PACK TYPE P VALUE 12,
INT TYPE I VALUE 32.
WRITE: / FLOAT,
/ PACK,
9876.54
12
12.0
32.00
These fields are not aligned because of the different standard output lengths of the numeric type fields.
ABAP Data Declarations | 3.04
You can determine the number of decimal places to display in all numeric fields (i.e., integer, packed, and floating point fields) with the “DECIMALS” addition to the “WRITE” statement:
WRITE <numeric field> DECIMALS <n>.
ABAP performs automatic rounding of numeric fields.
You can explicitly round numeric fields with the “ROUND” statement.
You can determine the value of the exponent to display in floating point fields with the “EXPONENT” addition to the “WRITE” statement:
WRITE <floating point field> EXPONENT <n>.
If you want a packed field to store a decimal value, the “DECIMAL” addition must be used when defining the field. For example:
DATA <packed field> DECIMALS <n>.
The “DECIMALS” addition can also be used when defining parameters, constants, or user-defined data types.
Coding Recommendations:
A common cause of error occurs using the WRITE command with prices/values.
Ensure to always use the CURRENCY option.
Ex: WRITE: nnnnnnn CURRENCY nnn.
TIPS
ADD <value> TO <field>.
SUBTRACT <value> FROM <field>.
MULTIPLY <field> BY <value>.
DIVIDE <field> BY <value>.
ABAP Data Declarations | 3.04
MOVE <value> TO <field>.
The key word “COMPUTE” is optional.
ADD <value> TO <field>.
SUBTRACT <value> FROM <field>.
MULTIPLY <field> BY <value>.
DIVIDE <field> BY <value>.
There are other alternatives that we will cover in later chapters.
The system carries out automatic type conversion if variables are not of the same type.
If a character field is to be converted to a packed field, only numbers, the plus/minus sign, and the decimal point are allowed. The plus/minus sign will be placed at the end of the packed field.
If a packed field is to be converted to a character field, the numerical value is formatted (decimal point, sign) and the leading zeros are converted to blanks.
For information for all conversions between variables of different types, see the ABAP editor documentation for MOVE (type MOVE and hit F1).
UNPACK is a specific conversion keyword moving values from a packed variable (type P) to a character one (type C).
IBM Global Services
© 2005 IBM Corporation
Spacing is very important when using arithmetic expressions!!!
Functions
The valid operators for arithmetic expressions are: +, -, *, /, **, DIV and MOD.
** indicates exponentiation (i.e., 3 ** 2 means 3 to the power of 2).
DIV indicates the quotient (i.e., 10 DIV 3 evaluates to 3).
MOD indicates the remainder (i.e., 10 MOD 3 evaluates to 1).
Arithmetic functions also exist in ABAP. Some of these functions include: SQRT, EXP, LOG, SIN, COS, and STRLEN.
The order of operation in arithmetic expressions is:
Parentheses
Functions
Exponentiation
+ and -
All operators and parenthesis in an arithmetic expression must be separated by at least one space. Functions must be followed immediately by an opening parentheses:
Incorrect: ANSWER=(10+SQRT(NUM1))/(NUM2 - 10).
Correct: ANSWER = ( 10 + SQRT( NUM1 ) ) / ( NUM2 - 10 ).
Before calculations are performed, field values of non-numeric fields are automatically converted to numerical values.
There is no limit to the nesting levels for parentheses in arithmetic expressions.
IBM Global Services
© 2005 IBM Corporation
Sub-Fields in ABAP
90 xx 1996
123456ABCD ----- 06/01/1996
Use an offset and length to display or change portions of a field.
DATA: CUSTOMER(10) TYPE C,
ABAP Data Declarations | 3.04
ABAP allows you to display and change portions of a field by specifying an offset and length.
To display a portion of a field, use the following “WRITE” statement:
WRITE <field>+<offset>(<length>).
For example, to write the third, fourth, and fifth characters of a field named “CUSTOMER”, the “WRITE” statement would be:
WRITE CUSTOMER+2(3).
To change a portion of a field, use the following statement:
<field>+<offset>(<length>) = <value>.
For example, to change the month of a date field named “BIRTHDAY”, the statement would be:
BIRTHDAY+4(2) = “11”.
The offset and length can be used together (as the previous examples illustrate) or separately.
IBM Global Services
© 2005 IBM Corporation
Date fields store values as “YYYYMMDD”.
DATA: DAYSOLD TYPE P,
ABAP Data Declarations | 3.04
Remember that date fields are stored “YYYYMMDD”.
You can perform calculations on dates, but you must keep in mind that the system can only work in terms of a number of days. For example:
You can add seven days to a date field.
You cannot add two weeks to a date field. This value would have to be converted into a number of days.
If you perform calculations on a date field, the date field is converted to a packed number that represents the number of days since the beginning of 01/01/0001.
Coding Recommendations:
There are numerous SAP functions that will perform date calculations.
Ex: DATE_COMPUTE_DAY, DATE_GET_WEEK, DATE_CHECK_PLAUSIBILITY,
POPUP_TO_SELECT_MONTH, EASTER_GET_DATE.
Conversion functions usually have the word CONVERT or CONVERSION in their name.
Ex: CONVERSION_EXIT_PERI_INPUT converts dates from the format MMYYYY to YYYYMM
(if passed in as MMYY, it is passed out as YYYYMM), CONVERSION_EXIT_INVDT_INPUT
converts date in format DDMMYYYY to SAP inverted date format - plus many other variations.
TIPS
ABAP Data Declarations | 3.04
ABAP Data Declarations | 3.04
ABAP Data Declarations | 3.04
To declare a variable (or field) in an ABAP program that can be given a value at runtime, use the “PARAMETERS” statement. The basic syntax is:
PARAMETERS <name>(<length>) TYPE <data type> DEFAULT <default>.
The only required addition in this statement is the <name> of the parameter.
A parameter’s name can be up to 8 characters in length.This name should begin with a letter and the remainder of the name should consist of letters, numbers, or an underscore.
“SPACE” is a reserved word in ABAP.
The parameter’s <length> is specified in parentheses immediately after the <name> (no spaces in between the name and the length).
If no length is specified, the variable will be the standard length for the data type (see page 1).
The parameter’s <data type> is specified after the “TYPE” addition.
If no data type is specified, the variable will be type ‘C’.
To define a default value for the parameter, use the “DEFAULT” addition.
This default value is different from the initial value automatically assigned to a data object (see page 3).
When you execute a program using the “PARAMETERS” statement, a “selection screen” is displayed for you to enter values into the parameters. To continue program execution from the selection screen, click on the “Execute” pushbutton (or click the “F8” key).
IBM Global Services
© 2005 IBM Corporation
Selection Texts
These selection texts will be used on the selection screen instead of the parameter names.
Selection Screen With the parameter description
ABAP Data Declarations | 3.04
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, so the display to the user will depend on the user’s specified logon language.
Maintain the selection texts by selecting the “Selection texts” option in the Text Elements function (“GoTo Text elements” menu path).
The text placed in the “Text” field for a parameter will be used on the selection screen instead of the name of the parameter. Using selection texts allows you to create meaningful text on the selection screen.
IBM Global Services
© 2005 IBM Corporation
Demonstration
Creation of a selection screen and displaying the value in the report.
ABAP Data Declarations | 3.04
Practice
Creation of a selection screen and displaying the value in the report.
ABAP Data Declarations | 3.04
FIELD-SYMBOLS: <F1>,
<F1> = 32.
Line 1: 12 12 12 12
Line 2: 32 32 32 32
A field symbol is a “pointer” that assumes a field’s address, not a field’s value.
ABAP Data Declarations | 3.04
A field symbol is a place holder for a field (i.e. a pointer). A field symbol does not reserve space for the field, but points to the field (i.e., a field symbol assumes the address of the field, not the value).
A field symbol can point to single fields or field strings (structures). Remember that the “TABLES” statement creates a field string with the appropriate ABAP Dictionary structure.
When working with field symbols, angle brackets (< >) must be used.
The “FIELD-SYMBOLS” statement is used to declare a field symbol.
A field symbol’s name can be up to 30 characters in length including angle brakets(<>).
This name should begin with a letter and the remainder of the name should consist of letters, numbers, or an underscore.
If a field symbol is defined without attributes, it takes on the attributes of the field assigned to it.
If a field symbol is given a data type and length (either explicitly or with the “LIKE” addition), the system verifies that the field symbol has the same attributes as the field assigned to it.
The “ASSIGN” statement associates a field to a field symbol at runtime (i.e., when the program is executed).
If you change the value to a field symbol, you are really changing the value of the field assigned to the field symbol. If you write the value of the field symbol, you are really writing the value of the field assigned to the field symbol.
IBM Global Services
© 2005 IBM Corporation
DATA: TEXT_LINE(30) VALUE ‘ABCDEFGHIJK’.
* this assigns 5 characters of text_line starting at
* position 3 to the field string.
WRITE: / ‘Text Line =‘, TEXT_LINE.
WRITE: / ‘Text Line =‘, TEXT_LINE.
ABAP Data Declarations | 3.04
With the “ASSIGN” statement, you can assign a portion of a field to a field symbol by specifying the field’s offset and length.
In the example above, the field symbol “FSYMBOL” is assigned to five characters of the variable “TEXT_LINE” starting with the third character. If a change is made to the field symbol, only these five characters of the variable will be changed.
The output from the code above would be:
Text Line =ABCDEFGHIJK
Field Symbol = CDEFG
Field Symbol = 12345
Text Line = AB12345HIJK
IBM Global Services
© 2005 IBM Corporation
Dynamic Field Assignment
ELSE.
ENDIF.
Selection Screen
ABAP Data Declarations | 3.04
With the “ASSIGN” statement, you can assign a field to a field symbol where the name of the field is not known until the program is executed. This is called dynamic field assignment.
For example, in the code above a field name is being entered as a parameter. The field entered will be assigned to a field symbol.
To perform a dynamic field assignment, you must enclose in parentheses the variable that contains the appropriate field name. For example:
ASSIGN (FIELD) TO <FSYMBOL>.
After a dynamic field assignment, the value of SY-SUBRC indicates whether a field was assigned to the field symbol.
If the assignment was successful (i.e., the variable in parentheses contained a valid field name), SY-SUBRC will be set to zero.
If the assignment was not successful (i.e., the variable in parentheses did not contain a valid field name), SY-SUBRC will be set to a non-zero number.
If you are going to assign a field string created with the “TABLES” statement to a field symbol, execution time is minimised if you use the “ASSIGN TABLE FIELD” option.
IBM Global Services
© 2005 IBM Corporation
ABAP Data Declarations | 3.04
ABAP Data Declarations | 3.04
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
ABAP Data Declarations | 3.04
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?
ABAP Data Declarations | 3.04