cancel
Showing results for 
Search instead for 
Did you mean: 

Numerical Variable in SAP script

Former Member
0 Kudos

hi

please i have a requirement to generate the some fields on as SAP Script form.But it's not outputing any time i print.This is what i did

/:declare &my_tax& = &REGUP-WRBTR& " i want to declare a field my_tax like REGUP-WRBTR

/:declare &my_tax& = 0.05 * &REGUP-WRBTR& " tax = 0.05*the gross(REGUP-WRBTR)

i now placed &my_tax& on the form,yet that particular field was blank on printing

Pls i'll appreciate if some one can help look at the syntax.

The idea is to compute the tax at runtime and output it on the form.

Many thanks.

Accepted Solutions (0)

Answers (5)

Answers (5)

Former Member
0 Kudos

Hi,

You can do the calculation in a Z program by calling a perform statement from SAP script and returning the calculated value back to the script. Then print this returend value in SAP script.

You can refer this link for further details.

Link: http://wiki.sdn.sap.com/wiki/display/ABAP/SubroutinesAndTheirUseIn+SAPScript

Harsh_Bansal
Contributor
0 Kudos

Hi,

To do the calculation, you need to use -

PERFORM subroutine CHANGING your_var

you will have to perform calculation in subroutine in the program.

Regards,

Harsh Bansal

Former Member
0 Kudos

Thank you guys ... pls to all you who suggested i write subroutine and use a perform- this is what i adapted have but it's already flagging some errors .please can u help me check at this piece !

DATA : VAR1 LIKE REGUP-DMBTR, "-AMOUNT IN LOCAL CURRENCY

TAX like REGUP-MWSTS, " - TAX AMOUNT IN aLOcal curency

wht TYPE p value '0.05'.

Constants wht type p decimal 2 value '0.05'

FORM tax_calc TABLES p_input_table STRUCTURE itcsy " (table to hold the values passed from script)

p_output_table STRUCTURE itcsy. " (table to hold the values passed to script)

READ TABLE p_input_table WITH KEY name = 'VAR1' . " (should be in samecase as in script').

If sy-subrc = 0.

VAR1 = VAR1 * wht. " (do whatever calculation you want)

Endif.

READ TABLE p_output_table WITH KEY name = 'TAX'

*IF sy-subrc = 0. " Flags an error here saying sy-subrc is not expected .... (1)

MOVE VAR1 TO p_output_table-value. " an error here "=..." expected after move .......(2)

MODIFY p_output_table INDEX sy-tabix.

Endif.

Endform.

i need this subroutine to work in my own copy of program RFFOUS_C that is ZRFFOUS_C.

Many thanks ...

Former Member
0 Kudos

Hi friend,

Just try using this piece of code,

form CALCUUU TABLES IN_PAR STRUCTURE ITCSY

OUT_PAR STRUCTURE ITCSY.

DATA CALCW TYPE WRBTR.

READ TABLE OUT_PAR with key 'TOT'.

if sy-subrc eq 0.

CALCW = 2 * 10.

OUT_PAR-value = CALCW.

CONDENSE OUT_PAR-value.

endif.

MODIFY OUT_PAR INDEX 1.

This is simply a model which i have used so do the similar.

But before that check the using and changing parameters in th Script. The USING parameter and CHANGING parameter should not be the same.

Add like this in Script

/: DEFINE &TOT&

/: PERFORM CALCUUU IN PROGRAM ZTR02_TEST_DRIVER

/: USING &MY_TAX&

/: CHANGING &TOT&

/: ENDPERFORM

/: &TOT&

I think this will solve your problem if you face any issues please revert back to me i will help you.

Thanks,

Sri Hari

former_member205763
Active Contributor
0 Kudos
DATA : VAR1 LIKE REGUP-DMBTR, "-AMOUNT IN LOCAL CURRENCY
TAX like REGUP-MWSTS, " - TAX AMOUNT IN aLOcal curency
wht TYPE p value '0.05'.

*Constants wht type p decimals 2 value '0.05'.

FORM tax_calc TABLES p_input_table STRUCTURE itcsy " (table to hold the values passed from script)
p_output_table STRUCTURE itcsy. " (table to hold the values passed to script)
READ TABLE p_input_table WITH KEY name = 'VAR1' . " (should be in samecase as in script').
If sy-subrc = 0.
VAR1 = p_input_table-value.
VAR1 = VAR1 * wht. " (do whatever calculation you want)
Endif.
READ TABLE p_output_table WITH KEY name = 'TAX'.    "<-------- u missed the dot here hence the sy-subrc is not recognised

IF sy-subrc = 0. MOVE VAR1 TO p_output_table-value. " an error here "=..." expected after move .......(2)

MODIFY p_output_table INDEX sy-tabix.
Endif.
Endform.

use this code it works.

Edited by: Kartik Tarla on Feb 9, 2012 6:05 PM

Former Member
0 Kudos

Hi Sri ,

I appreciate your piece but you need to get what i want to achieve.

1) Am using a program ZRFFOUS_C which is an exact copy of RFFOUS_C

2) Where will your Piece be in the code that is (ZRFFOUS_C).Immediately after the

REPORT RFFOUS_C LINE-SIZE 132 MESSAGE-ID F0 NO STANDARD PAGE HEADING

OR

as will i create a subroutine and include it .

Either way i will appreciate if you can briefly explain how.

3) Apologies ... i may be wrong but Your piece of code seem not to really capture my concerns.

Again let me explain it -This field(REGUP-WRBTR) contain the gross payment.I want to calculate the tax, which is 0.05 of the content of (REGUP-WRBTR).Now i want the result (the calculated tax) displayed on the form

So from your piece will it be right to code like this

if sy-subrc eq 0.

