Beginner's Guide to BI - ABAP

Applies to:
This article is applicable to all SAP BI Consultants who are novice in ABAP Skills. For more information, visit the Business Intelligence homepage.

Summary
Routines in SAP BI are essential part as they provide a level of customization in the development. This document provides a basic understanding on how to write these routines. We have two approaches for this, Simple ABAP and OOPS ABAP. This document covers both the methods. After reading this document reader should be able to design and develop ABAP Code at different levels of SAP BI. This document will enable reader to gain basic knowledge required to develop ABAP code in BI. Author: Sharayu Kumatkar

Company: L&T Infotech Created on: 09 December 2009

Author Bio
Sharayu Kumatkar is a BI & ABAP Certified Associate with four years of experience as a Senior SAP Technical consultant with L&T Infotech.

SAP COMMUNITY NETWORK © 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 1

...................................................................................................................sap...........................................................................................................sap.................................................................................................................................................................................com | BPX ........................................................................................................... 7 FIELD-SYMBOLS .............................................................................................................. 9 I_STEP = 2 ......................................................................................................................... 18 Disclaimer and Liability Notice ........... 10 Appendix A: Sample Code ....................................................................................................... 3 Important Note ...................................................................................................................................................................................... 3 Start Routine ............................................... 4 Expert Routine .......................ABAP Table of Contents Different Routines Available in SAP BI ......................................... 12 Appendix B: Sample code ................................................. 9 I_STEP = 3 ......................................................................Remove Special Characters .................................................................................................. 7 LOOP .........................................................com | BOC .................................................................................................................. 3 Info Package Routine .............................................................................................................................................................................................. 10 Debugging ........................................................................................................................................................ 10 Tips & Tricks ..............sdn.....................boc.. 8 SORT ................................. 14 Related Content ......................................................................... 6 Basic Coding Statements Required ....................................................................................................................Beginner's Guide to BI ........................................................ 19 SAP COMMUNITY NETWORK © 2009 SAP AG SDN ................................................................................................................................................sap......Splitting 60+ char string into multiple IO .................................................................................................................................................................................................................................................................................................................................................................................... 7 SELECT .................................................................................................................. 9 Virtual Characteristic / Key Figure .................... 9 I_STEP = 1 ...... 8 Customer Exit Variable (BEx) ..................................bpx.............................................................................................................................................................................................................................................................................................................................................. 13 Appendix C: Sample Code .....................................OOPS Start and Characteristic Routine ..................................................................................... 8 READ ..................................................... 3 End Routine .............com 2 .................. 5 Characteristic Routine ........................... 9 I_STEP = 0 ..........................

boc. SOURCE_PACKAGE contains contents of source data. This routine enables us to give dynamic filename to IP.ABAP Different Routines Available in SAP BI Info Package Routine Info Package routine is available with Info Package of Flat file Datasource. You need to create a routine by clicking on the Routine button.com 3 . It is triggered before Transformation. SOURCE_PACKAGE has same structure as that of Source of the Transformation.Beginner's Guide to BI .sap. It is triggered after Transformation.com | BOC . RESULT_PACKAGE has same structure as that of target Object. Generally Start routines are used for Filtering records or fetching global data to be used in Characteristic routine.sap. Click on End Routine button to create End Routine.com | BPX . SAP COMMUNITY NETWORK © 2009 SAP AG SDN . Start Routine Start Routine is available with Transformation. Click on Start Routine button to create Start Routine.sdn. Write a code inside this routine to derive file name and assign it to variable p_filename.sap. RESULT_PACKAGE contains process data i. Give a name to routine. processed via start routine and transformation. Generally End user is used for updating data based on existing data.e. End Routine End Routine is available with Transformation.bpx.

This button is to update behaviour of fields in End Routines. fields filled in the end routine are not lost if there is no other active rule.sdn.Beginner's Guide to BI . You get two options once you select this button.ABAP Important Note For support pack 16 and above you get one more button besides End Routine (once end routine is created). Alternatively.sap.sap.bpx. Note: Data fields with the 'No transformation' rule type in the transformation are overwritten if the transformation contains an end routine. This problem always occurs for characteristics. As a result. One needs to make selection of proper option as it is mandatory. The default setting for the pushbutton is that only the fields with active rules are updated in the transformation.com | BPX . you can define that all the fields should always be updated by selecting 2nd radio button.boc. With this selection.com | BOC . If your system is below SP16 then you can implement note 1024816 to get above facility. This problem does not occur for key figures if the aggregation summation was set in the transformation.sap. fields populated in End routine wont be updated in the data target if no active rule exists for them in Transformation.com 4 . SAP COMMUNITY NETWORK © 2009 SAP AG SDN .

