Wednesday, 18 November 2015

Chapter1(continue)

8. Assigning values to variables

An Assignment statement is used to assign value to a variable in an ABAP program. There are 4 different types of assignment statements:-
  1. MOVE
  2. MOVE-CORRESPONDING
  3. WRITE TO
  4. CLEAR
Now, let’s discuss each statement in detail.

1. MOVE statement

The MOVE statement is used to assign value to a variable.

MOVE 5 TO VAR1.
MOVE ‘a’ TO VAR2.


In above examples, value 5 is assigned to VAR1 and a is assigned to VAR2.

The above statements can also be written as:-

VAR1 = 5. VAR2 = ‘a’. So, "MOVE val TO f1" and "f1 = val" are both functionally equivalent. You can use either of the 2 statements in your program, but stick to 1 form throughout the program.

The MOVE statement is also used to assign the value of one variable to another variable, but the value of original variable remains unchanged. It means MOVE statement can also be used to transfer the contents of one variable to another variable. The syntax for MOVE statement is as follows:-

MOVE <f1> TO <f2>.

In above syntax, the content of <f1> is transferred to <f2>. It is not necessary for <f1> to be a variable; it can be a literal, constant or text symbol.

Following programs demonstrate the use of MOVE statement:-

Program 1:
REPORT ZMOVE_DEMO.

*-------------------------------------------------------*
*         data declarations
*-------------------------------------------------------*

DATA: var1 TYPE c,
      var2 TYPE i,
      var3(3) TYPE p DECIMALS 2,
      var4 TYPE i,
      var5 TYPE i.

CONSTANTS: c1 TYPE i VALUE 10.

*-------------------------------------------------------*
*       using MOVE statement
*-------------------------------------------------------*

var1 = 'B'."functionally equivalent to MOVE statement

MOVE 1 TO var2."using MOVE statement to assign literal 1 to variable var2

MOVE '3.14' TO var3."using MOVE statement to assign literal 3.14 to variable var3

MOVE c1 TO var4."using MOVE statement to assign constant c1 to variable var4

MOVE var2 TO var5."using MOVE statement to assign value of var2 to var5

*--------------------------------------------------------------------*
*       displaying data
*--------------------------------------------------------------------*

WRITE: 'var1= ',var1.
WRITE: / 'var2= ',var2.
WRITE: / 'var3= ',var3.
WRITE: / 'var4= ',var4.
WRITE: / 'var5= ',var5.  
*--------------------------------------------------------------------*

The output of above program is as follows:-
var1= B
var2= 1
var3= 3.14
var4= 10
var5= 1

Program 2:
REPORT zmove_demo1.

*--------------------------------------------------------------------*
*          data declarations
*--------------------------------------------------------------------*

DATA: BEGIN OF rec1,
        rec1_var1(3) TYPE c VALUE 'ABC',
        rec1_var2    TYPE i VALUE 100,
      END OF rec1.

DATA: BEGIN OF rec2,
        rec2_var1(3) TYPE c,
        rec2_var2    TYPE i,
      END OF rec2.

*--------------------------------------------------------------------*
*         using MOVE statement
*--------------------------------------------------------------------*

*rec2 = rec1.

MOVE rec1 TO rec2."Functionally equivalent to rec2 = rec1

*--------------------------------------------------------------------*
*         displaying data
*--------------------------------------------------------------------*

WRITE: rec2-rec2_var1,
    / rec2-rec2_var2. 
*--------------------------------------------------------------------*
The output of above program is as follows:-
ABC
100

2. MOVE-CORRESPONDING statement

The MOVE-CORRESPONDING statement is used to assign values between the components of 2 structures. The syntax for the MOVE-CORRESPONDING statement is as follows:-

MOVE-CORRESPONDING <struct1> TO <struct2>.

The contents of components of <struct1> structure are copied to the components of <struct2> structure, with identical names.

Following programs demonstrate the use of MOVE-CORRESPONDING statement:-

Program 1:
REPORT zmove_corres_demo.

*--------------------------------------------------------------------*
*           data declarations
*--------------------------------------------------------------------*

DATA: BEGIN OF struct1,
        var1 TYPE i VALUE 50,
        var2 TYPE c VALUE 'A',
      END OF struct1.