CALCW = REGUP-WRBtr * 0.05 ?

Thanks for your patience and assistance !

Former Member
0 Kudos

Hi Tarla,

yeah thanks for the syntax correction but when i run it i get some error which av tried debugging all to no avail.It is

Short text = Unable to interprete '23.000,00' as a Number (this is tha amount i enterd @ invoice/payment)

The exception Analysis : CY_SY_CONVERSION_NO_NUMBER was not caught in procedure TAX_CALC

nor was it propagated by a raising clause.Since the caller of the procedure could not have anticipated that the exception could occur,the current program is terminated.

The reason for the exception is :

The program attempted to interprete the value "23.000" as anumber,but since the value contravences the rule for correct number format thhis was not possible

Source Code :

Line 41 : If sy_subrc = 0

>>>Line 42 : VAR1 = VAR1 * wht "It flags the error at this point

Line 43 : VAR1 = VAR1 * wht

Please this is what i have done

1) Am using a program ZRFFOUS_C which is an exact copy of RFFOUS_C

2) I inserted the Piece of the code into the copy (ZRFFOUS_C) immediately after the

REPORT RFFOUS_C LINE-SIZE 132 MESSAGE-ID F0 NO STANDARD PAGE HEADING.

3)will it be fine to insert it there or to create a subroutine and include. Either way i will appreciate if you can briefly explain .

Many thanks for time and patience !

former_member205763
Active Contributor
0 Kudos

if you have only one subroutine then its ok to have the subroutine in ZRFFOUS_C itself, but future requirement may require you to add more subroutines so its better not to populate the driver program, its better you create a different program in se38 of type subroutine pool, thus keep you driver program logic and subroutine logic different.

for the number conversion, use the fm MOVE_CHAR_TO_NUM, to convert the char type value to numeric type in proper format.

DATA: v_c TYPE char14 VALUE '23,000.02',
      v_q TYPE wrbtr.

CALL FUNCTION 'MOVE_CHAR_TO_NUM'
  EXPORTING
    CHR             = v_c
  IMPORTING
    NUM             = V_q
  EXCEPTIONS
    CONVT_NO_NUMBER = 1
    CONVT_OVERFLOW  = 2
    OTHERS          = 3.

"result =  23000.02 pass this back to subroutine and the script should take care of converting it back to output format with commas and dots

Edited by: Kartik Tarla on Feb 9, 2012 9:39 PM

Former Member
0 Kudos

Yes friend you are right.

You can calculate the code after sy-subrc check but the value must be passed to "OUT_PAR-value" because this is the variable which will be updated in the internal table "OUT_PAR". This table is passed to the smart form.

Also you can write the code after displaying the smartform. Because if you give at beginning it will show the below statement cannot be reached.

SO after the close form function you can give this peace of code.

READ TABLE OUT_PAR with key 'TOT'. "Read the table using the key for which you need to pass the value in this case it will be VAR1

if sy-subrc eq 0.

CALCW = 2 * 10. "Perform your calculation ex: REGUP-WRBtr * 0.05

OUT_PAR-value = CALCW. "Pass it to the output internal table variable

endif.

MODIFY OUT_PAR INDEX 1. "Modify the output internal table so the value will be passed to the script

If you face issues still please revert back to me.

Thanks,

Sri Hari

Edited by: srihari.kumar on Feb 10, 2012 12:31 PM

former_member205763
Active Contributor
0 Kudos

you have to do this calculation in a subroutine.

Former Member
0 Kudos

HI friend,

Also to declare a variable you can use DEFINE in Scripts. Declare doesn't work her.

Use the below code

/: DEFINE &MY_TAX& = &REGUP-WRBTR&

/: DEFINE &MY_TAX& := 0.05 * &REGUP-WRBTR&

&MY_TAX&

If you have any doubt revert back to me i will help you.

Thanks,

Sri Hari

Edited by: srihari.kumar on Feb 8, 2012 3:31 PM

Former Member
0 Kudos

Thanks Sri Hari.

I have applied your piece ... but the output on printing is 0 (zero) .

Once again what i want to achieve is a calculation at runtime and the calculated value to be displayed on the form.

This field &REGUP-WRBTR& contains the Gross amount and it displays a certain amount.

Now i want this amount to be multiplied by a %age (0.05) and the result to be displayed on the form.

2) please how do i display a preview of the form containing the required element.

Most times i print.i want to be able to preview the changes before printing.

Please i'll appreciate all form of assistance.

Former Member
0 Kudos

HI friend,

Correct i too got the same. Can i know how you are passing the value to the variable &REGUP-WRBTR&. Because this variable is 0 when i checked it in run time.

So just check whether this field is holding the value in debug mode.

In my case i did not pass the value for that variable from the driver program.

You try passing that value from the driver program and it will work fine. Else try to do the calculation in an subroutine and do.

If any issues please revert back to me i will help you.

Thanks,

Sri Hari

Edited by: srihari.kumar on Feb 8, 2012 5:00 PM

Harsh_Bansal
Contributor
0 Kudos

Hi,

In Sapscript if you want to assign some value, then use :=

eg. /:declare &my_tax& := 0.05

Then it will work.

Regards,

Harsh Bansal

Former Member
0 Kudos

Bansal ,

I get you but the requirement is to display a tax field on the form.This field should contain the computation of the gross field(&REGUP-WRBTR&) multiplied by 0.05.The output should be displayed on the form which is the tax.

Now if i declare like below

/:declare &my_tax& := 0.05

will it be fine to say

/:&my_tax& * &REGUP-WRBTR&

then how do i display the computation on the form.

2) please how do i display a preview of the form containing the required element.

Most times i print.i want to be able to preview the changes before printing.

Many thanks