Saturday, 22 November 2014

STOP CONTINUE EXIT RETURN STATEMENTS

SAPNUTS_SAPABAP  

Tutorial Details

Tutorial NameWorking with EXIT, STOP, CONTINUE and RETURN in SAP ABAP
Tutorial DescriptionLearn how to use STOP, EXIT, CONTINUE and RETURN commands in SAP ABAP, difference between STOP, EXIT, CONTINUE and RETURN in SAP ABAP
Tutorial AreaCore ABAP
PrerequisitesABAP
Learning LevelIntermediate
Estimated Time to learn30
In our day to day ABAP programming implementations, we may need to use exit, continue, stop and return statements, this article will help you to understand what are exit, stop, continue and return statements? And when do we use them.

EXIT

The behavior of EXIT keyword is depends on where you use it.
  • If you use EXIT keyword inside IF .. ENDIF., it will comes out of the program.
  • If you use EXIT inside LOOP .. ENDLOOP., it will come out of loop.
  • If you use EXIT inside FORM .. ENDFORM., it will comes out of form (subroutine).
Example program os using EXIT keyword
REPORT ZSAPN_EXIT.
DATA: IT_MARA TYPE TABLE OF MARA,
      WA_MARA TYPE MARA.
PARAMETERS: P_MATNR TYPE MARA-MATNR.

START-OF-SELECTION.
  SELECT SINGLE * FROM MARA INTO WA_MARA WHERE MATNR = P_MATNR.

  IF WA_MARA IS INITIAL.
    EXIT. “exit program
  ENDIF.
  WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.

STOP

STOP is a statement which is used to stop processing an event block, ex: If I have two events START-OF-SELECTION and END-OF-SELECTION in my program, If I use STOP keyword in START-OF-SELECTION, the keyword will exits start-of-selection and goes to END-OF-SELECTION.
See the difference between below two programs.
Program1Program2
REPORT ZSAPN_STOP.
DATA: IT_MARA TYPE TABLE OF MARA,
      WA_MARA TYPE MARA.

START-OF-SELECTION.
  SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
  STOP.

  LOOP AT IT_MARA INTO WA_MARA.
    WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.
  ENDLOOP.
REPORT ZSAPN_STOP.
DATA: IT_MARA TYPE TABLE OF MARA,
      WA_MARA TYPE MARA.

START-OF-SELECTION.
  SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
  STOP.

END-OF-SELECTION.
  LOOP AT IT_MARA INTO WA_MARA.
    WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.
  ENDLOOP.
Result: NO OUTPUTRESULT: OUTPUT WILL BE DISPLAYED

CONTINUE

CONTINUE is a statement, which is used to skip execution of a record inside loop.. endloop, do..endo, while..endwhile etc. This keyword will only be used in loops.
REPORT ZSAPN_CONTINUE.
DATA: IT_MARA TYPE TABLE OF MARA,
      WA_MARA TYPE MARA.
START-OF-SELECTION.
  SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.

  LOOP AT IT_MARA INTO WA_MARA.
    IF WA_MARA-MTART = 'HALB'. "Don’t print if material type is 'HALB'
      CONTINUE. “Skip the record and go for next record
    ENDIF.
    WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.
  ENDLOOP.
In the above program, the output will not display materials of type ‘HALB’ because loop skips the records with HALB material type.

RETURN

RETURN is a statement which is used to stop processing of current block immediately. Execute and see the difference between the below two programs.
Program1Program2
REPORT ZSAPN_RETURN.
DATA: IT_MARA TYPE TABLE OF MARA,
      WA_MARA TYPE MARA.

START-OF-SELECTION.
  SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
  RETURN.

END-OF-SELECTION.
  LOOP AT IT_MARA INTO WA_MARA.
    IF WA_MARA-MTART = 'HALB'.

    ENDIF.
    WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.
  ENDLOOP.
REPORT ZSAPN_RETURN.
DATA: IT_MARA TYPE TABLE OF MARA,
      WA_MARA TYPE MARA.

START-OF-SELECTION.
  SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.

