SAP Function CHECK_FORMULA - Check syntax of a formula

Parameter Reference Type Length Default Optional Text
FORMULA 0 Formula to be checked
PROGRAM 0 SPACE X Program for value assignment to a variable
ROUTINE 0 SPACE X Routine for value assignment to a variable
UNIT_OF_MEASURE 0 SPACE X Unit of measurement for calculated value

Parameter Reference Type Length Text
FUNCNAME 0 Function name of the called routine
MESSAGE 0 Error Message
POS 0 Position of first error
SUBRC 0 Error status

Exception Text
ERROR_IN_FORMULA Error in formula (with MESSAGE E... RAISING)
MISSING_PARAMETER Program or Routines Missing for Variables

Functionality
The function module CHECK_FORMULA checks the syntax of a Literal orVariable formula. It accepts:

  • arithmetic formulas (with floating point result of type FLOAT)

  • logical formulas (with result TRUE or FALSE)

  • conditions of the form:

  • IF THEN

    ,,ELSE

    • Expressions with fields of the structure SY

    • Example:
      FORMULA = COS(A**2+B**2)
      FORMULA = IF X>0 THEN SIN(X) ELSE SIN(-X)
      FORMULA = SY-TZONE / 2.324
      Operands
      The following operands are allowed:
      • numeric constants, e.g. -5, 3.1416, 1.3E12

      • variables

      • TRUE

      • FALSE

      • PI (= 3.14159 ...)

      • SY-UZEIT

      • SY-DATUM

      • formal parameters, e.g. #1, #2

      • Functions
        The FM supports the following functions:
        ABS,,absolute value
        ,,Example: FORMULA = ABS(-2)
        ,,result: 2
        NOT,,negation
        ,,Example: FORMULA = IF NOT(2<3) THEN (5*6) ELSE (5-7)
        ,,result: -2
        SIN,,sine function
        ,,Example: FORMULA = SIN(PI/2)
        ,,result: 1
        COS,,cosine function
        ,,Example: FORMULA = COS(0)
        ,,result: 1
        TAN,,tangent function
        ,,Example: FORMULA = TAN(2*PI)
        ,,result: 0
        LOG,,logarithm to base e (natural logarithm)
        ,,Example: FORMULA = LOG(2.7182746352)
        ,,result: 1
        EXP,,exponential function to base e
        ,,Example: FORMULA = EXP(1)
        ,,result: 2.7182746352
        SQRT,,square root function
        ,,Example: FORMULA = SQRT(144)
        ,,result: 12
        ROUND,,round to a whole number
        ,,Example: FORMULA = ROUND(3.14159)
        ,,result: 3
        TRUNC,,truncate; the whole number part of a real number
        ,,Example: FORMULA = TRUNC(5/3)
        ,,result: 1
        MOD,,modulus function
        ,,Example: FORMULA = 10 MOD 3
        ,,result: 1
        DIV,,whole number part of a division
        ,,Example: FORMULA = 10 DIV 3
        ,,result: 3
        Operators
        The operators are classified in 6 priority groups. The operators ingroup 0 have the lowest priority and those in group 6 the highest.
        The operators are:
        group 0:
        AND,,logical AND
        OR,,logical OR
        group 1:
        =,,equals
        <>, ><,,not equals
        <,,less than
        >,,greater than
        <=, =<,,less than or equal to
        >,,greater than
        =>, >=,,greater than or equal to
        group 2:
        +,,plus value (of an expression)
        -,,minus value (of an expression)
        +,,addition
        -,,subtraction
        group 3:
        *,,multiplication
        /,,division
        group 4:
        **,,exponent
        group 5:
        ABS,,absolute value
        NOT,,negation (of a logical expression)
        group 6: all other functions.
        user-defined FORMULAs
        You can define functions in a table like TFKT and use them as well asstandard functions. The function name is the table key. The table andfunction names (separated by a colon) are specified in calls.
        Example
        The FORMULA PI*R**2 is in the table TFKT under the function namecirclearea. It is called with FORMULA =TFKT:circlearea(...)
        User-defined functions can be nested.
        Example:
        The expression PI*R**2 is defined as the function circlearea.The function cylinder can now be defined as cylinder =TFKT:circlearea*H.
        If a user group needs another table than TFKT (e.g. to structure thekey or include user fields), the reference table TFKT should be copied.User functions can be inserted from position 79.
        Tables used to store FORMULAs must have a key length of 13 and afunction length of 65.

        Notes

        • In contrast to ABAP, spaces are ignored.

        • A FORMULA must not contain other Literals.

        • Character strings in quotation marks are interpreted as comments and
        • are ignored.

          Description
          The parameter FUNCNAME contains the name of the function in TFKT inwhich an error occurred.

          Description
          The parameter MESSAGE contains an error message if an error occurred inthe formula check.

          Value range
          Error,,Message
          01,,Call & does not exist
          02,,Syntax error between positions &1 and &2
          03,,Open brackets expected from column &
          04,,ABAP program does not exist: &
          05,,ABAP function does not exist: &
          06,,Incorrect function name
          07,,Closing brackets expected
          08,,Incorrect formal parameter: &
          09,,Recursive call of &
          10,,Parameter error ( & )
          11,,Variable &1 in position &2 and &3 cannot be identified
          12,,Incorrect Literal: &
          13,,Formula set structure & is greater
          ,,than 13 characters
          14,,Formula string field is greater than 1024 characters
          15,,Formula set & does not exist

          Default

          Description
          When formula check errors occur, the parameter POS contains theposition of the first error.

          Default

          Description
          The parameter SUBRC states whether the formula check was successful.

          Value range
          Value,,Meaning
          0,,no error
          4,,error
          The formula to be checked is passed to the function module in theparameter FORMULA. The formula can be pased either in a Literal or in atype C field. The maximum length of a formula is 256 characters.

          Description
          The parameter PROGRAM specifies a user-specific program for furtherchecks of the formula passed.

          Default

          Description
          The parameter ROUTINE specifies a user-specific routine for furtherchecks of the formula passed.

          Description
          The parameter UNITS_OF_MEASURE determines the unit of measurement ofthe value which is calculated by the formula.

          Value range
          vgl. Tabelle T006