DATA: BEGIN OF struct2,
        var1 TYPE i,
        var2 TYPE c,
        var3,
      END OF struct2.

*--------------------------------------------------------------------*
*   using MOVE-CORRESPONDING statement
*--------------------------------------------------------------------*

MOVE-CORRESPONDING struct1 TO struct2.

*-------------------------------------------*
*     displaying data
*-------------------------------------------*

WRITE: struct2-var1,struct2-var2.
*--------------------------------------------------------------------*
The output of above program is as follows:-
50 A

Program 2:
REPORT zmove_corres_demo1.

*--------------------------------------------------------------------*
*           data declarations
*--------------------------------------------------------------------*

DATA: BEGIN OF struct1,
        var1 TYPE i VALUE 50,
        var2 TYPE c VALUE 'A',
      END OF struct1.

DATA: BEGIN OF struct2,
        var1(4) TYPE p DECIMALS 2,"data type of var1 is p whereas of var1 is i.
        var2 TYPE c,
        var3,
      END OF struct2.

*--------------------------------------------------------------------*
*   using MOVE-CORRESPONDING statement
*--------------------------------------------------------------------*

MOVE-CORRESPONDING struct1 TO struct2.

*--------------------------------------------------------------------*
*     displaying data
*--------------------------------------------------------------------*

* source data is converted into target format
WRITE: struct2-var1,
       struct2-var2.
The output of above program is as follows:-
50.00 A

3. WRITE TO statement

WRITE TO statement converts the content of source field to type c and then place the resulting string into the target field. The syntax for WRITE TO statement is as follows:-

WRITE <f1> TO <f2>.

In above syntax, <f1> is the source field and <f2> is the target field. Statement converts the content of <f1> into a string and then stores the string in <f2>.

Following program demonstrates the use of WRITE TO statement:-
REPORT zwrite_to_demo.

*--------------------------------------------------------------------*
*             data declarations
*--------------------------------------------------------------------*

DATA: source1(4) TYPE c VALUE 'aman',
      source2    TYPE i VALUE 50,
      target1(4) TYPE c,
      target2(2) TYPE c.

*--------------------------------------------------------------------*
*               using WRITE TO statement
*--------------------------------------------------------------------*

WRITE source1 TO target1.
WRITE source2 TO target2.
*--------------------------------------------------------------------*
*               displaying data
*--------------------------------------------------------------------*

WRITE: 'source name:',source1.
WRITE: / 'target name:',target1.
WRITE: / 'source number:',source2.
WRITE: / 'target number:',target2.
*--------------------------------------------------------------------*

The output of above program is as follows:-
source name: aman 
target name: aman
source number:  50
target number: 50
Note:

The data type of <f1> must be convertible into a character field. If it is not convertible, a syntax or runtime error occurs. The contents of <f1> remain unchanged, but the content of <f2> is always regarded as a character string. So, target field must be of type c. otherwise, syntax error occurs.

4. CLEAR statement

The CLEAR statement is used to reset the value of a variable. It means it resets the value of variable to initial value. Initial value of the variable depends on the data type of variable. For example, variable of type C is set to null and variable of type I is set to zero.

The syntax for CLEAR statement is as follows:-

CLEAR <F>.

Note:

Initial value does not mean the starting value, which is set using VALUE option of the DATA statement.

IMPACT OF CLEAR STATEMENT ON DIFFERENT DATA TYPES
Data Type
Impact of clear statement
Elementary Data Type
Resets value to initial value.
Reference
Resets a reference variable to its initial value so that it does not point to any object.
Structure
Resets the individual components of a structure to their respective initial values.
Internal Table
Deletes the entire contents of the internal table.

Following program demonstrates the use of CLEAR statement:-

REPORT zclear_demo.

*--------------------------------------------------------------------*
*               data declarations
*--------------------------------------------------------------------*

DATA: var1    TYPE i VALUE 5,
      var2    TYPE c VALUE 'A',
      var3(3) TYPE p DECIMALS 2 VALUE '3.14'.

*--------------------------------------------------------------------*
*      displaying data before using CLEAR
*--------------------------------------------------------------------*

WRITE: 'BEFORE CLEARING VARIABLES'.
WRITE: / 'var1:',var1.
WRITE: / 'var2:',var2.
WRITE: / 'var3:',var3.