END-OF-SELECTION.
  LOOP AT IT_MARA INTO WA_MARA.
    IF WA_MARA-MTART = 'HALB'.
RETURN.
    ENDIF.
    WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.
  ENDLOOP.
Result: NO OUTPUTRESULT: SOME RECORDS as program execution stops after ‘HALB’ material type.

Saturday, 15 November 2014

strings operations


String Operations:
String is the variable length data type
Dynamic memory management is used internally i.e the memory allocation at the run time according to the current field content
Note: we can’t declare the string variable through PARAMETER
PARAMETER p_name type String. Is not allowed because the system can’t understand how big it is
Difference between the character and string:
Character
String
Data: v1 type cahr50.
V1 = ‘Career it’.

Here memory is always allocated for 50 characters irrespective of number of characters should be stored currently
Data:v1 type string.
V1 = ‘Career it’.

Here memory is allocate for ‘Career it’ only because always string size depends on number of characters should be stored currently

String operations:
CONCATENATE: to join more than one substring into one variable
Syntax:
CONCATENATE <String1> <string2>………<string N> into <str> separated by <Separator>
Ex :
*&---------------------------------------------------------------------*
*& Report  ZVISHNU_STRING_OPERATIONS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  ZVISHNU_STRING_OPERATIONS NO STANDARD PAGE HEADING.*//declare varibles
data :str1 type string.*//concatenating the strings
CONCATENATE 'Career it' 'Ammerpet' 'hyd' INTO str1 SEPARATED BY ','.*//Displaying output
write:/ str1.
Output:
2. *&---------------------------------------------------------------------*
*& Report  ZVISHNU_STRING_OPERATIONS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  ZVISHNU_STRING_OPERATIONS NO STANDARD PAGE HEADING.*//declare varibles
data :str1 type string.CONCATENATE 'Career it' 'Ammerpet' 'hyd' INTO str1 SEPARATED BY ','.*//Displaying output
WRITE:/ str1.
Output:

CONDENSER: replace the sequence of spaces into one space
Ex: CONDENSER ‘Career                it’.
Result: Career it
Ex: CONDENSER ‘Career                it’ NO-GAPS.
Result: Careerit
TRANSLATE: to translate to UPPER / LOWER case
*&---------------------------------------------------------------------*
*& Report  ZVISHNU_STRING_OPERATIONS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  ZVISHNU_STRING_OPERATIONS NO STANDARD PAGE HEADING.*//declare varibles
data :str1 type string.* Assigning values to those variables
str1 = 'career it'.*//Traslate the string into Uppercase
TRANSLATE str1 TO UPPER CASE.*//Displaying output
write:/ str1.
REPLACE:
Syntax :
REPLACE <str1> with <str2> into <str>.
*&---------------------------------------------------------------------*
*& Report  ZVISHNU_STRING_OPERATIONS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  ZVISHNU_STRING_OPERATIONS NO STANDARD PAGE HEADING.*//declare varibles
data :str1 type string,
      str2 
type string,
      str3 
TYPE string.* Assigning values to those variables
str1 = 
'c'.
str2 = 
'i'.
str3 = 
'Career it'.*//Replaceing the stings
REPLACE  str2 WITH str1 INTO str3.*//Displaying output
write:/ str3.
Output:
                      
SPLIT: to split the main string into substrings at the given separator
SPLIT <String> AT <separator> into <str1> <str2>…….<str N>.
*//declare varibles
data :v_id TYPE char20,
      v_name 
TYPE char20,
      v_place 
TYPE char20,
      v_city 
TYPE char20.*//Calling the split
SPLIT 'c001, Careerit, ammerpet, hyd ' at ',' INTO v_id v_name v_place v_city.*//Displaying output
write: v_id, v_name, V_place, v_city.
Output:
SHIFT: By default shift to left by one place
SHIFT <str> <LEFT/RIGHT/CIRCULAR> by <N> places. (N>0)
SHIFT <str> LEFT DELETING LEADING <CHAR>
SHIFT <str> RIGHT DELECTING TRAILING <CHAR>.

