02 Basicabap
02 Basicabap
You get a feel of ABAP/4 Programs with Basics covered in detail. In the Program Data Objects, Data type, Case , Write and use of Tables statement. You can see the use of all the statements and control words in the given example. Output can be seen with a write statement in the end. You can also see the use of Predefined Data type and reference type. You see creation of reports in the program.
ABAP/4 Programs are objects of the R/3 Repository, and therefore maintained just like any other Repository objects (example ABAP/4 Dictionary Tables or user interface screens) using a tool of the ABAP/4 workbench, the ABAP/4 Editor. To start the ABAP/4 Editor to create or change ABAP/4 Programs, R/3 System offers three possibilities:
Open programs in object browser Open programs using the ABAP/4 Editor Open Programs via forward navigation
Displaying or Changing an existing program: 1) The browser displays the overview of all the components of a program
2) Position the cursor on the program name and choose display or change or double click.
To open ABAP programs directly using the ABAP Editor, choose ABAP Editor in the ABAP Workbench screen or start Transaction SE38, and enter a program name.
Whenever you work with a tool of the ABAP/4 Development Workbench and you position the cursor on a name of an R/3 Repository object and select the object the system opens the object together with the corresponding tool.
This procedure is suited whenever ABAP/4 programs are called from within objects such as screen flow logic or from within ABAP/4 Program.
Naming Conventions
A) Reports (Stand-alone programs):
Customer reports must follow this naming convention:Yaxxxxxx or Zaxxxxxx. Replace a with the short form of the corresponding application area. Replace x with any valid character. Sap reports adhere with the similar naming convention: Raxxxxxx. B) Module pool programs (for transactions): Customer Dialogue programs must follow this naming convention: SAPMYxxx or SAPMZxxx. Replace x with any valid character. SAP standard programs adhere to a similar naming convention: SAPMaaxxx, where a is the application area. C) Valid Characters: The name consist of at least one character,can have up to 8 characters. Do not use following characters: Period(.) equal sign(=) double quote() Comma(,) blank ( ) asterisk(*) parenthesis ( ) percent(%) and underscore(_)
ABAP Syntax
The syntax of the ABAP programming language consists of the following elements:
Statements Keywords Comments Statements: An ABAP program consists of individual ABAP statements. Each statement begins with a keyword and ends with a period. Keywords: A keyword is the first word of a statement. It determines the meaning of the entire statement.
Comments:
Comments are text elements which you can write between the statements of your program to explain its purpose to a reader.
Types of Keywords
Declarative keywords: These keywords define data types or declare data objects. Example: TYPES, DATA, TABLES
Modularization keywords: These keywords define the processing blocks in an ABAP program. The modularization keywords can be further divided into: * Event Keywords: Example: AT SELECTION SCREEN, START-OF-SELECTION, AT USER-COMMAND. * Defining keywords: Example: FORM ..... ENDFORM, FUNCTION ... ENDFUNCTION, MODULE ... ENDMODULE.
Keywords
Control keywords: You use these keywords to control the flow of an ABAP program within a processing block. Example: IF, WHILE, DO, FOR, CASE. Calling keywords: You use these keywords to call processing blocks that you have already defined. Example: PERFORM, CALL, SET USER-COMMAND, SUBMIT, LEAVE TO Operational keywords: These keywords process the data that you have defined using declarative statements. Example: WRITE, MOVE, ADD.
Comments:
Comments are text elements which you can write between the statements of your ABAP/4 Program to explain its purpose to the reader.Comments are flagged by special characters which causes system to ignore them. You should comment the document your program internally. Comments help other users to understand or change the program.
Structure of comments: There are two ways to insert comment in the program A) For entire line to be comment enter (*) at the beginning of the Line. B) If you want to enter part of line as comment, enter a double quotation mark before the comment.
Syntax Structure
An ABAP program consists of different statements which have a particular structure. Each statement begins with a keyword and ends with a period. Example:
Formatting ABAP Statements: ABAP has no format restrictions. You must separate words within a statement with at least one space. You can write several statements on one line, or spread a single statement over several lines. Chained Statements: You can concatenate consecutive statements with an identical first part into a chain statement by writing the identical part only once and placing a colon (:) after it. Example: WRITE: SPFLI-CITYFROM, SPFLI-CITYTO, SPFLI-AIRPTO.
Report programs: A report program generates a list from database tables in a user defined format . It does not alter the data in the database but only analysis(reads) them.The results which can be displayed on the screen or sent to a printer. A report program can either be an online or background program.
Dialog programs Dialog programs read and change database tables. They are also called as Module pool programs. Dialog programs accept user information,process the information and update the database. For this reason module pool programs cannot be executed in background.
Workbench Tools
The ABAP/4 development workbench contains tools you need to create and maintain ABAP/4 programs. Object browser: This utility offers a hierarchical overview of all R/3 repository objects. ABAP/4 Dictionary: The ABAP Dictionary centrally describes and manages all the data definitions used in the system. ABAP/4 Editor: You use the ABAP Editor to create and edit your programs and their components.
Workbench tools
Function library: The function library allows you to create and maintain the function modules. Screen painter: This ABAP Workbench tool allows you to create screens for your transactions. SAP R/3 provides two modes (graphical and alphanumeric modes) of the Screen Painter. Menu painter: The Menu Painter is a tool with which you design user interfaces.
Data Types
The following list is an overview of the main features of data types and objects: Data types: A data type describes the technical attributes of all the objects with that type. There is no memory associated with data types.
Data types Elementary Predefined C,D,F,I,N,P,T,X ABAP/4 Contains eight predefined elementary data types User-defined User defined elementary data typesare based on the predefined Elementary data types Field Strings and internal tables : These structured data types can be used for data objects and are user defined.
Structured
TABLE : This predefined structured data type is used for the typing of formal parameters and field symbols Reference types describe data objects You will have to create your own that contain references (pointers) to references other objects (data objects and objects in ABAP Objects).
Reference
Variable-Length Elementary Types: There are two predefined types in ABAP with variable length: STRING for character strings XSTRING for byte strings
User-defined: elementary data types are based entirely on predefined elementary data types. To define your own elementary data types, you use the TYPES statement Example: TYPES: NUMBER TYPE I. DATA: NO_FLIGHTS TYPE NUMBER
more reliable
To improve the quality of your programs, use the following Techniques: Indenting statement blocks Using Modularization Tools Inserting Program comments
To produce good programs you should use modularization tools. If you write larger processing blocks as subroutines, the logical structure of your program becomes easier to identify.It also allows you to sort the subroutines according to the tasks they perform. Inserting Program comments Correctly
You should avoid placing comments on statement line. Placing them on separate comment lines improves the readability of the program.To insert subroutine headings and comments in your program use the ready-made structures available in ABAP/4 Editor. Pretty Printer: The ABAP/4 Editor includes a tool which helps you to design the Layout of your program more easily. It follows ABAP/4 Layout guidelines
B) Inserting Ready-Made Comment Lines: To insert ready-made comment lines into your code, proceed as follows: 1.Follow steps 1 to 2 in inserting Ready-Made keyword structures. 2.Select a structure with an asterisk(*) as a first structure from the other instruct. field. 3.The system inserts comment lines into your program
Data Objects
Data objects contain the data with which ABAP programs work at runtime. ABAP contains the following kinds of data objects: A) Literal They are not created by declarative statements. Instead, they exist in the program code. They have fixed technical attributes but no name. Hence they are also referred to as unnamed data objects. B) Named Data Objects You declare these data objects either statically or dynamically at runtime. Their technical attributes are always fixed. ABAP contains the following kinds of named data objects:
Data Objects
Text symbols are pointers to texts in the text pool of the ABAP program. Variables are data objects whose contents can be changed using ABAP statements. Constants are data objects whose contents cannot be changed. Interface work areas are special variables that serve as interfaces between programs, screens, and logical databases. Predefined Data Objects They do not have to be declared explicitly - they are always available at runtime. Dynamic Data Objects You create them dynamically using data references. They do not have a name.
Data Objects
External data objects
External data objects exist independent of programs. You cannot work directly with them, but you can copy them to internal data objects and write them back when you have finished. External data objects can be used globally throughout the system environment. ABAP/4 stores external data objects in tables defined in the ABAP/4 Dictionary. To access this data from within a program, you declare the tables in the program with the TABLES statement .
ABAP/4 includes some data objects with special features, namely: Parameters
Parameters are variables which are linked to a selection screen. They can accept values after a program is started. Selection criteria
Selection criteria are special internal tables used to specify value ranges. They are linked to a selection screen.
Type and like are optional additions to most of the data declaration statements listed below.
With the TYPE option, you assign the data type<datatype>directly to the declared data object.
With the like option,you assign the data type of another data object<data object> to the declared data object.
In the context of Internal Tables, you use the operational statements APPEND,COLLECT and INSERT to create lines of an Internal Tables dynamically. In case of selection screens,you use the additional statements PARAMETERS SELECT-OPTIONS to create Data objects with special function
Data Statement Syntax DATA <f>[(<length>)] <type> [<value>] [<decimals>]. In its basic form, the keyword DATA has the following parameters: <f> <length> <type> <value> <decimals> Naming a Variable Specifying the Data Type and the Length of the Variable Specifying a Start Value Specifying the Number of Digits after the Decimal Point
TYPE GROUPS
You use Type-Groups to store user-defined data types or constants in the ABAP/4 Dictionary for cross program use.In your ABAP/4 program, you declare type groups with the TYPE-POOLS statement as:
Syntax: Type-Pools<name>
To determine the number of decimals for a Type P field, you use the DECIMALS parameter with the DESCRIBE FIELD statement as follows:
Syntax: Describe Field<f>EDIT MASK<m> If a conversion routine exists for the field <f> in the ABAP/4 Dictionary, the system writes it to the field <m> and sets the return code value in the System Field SY-SUBRC equal to 0. You can then use the field <m> directly as a format template in a write statement , as below:
Write<f>using EDIT MASK<m>
Naming a Variable
The variable name <f> may be up to 30 characters long. You can use any alphanumeric characters except those listed below. Do not use the following characters: plus sign + period . comma , colon : parentheses ( )
Syntax: WRITE<f>.
This statement outputs the field <f> to the current list in its standard output format.
The field <f> can be: A) Any Data Object B) A Field Symbol C) A Text Symbol
To write a value (literal) or the contents of a source field to a target field, you use the WRITE TO statement: Syntax WRITE <f1> TO <f2> [<option>]. Example DATA: NUMBER TYPE F VALUE '4.3', TEXT(10). WRITE NUMBER TO TEXT EXPONENT 2. WRITE / TEXT.
You can position the output of a write statement on the screen by making a format specification before the field name as follows: Syntax WRITE AT [/][<pos.>][(<len>)] <f>. A) the slash / denotes a new line. B) <pos.> is a number or variable up to three digits long denoting the position on the screen. C)<len> is a number or variable up to three digits long denoting the output length.
Formatting options
You can use formatting options with the write statement.
Syntax: Write..<f><option> Option LEFT-JUSTIFIED CENTERED RIGHT-JUSTIFIED UNDER <g> NO-GAP Purpose Output is left-justified. Output is centered. Output is right-justified. Output starts directly under the field <g>. The blank after the field <f> is omitted.
Formatting options for numeric fields NO-SIGN DECIMALS <d> EXPONENT <e> The leading sign is not output. <d> defines the number of digits after the decimal point. In type F fields, the exponent is defined in <e>.
Starting on current line , this statement generates <n> blank lines on the output screen.
To position the output on a specific line on the screen use: Syntax: SKIP TO LINE <n>
You can output the first character of a field as a checkbox on the output screen by using the following syntax: WRITE <f> AS CHECKBOX.
In the first character of the field <f> is an X, the checkbox is displayed filled. If the first character is a SPACE, the checkbox is displayed blank. With other words user can fill or empty the checkbox by mouse clicks.
Selection Texts
Replace the standard Text: You can replace the standard texts that appear for parameters and selection criteria on the selection screen by text elements. A)To change the text on the selection screen,select Selection texts from the ABAP/4 Text elements screen and choose change. B)On the following screen,the column name contains the names of parameters and selection criteria of your program. Now you can enter a selection text up to 30 characters for each parameter and selection criterion.
Text Symbols
Text symbols are text constants which you enter and maintain outside a program.You should use text symbols instead of text literals in the final version of your program to keep it language-independent and easy to maintain.
Maintain Text symbols: To create or change text symbols , select Text symbols on the ABAP/4 Text Elements screen and choose change.For each text symbol,you must specify a three-character identifier which contains no blanks and does not begin with the character %.You can assign a text up to 132 character to each text symbol.You specify 3 character identifier in column Sym and the text in column Text. Using Text Symbols in ABAP/4 Programs: You use text symbols exactly as literals in your ABAP/4 programs. At each position in a statement where you can write a literal, you can write also a text symbol. If the text symbol<idt> does not exist in the text pool, the system treats TEXT<idt>like the constant SPACE. Syntax: TEXT-<idt>...
B)Comparing Selection Texts: When you choose Selection Texts and compare the system supports you by finding missing or unused selection texts.
If you insert new text symbols or change existing ones in the program code. These text symbols are not automatically copied to the Text Pool.To update this list and to eliminate any error,choose text symbols and the function compare on the ABAP/4 Text Elements Screen.
Arithmetic operations
ABAP/4 supports the four basic arithmetic operations, as well as power calculation. You can specify the following arithmetic operators in a mathematical expression:
+ * / DIV MOD **
Addition Subtraction Multiplication Division Integer division Remainder of integer division Exponentiation
SUMMARY
You must have understood the basics after doing above exercises. Practice more and you will be able to command on the language the most. ABAP/4 Basics is sufficiently covered in the above presentation.
Example 1
Name of your report: ZSHU017 ID Number : TASK : Create the List as per the output below: Output: 10 20 30 40 50 1 2 3 4 5 XX BB XX DD XX 1 2 3 4 5
Solution
REPORT ZSHU017 . data: begin of it occurs 3, f1(2) type n, f2 type i, f3(2) type c, f4 type p, end of it. it-f1 = '40'. it-f3 = 'DD'. it-f2 = it-f4 = 4. append it. it-f1 = '20'. it-f3 = 'BB'. it-f2 = it-f4 = 2. append it. sort it by f1.
Example 1 Solution
do 5 times. it-f1 = sy-index * 10. it-f3 = 'XX'. it-f2 = it-f4 = sy-index. read table it with key f1 = it-f1 binary search transporting no fields. if sy-subrc <> 0. insert it index sy-tabix. endif. enddo.
loop at it. write: / it-f1, it-f2, it-f3, it-f4. endloop.
Example 2
Example of parameters , Data, Predefined Data type, Events,write. Name of your report: Z10393_10 ID Number : Task : To find whether a given number is a palindrome Output : Input string = 20 Output string = 02 The given text is not Palindrome
Example 2 - Solution
Solution report z10393_10 . * Variables declaration. data : v_text1(100) type c. data : v_text2(100) type c. data : v_text3(100) type c. data : v_length type i. ************** ********************************************** * Selection screen parameters : p_input(100) type c. ************************************************************* * Start of selection.... v_text1 = p_input.
Example 2 - Solution
* Get the string length. v_length = strlen( v_text1 ).
* Start a while loop... while v_length >= 0. * select last letter,last-1... v_text2 = v_text1+v_length(1). * lastletter+last-1letter+..... concatenate v_text3 v_text2 into v_text3. ... v_length = v_length - 1. endwhile.
Example 2 - Solution
* Output.... write :/1 'Input string = ', 20 v_text1. write :/1 'Reverse string = ', 20 v_text3. if v_text3 = v_text1. write :/ 'The given text is palindrome'. else. write :/ 'The given text is not a palindrome'. endif.
Example No. 3
Name of your report: Z31212 ID number : TASK : To retrieve material master information Output : MBLNR MJAHR Material Number: 2 ZEILE Desc: 567
Solution-Example 3
REPORT Z31212_EXERCISE2_MM NO STANDARD PAGE HEADING . TABLES: MARA, MSEG, MAKT. DATA: BEGIN OF I_MARA OCCURS 0, MATNR LIKE MARA-MATNR, MTART LIKE MARA-MTART, MATKL LIKE MARA-MATKL, END OF I_MARA, BEGIN OF I_MSEG OCCURS 0, MATNR LIKE MSEG-MATNR, MBLNR LIKE MSEG-MBLNR, MJAHR LIKE MSEG-MJAHR, ZEILE LIKE MSEG-ZEILE,
Solution-Example 3
END OF I_MSEG, BEGIN OF I_MAKT OCCURS 0, MATNR LIKE MAKT-MATNR, MAKTX LIKE MAKT-MAKTX, END OF I_MAKT. SELECTION-SCREEN BEGIN OF BLOCK B1. SELECT-OPTIONS: S_MATNR FOR MARA-MATNR, S_MTART FOR MARA-MTART, S_MATKL FOR MARA-MATKL. SELECTION-SCREEN END OF BLOCK B1. SELECT MATNR MTART MATKL FROM MARA INTO TABLE I_MARA WHERE MATNR IN S_MATNR AND MTART IN S_MTART
Solution-Example 3
AND MATKL IN S_MATKL. IF NOT I_MARA[] IS INITIAL. SELECT MATNR MBLNR MJAHR ZEILE FROM MSEG INTO TABLE I_MSEG FOR ALL ENTRIES IN I_MARA WHERE MATNR EQ I_MARA-MATNR. SELECT MATNR MAKTX FROM MAKT INTO TABLE I_MAKT FOR ALL ENTRIES IN I_MARA WHERE MATNR EQ I_MARA-MATNR. ENDIF. LOOP AT I_MARA. AT NEW MATNR. WRITE :/5 'Material Number:',I_MARA-MATNR. ENDAT.
Solution-Example 3
LOOP AT I_MARA. AT NEW MATNR. WRITE :/5 'Material Number:',I_MARA-MATNR. ENDAT. LOOP AT I_MAKT WHERE MATNR = I_MARA-MATNR. WRITE : 35 'Desc:', I_MAKT-MAKTX. ENDLOOP. SKIP. WRITE :/5 'MBLNR', 30 'MJAHR', 55 'ZEILE'. LOOP AT I_MSEG WHERE MATNR = I_MARA-MATNR. WRITE :/5 I_MSEG-MBLNR , 30 I_MSEG-MJAHR, 55 I_MSEG-ZEILE. ENDLOOP. ULINE. ENDLOOP.
Example 4
Task : Learn ABAP/4 Output: The number is bad sum of dates* 39 number is good
Solution-Example 4
REPORT Z31212_LEARN . *----------------------------------------------------------------------*example for between. DATA NUM TYPE I. NUM = 34. IF NUM BETWEEN 30 AND 33. WRITE 'number is good'. ELSE. WRITE 'number is bad'. ENDIF. *-----------------------------------------------------------------------
Solution-Example 4 *example for add. NUM = 34. IF NUM BETWEEN 30 AND 35. ADD 5 TO NUM. WRITE NUM. WRITE 'number is good'. ELSE. WRITE 'number is bad'. ENDIF. *----------------------------------------------------------------------DATA : DD1 TYPE D VALUE '19900101'. DATA : DD2 TYPE D VALUE '19990000',SUM. SUM = DD1 + DD2. WRITE :/ 'sum of dates', SUM.
*-----------------------------------------------------------------------
Solution-Example 5
Example 5 Name of your report: ZSHU006 ID number : Task : Do loop exercise. Output: 1 2 2 4 3 6 4 8 5 10 99 6
Solution-Example 5
REPORT ZSHU006 . data: f1 type i, begin of s, c1 type i value 1, c2 type i value 2, c3 type i value 3, c4 type i value 4, c5 type i value 5, c6 type i value 6, end of s. field-symbols <f>. write / ''.
Solution - Example 5
do 6 times varying f1 from s-c1 next s-c2. if sy-index = 6. s-c6 = 99. else. f1 = f1 * 2. endif. assign component sy-index of structure s to <f>. "<f> now points to write <f>. "a component of s enddo. write / ''. do 6 times varying f1 from s-c1 next s-c2. write f1. enddo.