*--------------------------------------------------------------------*
*       using CLEAR statement
*--------------------------------------------------------------------*

CLEAR: var1,
       var2,
       var3.

*--------------------------------------------------------------------*
*     displaying data after CLEAR
*--------------------------------------------------------------------*

WRITE: / 'AFTER CLEARING VARIABLES'.
WRITE: / 'var1:',var1.
WRITE: / 'var2:',var2.
WRITE: / 'var3:',var3. 
*--------------------------------------------------------------------*

The output of above program is as follows:-
BEFORE CLEARING VARIABLES
var1: 5
var2:    A
var3:    3.14
AFTER CLEARING VARIABLES
var1:  0
var2:
var3: 0.00

Thursday, 14 May 2015

Chapter 1 - Data types and Definitions

1. ABAP statements

  • They consist of command, any variables and options.
  • They end with a period (.).
  • There is no restriction on the layout of statements in ABAP. We can place multiple statements on a single line or stretch a single statement across multiple lines.
  • Statements can begin anywhere on the line. ABAP does not care where a statement begins on a line. We should take advantage of this fact to improve the readability of our program by using indentation to indicate blocks of code.
  • We can place blank lines anywhere in the program. Blank lines should be used to separate blocks of code.
    WRITE 'statement 1'. WRITE 'statement 2'.
          WRITE 'statement can begin anywhere on the line'.

2. Commenting code

  • Comments are those statements that are not processed by the system.
  • Programmers use comments to document the purpose of various statements in a program.
  • Comments play a very important role as they can save time and money when the code needs to be modified.
  • It is the responsibility of the programmer to leave behind well documented code.
  • Most companies do not consider a programming job complete until it is fully documented.
  • Comments in ABAP are of 2 types:

    1. Full-line comments


    They are created by placing an asterisk (*) at the first position of the line. In this case, entire line is considered to be a comment by the system. They do not need to be terminated by the period because they may not extend across more than one line:

    * display balance amount on the screen
    WRITE balance_amt.

    2. Partial-line comments


    They are created by placing a double quote (“) after a statement. All text following the double quote is considered to be a comment by the system. They do not need to be terminated by the period because they may not extend across more than one line:

    WRITE balance_amt. "display balance amount on the screen

3. Colon notation

  • This notation is used to chain the consecutive statement if the beginning of each statement is identical.
  • This notation saves the programmer’s time spent in repetitive typing. This notation uses colon (:) operator and comma (,).
  • Commas are used to end individual statements like periods end normal statements.
  • The final identical statement is terminated with a period (.).
  • Consider the following code :
    WRITE 'Welcome'.
    WRITE user_name.
    WRITE 'to the ABAP world'.
    
    
    Using colon notation, it could be rewritten as :
    WRITE: 'Welcome',
            user_name,
           'to the ABAP world'.
    
    Above statement can also be written as :
    WRITE: 'Welcome', user_name, 'to the ABAP world'.
  • Consider the following code :
    MOVE sy-subrc TO lv_one.
    MOVE sy-subrc TO lv_two.
    MOVE sy-subrc TO lv_three.
    
    
    Using colon notation, it could be rewritten as :
    MOVE sy-subrc TO: lv_one,
                      lv_two,
                      lv_three.
    
    

4. Defining Programs(Report)

Report is the first non-comment line in the program. It names the program and can also be used to format the output. Following is the syntax for the report statement:
REPORT program_name
       <NO STANDARD PAGE HEADING>
       <LINE-SIZE number_of_columns>
       <LINE-COUNT number_of_lines[(number_of_lines_reserved_for_footer)]>
       <MESSAGE-ID message_id>.
SAP automatically generates the REPORT statement whenever a new program is created, although it has no options.

These options are described below:
  • <NO STANDARD PAGE HEADING>
    It disables the default page heading generated by SAP. It allows the programmer to define their own page header.
    REPORT ZTEST NO STANDARD PAGE HEADING.
  • <LINE-SIZE number_of_columns>
    It defines the size of line(maximum number of characters that a line can contain). The size can be upto 255 characters.
    REPORT ZTEST LINE-SIZE 40.
  • <LINE-COUNT number_of_lines[(number_of_lines_reserved_for_footer)]>
    It defines number of lines for each page of the output. The second option is meant for reserving the specific number of lines for footer. If we want a page footer, we must reserve the space for the footer using this option because SAP does not provide default page footer.
    REPORT ZTEST LINE-COUNT 30(2).
  • <MESSAGE-ID message_id>
    It defines the set of messages that can be used in the program when the MESSAGE command is used.
    REPORT ZTEST MESSAGE-ID Z1.

    Example:
    REPORT ZTEST
    NO STANDARD PAGE HEADING
    LINE-SIZE 40
    LINE-COUNT 30(2)
    MESSAGE-ID Z1.