SEARCH: searches for the required main string in the main string
SEARCH <str> FOR <str1>.
SEARCH string1 FOR string2 STARTING AT position1 ENDING AT position 2.
-       position1 and position2 are starting ending line of internal tables.


Comparing strings:
                Operator
Description
                   CO
Contains only
                   CN
Contains not only
                   CA
Contains any
                   NA
Contains not only
                   CS
Contains String
                    NS
Contains no String
                    CP
Matches pattern
                    NP
Does not Matches pattern

CO(Contains only):
IF <str1> CO <str2>.
Is TRUE if string1 contains only the characters from string2 the comparison is case sensitive including spaces
If the comparison  is true then the system field SY-FDPOS contains the length of string1
If it is false then SY-FDPOS contains the offset of the first character of string1 that does not occur in string2
Examples:
         'AABB' co 'AB' True
         'ABCD' co 'ABC' False
         'AABB' cn 'AB' False
         'ABCD' cn 'ABC' True
         'AXCZ' ca 'AB' True
         'ABCD' ca 'XYZ' False
         'AXCZ' na 'ABC' False
         'ABCD' na 'XYZ' True 

Read more at http://sapabap-freshers.blogspot.com/2012/06/string-operations-in-abap.html#JARcy2vr8hetvJ4i.99

Saturday, 8 November 2014

PARAMETER IMPLEMENTATION



MONDAY, JANUARY 02, 2006

Parameters

Syntax
PARAMETERS name(length) [TYPE type or LIKE obj] DECIMALS d.
Examples
PARAMETERS: char1(20) TYPE C,
            date1 LIKE SY-DATUM,
            number1 TYPE P DECIMALS 2. 
Options
  • DEFAULT
    Example: 
    PARAMETERS p1(5) TYPE C DEFAULT ’we’.
  • MEMORY ID
    Example: 
    PARAMETERS p2(5) TYPE C MEMORY ID pid.
  • LOWER CASE
    Example:
    PARAMETERS f1(10) LOWER CASE.
  • VISIBLE LENGTH

Example:
PARAMETERS f1(10) TYPE C VISIBLE LENGTH 4.
  • OBLIGATORY
Example:
PARAMETERS f1(10) TYPE C OBLIGATORY.
  • MATCHCODE OBJECT
Example:
PARAMETERS p_belnr LIKE BKPF-BELNR MATCHCODE OBJECT zsh_f4_belnr.
  • VALUE CHECK
Example:
PARAMETERS p_belnr LIKE BKPF-BELNR OBLIGATORY VALUE CHECK.
  • AS CHECKBOX
Example:
PARAMETERS: c1 AS CHECKBOX DEFAULT ’X’.
  • RADIOBUTTON GROUP

Example:
PARAMETERS: r1 RADIOBUTTON GROUP rad1 DEFAULT ’X’,
            r2 RADIOBUTTON GROUP rad1,
            r3 RADIOBUTTON GROUP rad1.
  • NO-DISPLAY
Example:
PARAMETERS: f1(10) TYPE C NO-DISPLAY.
  • MODIF ID
Example: PARAMETERS: f1(10) MODIF ID GR1.

SUNDAY, JANUARY 01, 2006

Program Selections

When you want to create report in SAP, the selection screen will be displayed when we want to execute program. The syntax for create selections are compose of:

Data Declaration

In ABAP/4, we can declare data by command DATA.

Introduction

ABAP/4 (advanced business application program, 4th generation) is a language that we want to develop report or program in SAP system.
  1. Commenting
  2. Program Selections
  3. ABAP/4 Data Type

ABAP/4 Data Type

In ABAP/4, we can declare data by command DATA.
Type      Description  Initial Value C Character Space D Date ’00000000’
F Floating Point 0.0
I Integer 0
N Numeric Text ’0’
P Packed Decimal 0
T Time ’000000’
X Hexadecimals X00

SATURDAY, DECEMBER 31, 2005

Commenting

