Former Member

# related to a logic in reports

hello experts,

i have a small issue...i have 3 numbers...suppose 10 14 and 18 and i got one more number 15 which is to be compared with the first 3 values...this 15 dont match with any of the 3 values(10,14 and 18)...in this case i got to print the nearest value i.e., 14....

plz tell me the logic how i can do that

10|10000 characters needed characters exceeded

### Related questions

• Posted on Sep 12, 2007 at 07:11 AM

PARAMETERS znum TYPE i.

data: znum1 TYPE i, znum2 TYPE i, znum3 TYPE i.

data:diff1 type i, diff2 type i, diff3 type i.

znum1 = 10.

znum2 = 14.

znum3 = 18.

diff1 = abs( znum1 - znum ).

diff2 = abs( znum2 - znum ).

diff3 = abs( znum3 - znum ).

if diff1 lt diff2 and diff1 lt diff3.

WRITE / znum1.

ELSEIF diff2 lt diff1 and diff2 lt diff3.

WRITE / znum2.

elseif diff3 lt diff1 and diff3 lt diff2.

WRITE / znum3.

endif.

plz reward points if dis helps

Message was edited by:

abapuser

10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 12, 2007 at 07:15 AM

Hi vinil,

data: x1 type i value 10,

x2 type i value 20,

x3 type i value 30,

x4 type i value 23.

if x4 BETWEEN X1 AND X2.

WRITE X1.

ELSEIF X4 BETWEEN X2 AND X3.

WRITE X2.

ELSE.

WRITE X4.

ENDIF.

10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 12, 2007 at 07:17 AM

Hello Vinil,

If you have n numbers... Just put those into an internal table with one filed.

data: begin of itab occurs 0,

num type i,

end of itab.

itab-num = 10. append itab. clear itab.

itab-num = 14. append itab. clear itab.

itab-num = 18. append itab. clear itab.

itab-num = 15. append itab. clear itab.

now sort the itab. store the last entered value.

now loop at the internal table... if last entered value occurs just print sy-tabix - 1 th record. this is the nearest value of last enterd value.

Reward If Useful.

Regards

--

Sasidhar Reddy Matli.

10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 12, 2007 at 07:23 AM

Hi,

try this logic this will be dynamic

PARAMETERS znum TYPE i.

DATA: BEGIN OF itab OCCURS 0,

num TYPE i,

END OF itab.

DATA: diff TYPE i.

itab-num = '10'.

APPEND itab.

itab-num = '20'.

APPEND itab.

itab-num = '12'.

APPEND itab.

itab-num = '13'.

APPEND itab.

SORT itab BY num.

data: diffc type i.

data: begin of itab1 occurs 0,

number type i,

differ type i,

end of itab1.

loop at itab.

if itab-num > znum.

diff = itab-num - znum.

else.

diff = znum - itab-num.

endif.

itab1-number = itab-num.

itab1-differ = diff.

clear: diff.

append itab1.

endloop.

sort itab1.

if sy-subrc eq 0.

write:/ itab1-number.

endif.

regards,

Venkatesh

10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 12, 2007 at 07:30 AM

hi,

try like this,

data: no1 type i,

no2 type i,

no3 type i,

no type i,

diff1 type i,

diff2 type i,

diff 3 type .

diff1 = abs(no1 - no).

diff2 = abs(no2 -no).

diff3 = abs(no3 - no).

if diff1 = diff2 or diff1 =diff3.

write:/10 no,' IS IN THE RANGE OF',no1,no2,no3.

else.

if diff2 = diff3.

write:/10 no,' IS IN THE RANGE OF',no1,no2,no3.

else.

write:/10 no-1.

endif.

WITH REGARDS,

SURESH ALURI.