sap. This is used generally for customizing rules. All existing Rules will be deleted once you develop Expert Routine.com | BPX .com 5 . Note: If the target of the transformation is a Data Store Object. RESULT_PACKAGE has same structure as that of target Object.sap.sap.sdn. Expert Routine will trigger without any transformation Rule.boc. key figures are updated by default with the aggregation behaviour Overwrite (MOVE).Beginner's Guide to BI .bpx. SAP COMMUNITY NETWORK © 2009 SAP AG SDN .com | BOC . SOURCE_PACKAGE has same structure as that of Source of the Transformation.ABAP Expert Routine To create an Expert routine go to Edit menu and select Expert Routine. You can manipulate values from SOURCE_PACKAGE and append them in RESULT_PACKAGE.

Beginner's Guide to BI .com | BPX . Here you can do calculation of InfoObject value and assign value to variable Result.com 6 . If rule is for key figure. If rule is for Characteristic. which in turn will get assigned to InfoObject.boc.bpx. All global objects declared in Start Routine are available in Characteristic Routine. then key figure transformation routine will trigger. then Characteristic transformation routine will trigger. SAP COMMUNITY NETWORK © 2009 SAP AG SDN .ABAP Characteristic Routine Characteristic Routine is inside Transformation.com | BOC . This routine will be triggered inside the Transformation based on the characteristic or key figure.sdn.sap. Characteristic routine is executed after Start routine and before End Routine.sap. In Rule details of transformation select Routine to create a Characteristic Routine.sap.

ABAP Basic Coding Statements Required SELECT Syntax : SELECT result FROM source INTO/APPENDING target [[FOR ALL ENTRIES IN <ITAB>] WHERE <COND>] [GROUP BY <GROUP>] [HAVING <GROUP COND>] [ORDER BY <SORT KEY>]. WHERE : The addition WHERE is used to restrict the number of records fetched by SELECT statement. =. The sequence in which the lines are read depends on the table type: Standard tables and sorted tables : The lines are read by ascending table index. LT. The LOOP and ENDLOOP statements define a loop around internal table records.com | BOC . LE BETWEEN LIKE IN IS NULL FOR ALL ENTRIES : If the addition FOR ALL ENTRIES is specified before WHERE clause. The logical expression compares the content of columns in the database with the conditions provided in Where clause. then the fields from the internal table <ITAB> can be used as operands when comparing with relational operators. <. Addition APPENDING will add records fetched by SELECT. <.sap.bpx.. <>. SELECT will return records from database which are matching with the <ITAB> based on WHERE clause. Terms that can be used as Logical Expressions for a WHERE Condition are      2.Beginner's Guide to BI . … ENDLOOP.sap. The statement block between LOOP and ENDLOOP is executed once for each line. <=.sap. NE.. at the end of ITAB.com | BPX . >=. Existing contents of ITAB remains unchanged. Hashed Tables : The lines are processed in the sequence in which they were inserted in the table. The LOOP statement reads lines from internal table <ITAB> sequentially.sdn. LE. SAP COMMUNITY NETWORK © 2009 SAP AG SDN . OR LOOP AT <ITAB> [ASSIGNING <FIELD SYMBOL>] [WHERE <COND>].boc. EQ. >. . LOOP Syntax : LOOP AT <ITAB> [INTO <WORK AREA>] [WHERE <COND>]. You can either read all the lines or specify conditions <COND> to restrict which lines to be read. GE. The internal table <ITAB> must have a structured line type and the component comp must be compatible with the column col. 3. GT. ENDLOOP. and in the sort sequence following use of the SORT statement. INTO/ APPENDING : Addition INTO specifies where the records fetched by SELECT will get stored. Frequently used options of SELECT: 1. INTO clause refreshes ITAB contents and adds new records fetched by SELETC into ITAB.com 7 .

