The CONSTANTS statement is used to declare constants in ABAP. A constant is a named data object whose value is set at declaration and cannot be changed during the entire program runtime.
Using constants has several advantages:
- Readability: Code becomes more understandable when self-explanatory names are used instead of “magic” values (e.g., numbers or single letters) (e.g.,
c_status_releasedinstead of'F'). - Maintainability: When a fixed value changes (e.g., a threshold or tax rate), it only needs to be adjusted in one central place – in the
CONSTANTSdeclaration – not at every place in the code where it’s used. - Safety: They protect against accidentally changing fixed values during program execution. Any attempt to assign a new value to a constant results in a syntax error.
Syntax
There are different ways to declare constants:
1. Declaration of a Single Constant
CONSTANTS <constant_name> TYPE <data_type> [LENGTH <length>] [DECIMALS <decimal_places>] VALUE <value> | IS INITIAL.<constant_name>: The name that the constant should receive (must follow ABAP naming conventions).TYPE <data_type>: The ABAP data type (e.g.,I,P,C,STRING,D,T, a Dictionary type likeT001-BUTXT, or a self-defined type).LENGTH <length>: Optional length specification for types likeC,N,X,P.DECIMALS <decimal_places>: Required for packed numbers (typeP) to specify the number of decimal places.VALUE <value>: Mandatory (except withVALUE IS INITIAL). Here the fixed, immutable value is assigned. The value must match the data type. It can be a literal (e.g.,100,'Hello','19.99') or another, already defined constant.VALUE IS INITIAL: Assigns the type-specific initial value to the constant (e.g., 0 forI, space forC, empty string forSTRING).
2. Declaration of Multiple Constants (Chain Statement)
CONSTANTS: <const1> TYPE <type1> VALUE <value1>, <const2> TYPE <type2> VALUE <value2>, ... .With a colon after CONSTANTS, multiple constants can be declared separated by commas. The statement is terminated with a period.
3. Declaration of a Constant Structure
CONSTANTS: BEGIN OF <structure_name>, <component1> TYPE <type1> VALUE <value1>, <component2> TYPE <type2> VALUE <value2>, ... END OF <structure_name>.This allows defining a structure whose components all have constant values. Access is then via <structure_name>-<componentN>.
Examples
1. Simple Constants
CONSTANTS c_max_length TYPE i VALUE 255.CONSTANTS c_app_name TYPE string VALUE `My Super Report`.CONSTANTS c_space TYPE c LENGTH 1 VALUE ' '. " or VALUE IS INITIALCONSTANTS c_true TYPE abap_bool VALUE abap_true. " Using ABAP boolean constants2. Constant with Decimal Places (Type P)
CONSTANTS c_vat_standard TYPE p LENGTH 8 DECIMALS 2 VALUE '0.19'. " 19% VAT3. Chain Statement for Multiple Constants
CONSTANTS: c_status_new TYPE c LENGTH 1 VALUE 'N', c_status_in_progress TYPE c LENGTH 1 VALUE 'I', c_status_completed TYPE c LENGTH 1 VALUE 'C', c_euro TYPE waers VALUE 'EUR'.4. Constant Structure
CONSTANTS: BEGIN OF c_colors, red TYPE c LENGTH 1 VALUE '1', green TYPE c LENGTH 1 VALUE '2', blue TYPE c LENGTH 1 VALUE '3', END OF c_colors.5. Using Constants in Code
DATA gv_current_status TYPE c LENGTH 1.DATA gv_text_length TYPE i.DATA gv_amount_net TYPE p DECIMALS 2 VALUE '100.00'.DATA gv_amount_gross TYPE p DECIMALS 2.
gv_current_status = c_status_in_progress.gv_text_length = strlen( c_app_name ).
IF gv_current_status = c_status_in_progress. WRITE: / 'The report runs under the name:', c_app_name. WRITE: / 'Maximum length is:', c_max_length. WRITE: / 'Color red has code:', c_colors-red.ENDIF.
" Calculation with constantgv_amount_gross = gv_amount_net * ( 1 + c_vat_standard ).WRITE: / 'Gross amount:', gv_amount_gross CURRENCY c_euro.
" ****** SYNTAX ERROR ******" The following code would result in a syntax error," as constants cannot be changed:" c_max_length = 500." c_colors-red = 'X'." **************************In summary: The CONSTANTS statement is an important tool in ABAP to make fixed values nameable, maintainable, and secure in code. They should always be used when a value is guaranteed not to change during program execution.