5. Variables

Like other programming languages, ABAP also allows programmers to use variables in a program. A variable is a symbolic reference to a memory location that stores data temporarily.

They persist only during the execution of the program in which they are declared and the data they contain is lost when the execution of program is completed.

Consider the following rules when using variables in your program:
  1. All variables must be declared before they are used in the program.
  2. Variables can be defined at any point in a program but it is a good practice to define variables at the starting of the program.
  3. A variable name can be up to 30 characters in length. Variable name should clearly specify the function of variable.
  4. A variable name should begin with a letter and then any mixture of letters and numbers. It should not contain special symbols(*,%,<,>,? Etc.). An underscore can be used to separate different words in a variable.
  5. ABAP keywords cannot be used as a variable name.
  6. All variables defined at the program level are considered global and are visible to all subroutines defined in that program. Whereas variables defined within a subroutine are visible only to that subroutine.

Predefined datatypes in ABAP:

There are 2 main categories of datatypes in ABAP: character and numeric

1. Character data types-


Data type
Description
Default Internal Length
Maximum
Internal
Length
Valid
Values
Default Initial
Value
c
Character
1
65535
Any char
Blank
n
Numeric text
1
65535
0-9
0
d
Date
8(fixed)
-
0-9
00000000
t
Time
6(fixed)
-
0-9
000000
x
Hexadecimal
1
65535
Any


Note:
  • Internal length is given in bytes.
  • A dash is given in maximum internal length field for the fixed length datatypes.
  • Default initial value indicates the value assigned to the variable by default. This default value is specified using the ‘Value’ addition (discussed below).
  • Date and time are fixed length data types. We should not specify the length for date and time variables while declaring them using data statement (discussed below).
  • Values for data and time variables are stored internally as YYYYMMDD and HHMMSS respectively.

2. Numeric data types-


Data type
Description
Default Internal Length
Maximum
Internal
Length
Maximum
Decimals
Default Initial
Value
i
Integer
4(fixed)
-
0
0
f
Floating-point
8
8
15*
0.0
p
Packed decimal
8
16
14
0
Note:
  • A dash is given in maximum internal length field for the fixed length datatypes.
  • An asterisk(*) indicates that attribute is machine dependent.

Defining variables using DATA statement:

Variables can be defined using data statement as follows-
  1. DATA  var<(length)>   <TYPE type>   <DECIMALS number>   <VALUE intial_value>
  2. DATA  var  LIKE table-field   <VALUE initial_value>
Note:
  • If no length is specified, the variable uses default length for that datatype.
  • If no datatype is specified, the variable defaults to character(c) datatype.
Form 1: DATA  var<(length)>   <TYPE type>   <DECIMALS number>   <VALUE intial_value>

This form explicitly defines the datatype and length of variable. This form should be used when variable is not dependent on the values stored in the SAP database such as counters or flags.

Example: DATA: unit_price(5) TYPE F DECIMALS 2 VALUE ‘1.00’.

This statement creates a variable named ‘unit_price’ which is a floating point of length 5 with 2 spaces reserved for decimals and a default value of 1.00.

Note:

Decimal value must be surrounded by single quotes in ABAP as in above example just like the string is surrounded by single quotes. Numbers without a decimal can be written without the single quotes.

Form 2: DATA  var  LIKE table-field   <VALUE initial_value>

In this form, length and datatype of variable is determined dynamically at runtime. The sap system looks for the datatype and length of the specified field and then it uses this information to define the variable.

Example:

DATA: pur_docno  LIKE  EKKO-EBELN  VALUE  450000000.  ”EBELN is a field in table EKKO for storing Purchasing Document number

This statement creates a variable pur_docno that has the same datatype and length as the field EBELN with a default value of 4500000000.

Record