In ABAP/4, we have 2 formatting for comment:
  1. Line Comment: If you want to comment whole line, you can put * sign in front of the line.
    Example:
    * Comment Line
    WRITE: This is the example for commenting. 
  2. Partial Line Comment: If you want to comment partail of line, you can put " sign in front of the comment text
    Example:
    WRITE: This is the example for commenting.    "Comment Line

How to get windows filename

Method 1 
CALL FUNCTION ’KD_GET_FILENAME_ON_F4’ EXPORTING MASK = ’,*.txt,*.*’ STATIC = ’X’ CHANGING FILE_NAME = LV_FILE.
Method 2
     CALL FUNCTION ’F4_FILENAME’
       EXPORTING
         program_name  = syst-cprog
         dynpro_number = syst-dynnr
         field_name    = ’ ’
       IMPORTING
         file_name     = LV_FILE.

MATHEMATICAL_FUNCTION_FOR _NUMERIC_DATA_TYPES(I,F,P)

Mathematical functions Locate the document in its SAP Library structure

ABAP contains a range of built-in functions that you can use as mathematical expressions, or as part of a mathematical expression:
[COMPUTE] n = func( m ).
The blanks between the parentheses and the argument m are obligatory. The result of calling the function func with the argument m is assigned to m.

Functions for all Numeric Data Types

The following built-in functions work with all three numeric data types (fip) as arguments.
Functions for all Numeric Data Types
Function
Result
abs
Absolute value of argument.
sign
Prefix of the argument :                      1            x > 0
                                          SIGN( x ) =  0   if  x = 0
                                                                 -1            x < 0
ceil
Smallest integer value not smaller than the argument.
floor
Largest integer value not larger than the argument.
trunc
Integer part of argument.
FRAC
Fraction part of argument.
The argument of these functions does not have to be a numeric data type. If you choose another type, it is converted to a numeric type. For performance reasons, however, you should use the correct type whenever possible. The functions itself do not have a data type of their own. They do not change the numerical precision of a numerical operation.
Example
DATA n TYPE p DECIMALS 2.
DATA m TYPE p DECIMALS 2 VALUE '-5.55'.
n = abs( m ).   WRITE:   'ABS:  ', n.
n = sign( m ).  WRITE: / 'SIGN: ', n.
n = ceil( m ).  WRITE: / 'CEIL: ', n.
n = floor( m ).
 WRITE: / 'FLOOR:', n.
n = trunc( m ).
 WRITE: / 'TRUNC:', n.
n = frac( m ).  WRITE: / 'FRAC: ', n.
The output appears as follows:
ABS:              5.55
SIGN:             1.00-
CEIL:             5.00-
FLOOR:            6.00-
TRUNC:            5.00-
FRAC:             0.55-
Example
DATA: t1(10) TYPE c,
      t2(10) TYPE c VALUE '-100'.
t1 = ABS( t2 ).
WRITE t1.
Result:
       100
Two conversions are performed. First, the contents of field t2 (type c) are converted to type p. Then the system processes the abs function using the results of the conversion. Then, during the assignment to the type c field t1, the result of the function is converted back to type c.

Floating-Point Functions

The following built-in functions work with floating point numbers (data type f) as an argument.
Floating point functions
Function
Meaning
acosasinatancossintan
Trigonometric functions.
coshsinhtanh
Hyperbolic functions.
exp
Exponential function with base e (e=2.7182818285).
log
Natural logarithm with base e.
log10
Logarithm with base 10.
sqrt
Square root.
For all functions, the normal mathematical constraints apply (for example, square root is only possible for positive numbers). If you fail to observe them, a runtime error occurs.
The argument of these functions does not have to be a floating point field. If you choose another type, it is converted to type f. The functions themselves have the data type f. This can change the numerical precision of a numerical operation.
Example
DATA: result TYPE f,
      pi(10) TYPE c VALUE '3.14159265'.
result = cos( pi ).
WRITE result.
The output is -1.00000000000000E+00. The character field pi is automatically converted to a type f field before the calculation is performed.



End of Content Area