SAPHub

ABAP Variables

ABAP Variables are instances of data types. Variables are created during program execution and destroyed after program execution.

Use keyword DATA to declare a variable.

DATA: firstname(10) TYPE c,
      index         TYPE i,
      student_id(5) TYPE n.

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: firstname(10) TYPE c,
      lastname(10)  LIKE firstname. " Observe LIKE keyword

Structured Variable

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

DATA: BEGIN OF student,
      id(5)     TYPE n,
      name(10)  TYPE c,
      dob       TYPE d,
      place(10) TYPE c,
      END OF student.

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

TYPES: BEGIN OF address,
       name(10)   TYPE c,
       street(10) TYPE c,
       place(10)  TYPE c,
       pincode(6) type n,
       phone(10)  type n,
       END OF address. 

Data: house_address  type address,
      office_address like 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.

Character is the default data type.

DATA: true.  " By default it will take C as data type