A record is a group of several variables. It is used when we have a group of related data that needs to be kept together. A record is declared by using a group of data statements. Variables in a record are referenced as record_name – field_name.

Following is the syntax for declaring variables:-

DATA BEGIN OF record_name.
DATA field_1.
DATA field_2.
.
.
DATA field_N.
DATA END OF record_name.

The individual fields within the record can be defined using any form of DATA statement as in the following example:-

DATA BEGIN OF purchase_order.
DATA po_number LIKE EKPO-EBELN.
DATA item_number LIKE EKPO-EBELP.
DATA short_text LIKE EKPO-TXZ01.
DATA flag(1) TYPE C VALUE 'N'.
DATA item_price(9) TYPE p DECIMALS 2.
DATA END OF purchase_order.

Note:
  • EKPO is a standard table in SAP for storing information regarding items in a purchase order.
  • EBELN is a field in EKPO for storing purchasing document number.
  • EBELP is a field in EKPO for storing purchasing document item number.
  • TXZ01 is a field in EKPO for storing short text of item.
In above example, if you have to access the field “po_number”, then you have to use the notation: purchase_order-po_number.

With colon notation, the above example can be rewritten as follows:

DATA: BEGIN OF purchase_order,
      po_number LIKE EKPO-EBELN,
      item_number LIKE EKPO-EBELP,
      short_text LIKE EKPO-TXZ01,
      flag(1) TYPE C VALUE ‘N’,
      item_price(9) TYPE P DECIMALS 2,
      END OF purchase_order.

Now you observe that readability of the code is improved.

Defining new data types using TYPES statement:

In addition to 8 predefined data types(C,D,T,X,P,F,I,N) in ABAP,we can create new data types in ABAP using the “types” statement. New data types are created to meet the business needs and are called as “user defined types”.

Suppose a financial program needs to make a reference to bank account numbers. As we know there are several types of bank account numbers like customer bank account number, merchant bank account number etc.

So, separate variables can be declared for these bank account numbers as follows:
DATA: CUST_ACCNO(18) TYPE N,
      MERC_ACCNO(18) TYPE N.

In this example, we have defined 2 variables CUST_ACCNO and MERC_ACCNO in terms of standard ABAP ‘numeric’ data type. Instead, we could define these variables in terms of our own data type say BANK_ACCOUNT.

TYPES: BANK_ACCOUNT(18) TYPE N.
 DATA: CUST_ACCNO TYPE BANK_ACCOUNT,
       MERC_ACCNO TYPE BANK_ACCOUNT. 

In this case, if the length of bank account number gets changed from 18 to 25. Then, we would need to modify only the BANK_ACCOUNT variable not the CUST_ACCNO and MERC_ACCNO variables.

“Types” statement is used to declare new types and has 3 possible forms:-
1. TYPES name<(length)> <TYPE type> <DECIMALS number>
2. TYPES name LIKE table-field
3. TYPES BEGIN OF record_type.  
        ........
        TYPES END OF record_type.

Form 1: TYPES   name<(length)>   <TYPE type>   <DECIMALS number>

The syntax of TYPES statement is similar to the data statement. Form1 defines a data type explicitly. This statement declares only a data type not a variable. We still need to declare the variable by using the DATA statement.

Example:
TYPES NAME_TYPE(15) TYPE C.
TYPES PRICE_TYPE(8) TYPE P DECIMALS 3.
TYPES QTY_TYPE TYPE I.

Using colon notation, it can also be written as:

TYPES: NAME_TYPE(15) TYPE C,
       PRICE_TYPE(8) TYPE P DECIMALS 3,
       QTY_TYPE TYPE I.


Form 2: TYPES   name   LIKE   table-field

In this form, the data type and length is determined dynamically at runtime. The system determines the data type and length of the specified field of table. Then system uses this information to define the data type.

Example:

TYPES FLAG_TYPE LIKE SY-SUBRC.
TYPES TEXT_TYPE LIKE EKPO-TXZ01.

Above example creates 2 user-defined data types. FLAG_TYPE whose length and data type is similar to that of SY-SUBRC. TEXT_TYPE whose length and data type is similar to that of TXZ01 field in table EKPO.

Note: SY-SUBRC is a system field in SAP for storing the return code of the statement.
Form 3:

 TYPES BEGIN OF record_type.  
  ........
  TYPES END OF record_type.


