cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How calculate based on String

former_member187400
Active Contributor
0 Kudos
3,571

Hi All -

I am working for BPC Embedded Planning BW 7.5 .

We have z-table having formula maintained -

Account (Output)SequenceOperatorAccount Ref.
1001011START30101
1001012PLUS30102
1001013MINUS30103
1001014PLUS 30104
1001015END

and we have transaction data:

AccountYearAmount
30101201610
3010220165
30103201620
30104201630

The plan is to have program to calculate based on that table to be like this:

100101 = 10 + 5 - 20  + 30

100101 = 25

I have a plan to put the aritmatics into string - and function module calculate it for me.

Do you know what ABAP standard function module that can do it?

Many thanks,
Daniel N.

Accepted Solutions (1)

Accepted Solutions (1)

RaymondGiuseppi
Active Contributor
0 Kudos

You could play with FM EVAL_FORMULA, but using this FM will need more time than coding it yourself.

Regards,

Raymond

Sandra_Rossi
Active Contributor
0 Kudos

Not long, if there's a good example 😉 :

DATA d(255) TYPE c.
DATA l_prog TYPE syrepid.
DATA l_sr TYPE syrepid.
DATA a TYPE f.
DATA b TYPE I.
d = `2.5 * ( PIX + abs( -1 ) )`.
l_prog = sy-repid.
l_sr = 'MYROUTINE'.
CALL FUNCTION 'EVAL_FORMULA'
  EXPORTING
    formula                 = d
    program                 = l_prog
    routine                 = l_sr
  IMPORTING
    value                   = a
  EXCEPTIONS
    division_by_zero        = 1
    exp_error               = 2
    formula_table_not_valid = 3
    invalid_expression      = 4
    invalid_value           = 5
    log_error               = 6
    parameter_error         = 7
    sqrt_error              = 8
    units_not_valid         = 9
    missing_parameter       = 10
    OTHERS                  = 11.
b = a.
ASSERT B = 10.

FORM myroutine
    USING
        i_var     TYPE c
    CHANGING
        e_value   TYPE f
        e_subrc   TYPE p.
  CASE i_var.
    WHEN `PIX`. e_value = 3.
    WHEN OTHERS. e_subrc = 1.
  ENDCASE.
ENDFORM.

And a good documentation:

  • Possible operators: *, /, +, -, DIV, MOD, ABS, NOT, **, SIN, COS, LOG, TAN, EXP, SQRT, ROUND, INT, TRUNC, (, )
    • INT and TRUNC are the same.
  • IF, AND, OR, =, >=,, THEN, ELSE
  • Literals: 3E2, -4, -4.4, PI (314159265358979E-14), 'litteral', SY-xxxxx, TRUE (1), FALSE (0), "comment",
    • 'litteral' : it's to be used for conditions because EVAL_FORMULA only works with numbers
    • texts are converted to upper case (IF tp = 'gOu' ... is same as IF TP = 'GOU'). # must be set before a letter to not convert it to upper case (IF tp = '#gO#u').
    • others characters = variable : call EVAL_FORMULA with callback routine to convert the variable into a value
  • MYTABLE:KKK( A, B, C, ... ) or MYTABLE:KKK( A; B; C; ... 😞 the table MYTABLE must have first key field 1-13 characters and next field is a formula between 1-1024 characters.
    • It reads record with firstkey = 'KKK', the second field is a up-to-1024 characters formula which is evaluated.
    • This formula may use #1 to #9 to reference parameters.
    • SAP provides the table TFKT where first key is 13 characters and formula 65 characters.
    • Example : MIN = IF #1 < #2 THEN #1 ELSE #2. If we evaluate TFKT:MIN(1,4), it will return 1.

former_member187400
Active Contributor
0 Kudos

Hi Sandra, I will try, and revert back to you.

Many thanks,

former_member187400
Active Contributor
0 Kudos

It works - many thanks Sandra. You're awesome.

Answers (3)

Answers (3)

Former Member
0 Kudos

convert it to a subroutine in ABAP (have a standard interface) and generate a subroutine pool.

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
0 Kudos

As far as I know there is no dynamic way to do arithmetic calculations (variant of dynamic SELECT ... WHERE (cond_syntax) ). You can only dynamicaly create report (INSERT REPORT).

Also there is this "Internally released" function EVAL_FORMULA which works with arithmetic expresions in string format. Program to evaluate formula in a string - ABAP Development - SCN Wiki

But I would rather go simple step-by-step way:

CASE operator.

     WHEN '+'.

          result = a + b.

     ....

ENDCASE.

former_member187400
Active Contributor
0 Kudos

Thanks Thomas, I found this blog as well. I tested, but I had no luck. I encountered an error.

I will do the "manual" way for the last resort.

Many thanks,

Daniel

Sandra_Rossi
Active Contributor
0 Kudos

I don't understand whether it's a pure ABAP question, then the answer is no (i.e. do it yourself, it seems to be a quite simple logic), or if it's related to BPC that I don't know, in which your Z-table was generated by a standard BPC process, and in that case I would recommend to post to the BPC forum.

former_member187400
Active Contributor
0 Kudos

Hi Sandra -

Many thanks for the quick response.

It is pure ABAP question. The sample here is very simple sample, but in the real world, it has more e.g. Bracket, Devide, Multiply, etc.

Hence, does it have ABAP function module to calculate based on string?

Many thanks,

Former Member
0 Kudos

For a start, I would not have "PLUS", "MINUS". Define the symbols instead "+", "-" etc.

How will you handle brackets with regards to multiplications or division etc.

former_member187400
Active Contributor
0 Kudos

if I have standard ABAP Function module where it can compute the arithmetic on the string -  the Function Module will handle it.

What I need to do is to put all values of the account into 1 string -  the function module will take care of it. 

Does it answer your question?

Many thanks, 

Daniel N .

Former Member
0 Kudos

If you have a string of

4 + 4  - 3 in a string. You don't need a function module to handle it

Simply having the following will suffice.

answer = 4 + 4  - 3