cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with IF statement in SAP Script

hubert_heitzer
Contributor
0 Kudos

Hi all,

in my SAP Script form two IF statments are evaluated in different ways. Please see the image below.

Both LV_PSP_LEN and LV_HLTST_LEN are calculated in one ABAP subroutine via PERFORM ... ENDPERFORM call.

Seems like the second comparison uses only the first digit of LV_HLTST_LEN.

I tried this values:

LV_HLTST_LEN = 3 => processor evaluates IF &LV_HLTST_LEN& LE '3' to true

LV_HLTST_LEN = 9 => processor evaluates ELSEIF &LV_HLTST_LEN& LE '9' to true

LV_HLTST_LEN = 16 => processor evaluates IF &LV_HLTST_LEN& LE '3' to true

LV_HLTST_LEN = 44 => processor evaluates ELSEIF &LV_HLTST_LEN& LE '9' to true

Please give me some hints to solve my problem.

Regards,

Hubert

Accepted Solutions (1)

Accepted Solutions (1)

Jelena
Active Contributor
0 Kudos

In this case it's not the numbers that are getting compared but the alphanumeric strings. You can observe the same effect in Excel, for example, if you set Text type for the column. For the numbers the order would be:

3

9

44

But for the strings it's:

3

44

9

For the text type, the first character is compared first, then the next character and so forth.

The solution seems quite obvious though: since you only have few options in IF then instead of using a number (which does not work as expected) simply use some other indicator. E.g. 'A' would correspond to  <= 3 case, then 'B' to 3-9 case, etc. Something like that.

hubert_heitzer
Contributor
0 Kudos

Hi Jelena,

I understand your arguments, but

1. why should SAPscript use numbers to compare in case of LV_PSP_LEN but alpha numeric values in case of LV_HLTST_LEN?

I want SAPscript to use numbers to compare with LV_HLTST_LEN! Where is the problem?

2. I thought already before of your suggestion to use indicators, but this could only be a workaround, no permanent solution.

Regards,

Hubert

Rudy_Clement1
Participant
0 Kudos

Hi Hubert,

1) LV_PSP_LEN = 19. The IF-statement compares it with 17. As Jelena said, the characters are compared one-by-one:

1 LE 1 --> Yes

9 LE 7 --> No

Overall the outcome is no so that is why goes into the else part.

Kind regards,

Rudy.

@Jelana: thanks for your clarifying post. I haven't thought of that.

Jelena
Active Contributor
0 Kudos

Hubert, your screenshot does not confirm that it "works" with another variable. It just works with this specific value because 17 is less than 19 no matter whether you use text or numeric order. You just got lucky there.

This helped me realized a possible solution though - just add zero ('03'), that should make the text order work as numeric.

hubert_heitzer
Contributor
0 Kudos

Thanks Jelena and Rudy!

I will change my data delivery FORM and my SAPscript formular to work consequent with two-place numeric char-fields.

Kind Regards,

Hubert

Answers (1)

Answers (1)

Rudy_Clement1
Participant
0 Kudos

Hi Hubert,

What type of fields are LV_HLTST_LEN and LV_PSP_LEN? If they are numeric fields, maybe you should remove the quotes in the if/elseif conditions:

/: IF &LV_HLTST_LEN& LE 3

H1 ,,&LV_HLTST&

/: ELSEIF &LV_HLTST_LEN& LE 9

H2 ,,&LV_HLTST&

/: ELSEIF &LV_HLTST_LEN& LE 16

H3 ,,&LV_HLTST&

/: ELSE

H4 ,,&LV_HLTST&

/: ENDIF

Kind regards,

Rudy.

hubert_heitzer
Contributor
0 Kudos

Hi Rudy,

I don't know wich type SAPscript uses for variables but I think it should use the same type for LV_HLTST_LEN and LV_PSP_LEN.

The interface of the data-delivery-FORM is given by

FORM get_delnote_print_data TABLES in_tab  STRUCTURE itcsy
                                                         out_tab STRUCTURE itcsy.

where ITCSY-VALUE is of type CHAR(255).


The values for both LV_HLTST_LEN and LV_PSP_LEN is calculated like

READ TABLE out_tab ASSIGNING <ls_out_param> WITH KEY 'LV_HLTST'.
IF sy-subrc EQ 0.

    ...
    lv_size = STRLEN( <ls_out_param>-value ).
    READ TABLE out_tab ASSIGNING <ls_out_param> WITH KEY 'LV_HLTST_LEN'.
    IF sy-subrc EQ 0.
        WRITE lv_size TO <ls_out_param>-value.

        SHIFT <ls_out_param>-value LEFT DELETING LEADING space.

    ENDIF.

ENDIF.


thanks,

Hubert

Rudy_Clement1
Participant
0 Kudos

Hi Hubert,

The STRLEN function usually returns an integer. However, itcsy-value is indeed of type CHAR...

Can you try to remove the single quoites from the IF/ELSEIF conditions and see if it works then?

Kind regards,

Rudy.

hubert_heitzer
Contributor
0 Kudos

Hi Rudy,

checked, but did not work.

BTW:

- assigning interger to char converts the int to char

- same approach with LV_HLTST_LEN and LV_PSP_LEN: LV_PSP_LEN works fine, LV_HLTST_LEN does not.


Regards,

Hubert

Rudy_Clement1
Participant
0 Kudos

Hi Hubert,

You are right about the conversion to char. But I'm not sure if your first IF-statement really does work correct.. In the SAP help on the SAPScript IF-statement, there is a note:

  • If a syntax error occurs in the interpretation of this command, then the command is not executed. This may have an unexpected effect on the subsequent text output. For example, if the IF statement is incorrect, then all following ELSEIF and ELSE commands will be ignored, since the opening IF command is ‘missing’. This will cause all the text lines attached to the ELSEIF and ELSE commands to be printed.

According to this note it should print &LV_HLTST& multiple times then. I don't know if this is the case.

Anyway, I checked some standard SAP forms and noticed that they close the IF-statement with a period sign:

Maybe that is you syntax error? Can you try to add period sign at the end of each condition?

Kind regards,

Rudy.

hubert_heitzer
Contributor
0 Kudos

Hi again Rudy,

thanks for your continous support.

I checked the form with [Form] -> [Check] -> [Definition]. Result was both times (with and without period sign at end of IF, ELSEIF and ENDIF statement): "No errors found in form ZMM_ABRUF_LEFER"

According to this, the evaluation of the logical expressions did not change with period signs.

&LV_HLTST& is only printed once, with size of the paragraph selected by log.exp. (H1 in case of LV_HLTST_LEN = 16 )


Best regards,

Hubert