The FIELD-SYMBOLS statement declares a field symbol <FS>. SORT Syntax : SORT <ITAB> [STABLE] { { [ASCENDING|DESCENDING] [BY {comp1 [ASCENDING|DESCENDING] } {comp2 [ASCENDING|DESCENDING] } . ] } . If no explicit sort key is entered using the BY addition.Beginner's Guide to BI . numeric and byte-type components are sorted by their value and character-type components according to their binary representation code page. The ASSIGNING clause is used to assign a field symbol to current loop’s record. Any changes made to field-symbol values are directly reflected in <<ITAB>. The output result determines when and where the row contents are read.sap. the internal table <ITAB> is sorted according to the table key.ABAP The INTO clause is used to move current loop’s record into Work Area. SORT statement sorts an internal table <ITAB>. a free condition free_key or an index. Otherwise an exception/short dump will be triggered. BINARY SEARCH clause makes READ statement a bit faster. FIELD-SYMBOLS Syntax : FIELD-SYMBOLS <FS> { typing | STRUCTURE struc DEFAULT dobj }. the table key can contain a maximum of 250 components.com | BPX ..boc.sdn. no additional Update or Modify statement is required. After its declaration. it does not reference a memory area.bpx. Field-Symbol acts as a pointer to the record. Prerequisite for using BINARY SEARCH is that <ITAB> must be sorted with the keys used in READ’s table key. READ Syntax : READ TABLE <ITAB> { table_key | free_key | index } result BINARY SEARCH.com | BOC . You have to specify the row by either naming values table_key for the table key. a field symbol is initial that is.sap.sap. In this case. This statement reads a row from internal table <ITAB>. SAP COMMUNITY NETWORK © 2009 SAP AG SDN .. You have to assign a memory area to it (normally using the ASSIGN statement) before you can use it as an operand.com 8 . As standard.

the enhancement has been called via authorization check or from the monitor and not from variable screen.sap.boc. Structure of this internal table is as follows: Field VNAM IOBJNM SIGN OPT LOW HIGH Description Variable name InfoObject name (I)ncluding or (E)xcluding Operators: EQ =. This step is only started up when the same variable is not input ready and could not be filled at I_STEP=1. most commonly used values are explained below. SAP COMMUNITY NETWORK © 2009 SAP AG SDN .sap. I_STEP parameter from the Exit Function Module determines when customer exit code will be executed. This step can be used to validate user input.com | BPX .Beginner's Guide to BI . GT >. Characteristic value Upper limit characteristic value for intervals/the node-InfoObject for hierarchy nodes. I_STEP=2 is also called again. CP and so on. Coding for Customer Exit variable is done in EXIT_SAPLRRS0_001 Function Module.sdn. This can be used to populate variable with some default values. I_STEP = 3 When i_step has value 3 you can check the values of the variables. LT <. Depending upon i_step values we can populate values in variable during different steps of BEx execution. Afterwards. I_STEP = 2 When i_step has value 2 the enhancement call takes place directly after variable entry.com 9 . I_STEP has values from 0-3.com | BOC . LE <=. BT Between.ABAP Customer Exit Variable (BEx) Customer Exit Variable created in BEx query provides means to do complex calculation (not possible with BEx) in ABAP. I_STEP = 0 When i_step has value 0. Data records from BEx query are passed in I_T_VAR_RANGE internal table.bpx.sap. I_STEP = 1 When i_step has value 1 the enhancement call takes place directly before variable entry. GE >=. Triggering an exception here (RAISE) causes the variable screen to appear once more.

bpx. For more details please refer to links provided in ‘Related Content’ section. But if you want virtual KF for large number of Query fields then opt for BADI method.Beginner's Guide to BI .sap.boc.com 10 . If you are developing virtual KF for few Query fields then you should opt for SE38 method.ABAP Virtual Characteristic / Key Figure There are two ways of developing Virtual Characteristics / Key figures.sap. One is using BADI and other is using CMOD & SE38.com | BPX . Choose processing mode as ‘Serially in the Dialog Process (for Debugging)’.sap. Tips & Tricks Debugging Following screenshots will explain how to debug code written in Start Routine or End Routine. Open DTP of desired transformation. Put a hard coded break point (BREAK-POINT) in Start Routine/End Routine which you desire to debug. go to Execute tab. SAP COMMUNITY NETWORK © 2009 SAP AG SDN . Step 1.sdn.com | BOC . as it is very easy to maintain. Step 2.

com | BPX .Beginner's Guide to BI . SAP COMMUNITY NETWORK © 2009 SAP AG SDN . Click on Simulate button to start debugging. Step 4.com 11 . Select ‘Before Transformation’ check box to debug Start Routine. Select ‘Before End Routine’ check box to debug End Routine.ABAP Step 3.sap.sap.boc. Click on change Breakpoints button in front of Transformation.com | BOC .bpx. instead it will simulate the code behavior for Data Loading.sap. Debugging will not actually load data into data target.sdn.

com 12 .Beginner's Guide to BI .Code provided by Mr.com | BOC .sap.bpx. Note: SAP String character numbering starts with 0.sap. Following is the characteristic code routine for these InfoObjects. As BI has character limit of 60 for an InfoObject. Code for InfoObject 1 Characteristic routine: *Read first 60 characters from the string and assign to Result MOVE SOURCE_FIELDS-STRING+0(60) TO RESULT. Kartik Ravi SAP COMMUNITY NETWORK © 2009 SAP AG SDN . we need to split this string into two InfoObjects.Splitting 60+ char string into multiple IO Consider an example of character string of 120 Characters fetched via Datasource.com | BPX .sdn. --.boc. Code for InfoObject 2 Characteristic routine: *Read 60-119 characters from the string and assign to Result MOVE SOURCE_FIELDS-STRING+60(60) TO RESULT.ABAP Appendix A: Sample Code .sap.

Tej Trivedi Note: Instead of above code you can also use SF_SPECIALCHAR_DELETE Function module to remove these special characters. Result) till special characters are found in string WHILE RESULT CN '%&()"''*+. remove special character * sy-fdpos -> Found location for search in byte-type and character-type data objects. *Remove blank spaces from string by condensing it. --./:. ENDIF. CONDENSE RESULT.-. Following code is helpful in avoiding such errors by removing special characters from the string. *If first character of string is a space or special character ‘#’ then remove it.boc. *Convert string into Upper case TRANSLATE RESULT TO UPPER CASE. RESULT+SY-FDPOS(1) = ' '. *Conditional loop on String (i.com 13 .bpx.sap.e. Code: * Assign source field to Result RESULT = SOURCE_FIELDS-ZZLIFEX.<=>?_#~@!$^[]{}0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ '.Code provided by Mr.sdn. *Replace special character found with none i.Remove Special Characters Most data loading related errors in BI are due to some special character in Datasource string. CONDENSE RESULT. ENDWHILE.sap. RESULT(1) = ' '.ABAP Appendix B: Sample code . IF RESULT(1) = '#'.sap.com | BPX .e. SAP COMMUNITY NETWORK © 2009 SAP AG SDN .Beginner's Guide to BI .com | BOC .