A record type is similar to the variable record. A variable record is a group of several variables. Similarly, a record type is a group of several data types. Using colon notation, the syntax can also be written as:

TYPES: BEGIN OF record_type,
 .....
        ..... 
 END OF record_type.

Example:

TYPES: BEGIN OF PART_LIST,
       PART_NAME(20) TYPE C,
       MATERIAL_CODE LIKE EKPO-MATNR,
       DESC LIKE EKPO-TXZ01,
       PRICE TYPE P DECIMALS 2,
       END OF PART_LIST.

The above example creates a user-defined record type PART_LIST with types: PART_NAME of length 20 and data type C, MATERIAL_CODE with length and data type similar to that of MATNR field in table EKPO and DESC with length and data type similar to that of TXZ01 field in table EKPO. A variable record can be created from above record type as follows:-

DATA: OLD_PART TYPE PART_LIST,
      NEW_PART TYPE PART_LIST.

Defining constant using CONSTANTS statement:

Constants are those data elements whose value does not change throughout the execution of program. Their value remains fixed. They must be assigned an initial value when they are declared and this initial value never changes. If you try to change the value of a constant, a syntax error or runtime occurs.

Constants are defined using CONSTANTS statement. The syntax of CONSTANTS statement is similar to that of the DATA statement, with an exception that “VALUE” addition is always required and “OCCURS” option is never allowed.

The syntax for CONSTANTS statement is as follows:-

1. CONSTANTS  name<(length)>  <TYPE type>  VALUE value  <IS INITIAL>
2. CONSTANTS name LIKE table-field VALUE value <IS INITIAL>
3. CONSTANTS: BEGIN OF rec,
   .........
   END OF rec.

Example:

CONSTANTS PI(3) TYPE P DECIMALS 2 VALUE ‘3.14’.
CONSTANTS C1 TYPE I VALUE IS INITIAL.
CONSTANTS ITEM_NUM LIKE EKPO-EBELP VALUE 45000000.
CONSTANTS: BEGIN OF raj_address,
           name(10) TYPE C VALUE ‘RAJ SHARMA’,
           street(20) TYPE C VALUE ‘HT PARK ROAD’, 
           city(10) TYPE C VALUE ‘KASHIPUR’,
           postal_code(6) TYPE N VALUE ‘244713’,
           END OF raj_address.

The components of this constant can be accessed as raj_address-name, raj-address-street and so on, but they cannot be changed.

6. Using Parameters:

Parameters are used to obtain data from the user and pass that data to the ABAP program when the program is executing. There are 2 types of parameters in ABAP. One parameter allows user to enter a single value whereas second parameter allows user to enter range of values.

Type1: Accepting Single value from user using Parameters statement

The PARAMETERS statement is used to define parameter for accepting single value from the user. It has the following syntax:-

PARAMETERS  para_name<(length)>  <TYPE type>  <LIKE field>  <DEFAULT val>  <LOWER CASE>  <AS CHECKBOX>  <RADIOBUTTON GROUP num>  <OBLIGATORY>.

Like variables, data type of PARAMETERS can be specified by using TYPE or LIKE options. But unlike the variables, a parameter can have up to 8 characters or digits. A parameter cannot use the DECIMALS option. Also, a parameter cannot be part of a record.

The description of various options used in parameters statement is as follows:-
  • ‘DEFAULT val’ option is used to assign a default value to the parameter just like the VALUE option of the DATA statement. This default value is seen by the user at the runtime and can be overwritten by the user.
    PARAMETERS: p1 type i DEFAULT 100.
  • ‘LOWER CASE’ option allows entry of lower case data. If this option is not specified, all data is converted into uppercase.
  • ‘AS CHECKBOX’ option is used only with a parameter one character in length. This option creates a push button that the user can click to be on or off. If the user clicks the push button to ON, then the parameter is set to ‘X’.
  • ‘RADIOBUTTON GROUP’ option is used only with a parameter one character in length just like the AS CHECKBOX option. It also creates a push button that the user can click to on or off. The difference is several push buttons can be grouped together and the user can select one of them.
  • ‘OBLIGATORY’ option forces user to enter a value. The program does not execute until the user specifies a value.
Examples of parameters statement:-

