SAP ABAP — Variables

Feyza DERİNOĞLU
3 min readJun 6, 2020

Variables are named data objects used to store values within the allotted memory area of a program. Variable can be used as a referring name of the data.

A variable must be declare before using it and each variable in ABAP should declare with specific data type and size.

Variable names can be 1 to 30 characters long.

Use keyword DATA to declare a variable.

Examples of Variables Defined with the DATA Statement

REPORT Z_ABAP_DEVELOPER_TEST.

WRITE / ‘Welcome to Feyzas ABAP Tutorial ‘.
ULINE.

DATA: GV_FIRSTNAME(10) TYPE C,
GV_INDEX TYPE I,
GV_STUDENT_ID(5) TYPE N.

DATA GV_MAX_VALUE TYPE I VALUE 100.
DATA GV_TIME TYPE T VALUE ‘183240’.
DATA GV_QUARANTINE_DATE TYPE D VALUE ‘20200606’.
DATA GV_COUNTRY TYPE C LENGTH 6 VALUE ‘TURKEY’.

WRITE :/ GV_QUARANTINE_DATE.
WRITE :/ GV_TIME.
WRITE :/ ‘GV_COUNTRY :’ ,GV_COUNTRY.
WRITE :/ ‘GV_MAX_VALUE :’ ,GV_MAX_VALUE.

Output:

While declaring a variable we can also refer to an existing variable instead of data type. For that use LIKE instead of TYPE keyword while declaring a variable.

DATA: GV_FIRSTNAME TYPE C,
GV_LASTNAME LIKE GV_FIRSTNAME.

Structure Variable

Similar to structured data type, structured variable can be declared using BEGIN OF and END OF keywords.

DATA: BEGIN OF GS_STUDENT,
ID(5) TYPE N,
NAME(10) TYPE C,
SURNAME(10) TYPE C,
DOB TYPE D,
LOCATION(10) TYPE C,
END OF GS_STUDENT.

We can also declare a structured variable by referring to an existing structured data type.

TYPES: BEGIN OF TY_ADDRESS,
NAME(10) TYPE C,
STREET(10) TYPE C,
PLACE(10) TYPE C,
PINCODE(5) TYPE N,
PHONE(10) TYPE N,
END OF TY_ADDRESS.

DATA: GS_HOUSE_ADDRESS TYPE ADDRESS,
GS_SCHOOL_ADDRESS LIKE GS_HOUSE_ADDRESS.

Each individual field of the structured variable can be accessed using hyphen (-). For example, name field of the house_address structure can be accessed using housing_address-name.

Using the PARAMETERS Statement to Define Variables

The PARAMETERS statement can be used to declare the data objects that are linked to input fields on a selection screen.

A parameter is a lot like the data statement, but when you run the program, the system will display the parameters as input fields on a selection screen before the program actually begins to execute. You can use both parameters and data in the same program.

The maximum length is 8 characters.

Example

REPORT Z_ABAP_DEVELOPER_TEST.

PARAMETERS: NAME(10) TYPE C,
BRAND(10) TYPE C,
BUILDING TYPE I,
STOREMO TYPE I,
SCORE TYPE P DECIMALS 2,
CONNECT TYPE MARA-MATNR.

Output:

Reference Variables

The sytntax for declaring referance variable is :

DATA <ref> TYPE REF TO <type> VALUE IS INITIAL.

The REF TO addition declares a reference variable ref.

ABAP contains data data refence variable and object references.

The specification after REF TO specifies the static type of the reference variable.

Reference variable can be defined as a component of structure or ınternal table.

DATA DOCUMENT TYPE REF TO CL_DOCUMENT_BCS.
DATA RECIPIENT TYPE REF TO IF_RECIPIENT_BCS.

--

--

Feyza DERİNOĞLU

SAP Tutorials - Industrial Engineer & Software Developer & Management İnformation System and Engineering 👩🏻‍💻