Following is a step by step example of basic OOPS programming used for Start Routine and Characteristic Routine.sdn.boc. The main purpose of OOPS is to obtain modularity in the code.sap.com 14 .sap. Frequently used code is converted into a class method.com | BOC .Beginner's Guide to BI . which can be called from all the routines.ABAP Appendix C: Sample Code .sap. Create a class using TCODE SE24 SAP COMMUNITY NETWORK © 2009 SAP AG SDN . Step 1.com | BPX .bpx.OOPS Start and Characteristic Routine These days most of the clients are asking for OOPS programming in the routine.

com | BPX .sap.boc.sdn. Create a method inside this class Step 3.sap.Beginner's Guide to BI .com | BOC . Assign parameters to the method by clicking on parameters button SAP COMMUNITY NETWORK © 2009 SAP AG SDN .bpx.com 15 .ABAP Step 2.sap.

com 16 .Beginner's Guide to BI .com | BOC .sap.com | BPX .ABAP Step 4.sap.sdn.boc. Define class instance in global are of Start Routine SAP COMMUNITY NETWORK © 2009 SAP AG SDN .sap.bpx. Write code inside method Step 5.

com 17 .Beginner's Guide to BI .com | BPX .sap.ABAP Step 6.sap.boc. Kshitija Karadkhedkar SAP COMMUNITY NETWORK © 2009 SAP AG SDN . Call class method in Characteristic Routine using above class object ---Code provided by Ms.bpx.sdn. pass appropriate parameters to the method Step 7.com | BOC . Call class method in Start Routine using above class object. Create a class object in Start Routine using ‘CREATE OBJECT’ statement.sap.

sap.com Dependencies for Variables of Type Customer Exit Update Routines and Start Routines Routines in Transformations Creating Virtual Characteristic/Key Figures via CMOD and SE38 Creating Virtual Characteristic/Key Figures via BADI SAP COMMUNITY NETWORK © 2009 SAP AG SDN .boc.bpx.com | BPX .Beginner's Guide to BI .com | BOC .com 18 .ABAP Related Content Customer Exits on help.sdn.sap.sap.sap.

bpx.com 19 . SAP will not be held liable for any damages caused by using or misusing the information. Changes made based on this information are not supported and can be overwritten during an upgrade. You agree that you will not hold. SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample. and anyone using these methods does so at his/her own risk. SAP responsible or liable with respect to the content of this document.sap.ABAP Disclaimer and Liability Notice This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP.boc. code or methods suggested in this document.com | BOC .com | BPX .sap. including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP.sdn. SAP COMMUNITY NETWORK © 2009 SAP AG SDN . or seek to hold.sap.Beginner's Guide to BI .

Sign up to vote on this title
UsefulNot useful