PARAMETERS P_NAME1(20TYPE OBLIGATORY"defines a mandatory field
PARAMETERS P_NAME2(20TYPE C"defines a normal field






Notice the symbol in P_NAME1 field. It indicates an obligatory field. If you try to execute the program without entering data in P_NAME1 field, you get the following message:-



The above statements creates an obligatory parameter named P_NAME1 of length 20 and data type character and another parameter P_NAME2 of length 20 and data type character. The user must enter a value in P_NAME1 in order to execute the program.

PARAMETERS P_FILENM(30) TYPE C DEFAULT 'file1.txt' OBLIGATORY LOWER CASE.

The above statement creates a parameter named P_FILENM of length 30, data type character, default value file1.txt. The parameter allows only lowercase letters and must be filled in before the program will execute.



PARAMETERS: P_CHECK1 AS CHECKBOX DEFAULT 'X',
            P_CHECK2 AS CHECKBOX,
            P_CHECK3 AS CHECKBOX.


The above set of statements creates 3 checkboxes out of which P_CHECK1 is set to ON by default. The user choose either of them or all of them.


PARAMETERS: P_MALE RADIOBUTTON GROUP 001, P_FEMALE RADIOBUTTON GROUP 001 DEFAULT ‘X’. 

The above set of statements creates 2 radio buttons out of which P_FEMALE is set to ON by default. The user choose anyone out of these 2.

radiobutton


Type2: Accepting multiple values from user using SELECT-OPTIONS statement

SELECT-OPTIONS statement creates 2 input fields that allows user to enter range of values. The syntax of SELECT-OPTIONS statement is as follows:-

SELECT-OPTIONS <so_name> FOR <table-field>. "This creates 2 input fields with extension.
SELECT-OPTIONS <so_name> FOR <table-field> NO INTERVALS."This creates 1 input field with extension
SELECT-OPTIONS <so_name> FOR <table-field> NO EXTENSION."This creates 2 input fields without extension
SELECT-OPTIONS <so_name> FOR <table-field> NO INTERVALS NO-EXTENSION."This creates one input field without extension

Some examples of SELECT-OPTIONS statement are as follows:-

1)TABLES: MARA. 
  SELECT-OPTIONS S_MATNR FOR MARA-MATNR. 

The above statement creates a select-option with intervals and extension.


Note:

While using SELECT-OPTIONS statement, TABLES statement is used to mention the name of table for which you are creating select-option.

2)TABLES: MARA. 
  SELECT-OPTIONS S_MATNR1 FOR MARA-MATNR NO INTERVALS.

The above statement creates a select-option without intervals.


3)TABLES: MARA.
SELECT-OPTIONS S_MATNR1 FOR MARA-MATNR NO-EXTENSION.

The above statement creates a select-option without extension.


4)TABLES: MARA.
  SELECT-OPTIONS S_MATNR FOR MARA-MATNR NO INTERVALS NO-EXTENSION.

The above statement creates a select-option without intervals and without extensions.


7. Field-Symbols

The final type of variable is called a field symbol. It is different from all the previous types of variables that we have discussed because unlike other variables, a field symbol does not actually hold the data. It refers to the variables declared using DATA or PARAMETERS statement. A field symbol is similar to the C pointers. The name of the field symbol must always be enclosed in angle brackets. Hence, if the name of the field symbol is FS1, then it would appear in the program as <FS1>.

Following is the syntax for declaring the field symbols:-

FIELD-SYMBOLS <FS>.

The field symbols does not care what kind of variable(i,c,d,p and so on) it points to.
The “ASSIGN” command is used to assign a variable to a field symbol.

Note:

When you assign a variable to a field symbol, the field symbol inherits its technical attributes. The data type of the assigned data object becomes the actual data type of the field symbol i.e. if you assign an integer type variable to a field symbol, the field symbol will also have an integer data type.

Example:

DATA: var1 TYPE P DECIMALS 2 VALUE ‘3.56’,
      var2(6) TYPE C VALUE ‘PRASAD’,
      var3 TYPE I VALUE 50.
FIELD-SYMBOLS <fs>.
ASSIGN var1 TO <fs>.
WRITE: var1.
ASSIGN var2 TO <fs>.
WRITE: var2.
ASSIGN var3 TO <fs>.
WRITE: var3.

Output:

3.56 PRASAD 50
Continue...