‎2006 Oct 18 11:11 AM
Hello experts,
Could you help me with this.
Here's the scenario:
col1 col2
AA 01
AA 00
AA 01
BB 00
BB 01
BB 00
I wanted to know how many 01s and 00s per column 1. so in that scenario.. the result should be:
AA 2 1
BB 1 2
Column2: for all 01
Column3: for all 00
Thanks in advance..
‎2006 Oct 18 11:17 AM
Hi
Hope this helps - I've not tested it but it should work you just need to declare the count variables.
Kind regards
Andy
SORT itab BY col1 col2
LOOP AT itab.
IF itab-col2 = '00'.
count_0 = count_0 + 1.
ELSEIF itab-col2 = '01'.
count_1 = count_0 + 1.
AT END col1.
WRITE: /itab-col1, count_1, count_2.
count_0 = 0.
count_1 = 0.
ENDAT.
ENDLOOP.
‎2006 Oct 18 11:17 AM
Hi
Hope this helps - I've not tested it but it should work you just need to declare the count variables.
Kind regards
Andy
SORT itab BY col1 col2
LOOP AT itab.
IF itab-col2 = '00'.
count_0 = count_0 + 1.
ELSEIF itab-col2 = '01'.
count_1 = count_0 + 1.
AT END col1.
WRITE: /itab-col1, count_1, count_2.
count_0 = 0.
count_1 = 0.
ENDAT.
ENDLOOP.
‎2006 Oct 18 11:18 AM
Hi Lana,
try it with collect.
copy your internal table to itab1 an add
a field like I type I.
loop at itab.
itab1 = itab.
itab1-i = 1.
collect itab1.
endloop.
in Itab1 you get your Information.
Regards, Dieter
‎2006 Oct 18 11:22 AM
define itab with fields col1 cnt0 cnt1.
itable->is the internal table with your data.
sort itable.
Loop at itable.
at new col1.
clear : lcnt0, lcnt1.
endat.
if itable-col2 = '00'.
add 1 to lcnt0.
elseif itable-col2 = '01'.
add 1 to lcnt1.
endif.
at end of col1.
move itable-col1 to itab-col1.
move lcnt0 to itab-cnt0.
move lcnt1 to itab-cnt1.
append itab.
endat.
Endloop.
itab has the data you require
‎2006 Oct 19 3:36 AM
Hi anurag,
thanks.. that was a very helpful tip..
btw, we are trying to eliminate the use of AT NEW and in the coding.. instead were using ON CHANGE OF and ENDON to close the statement..
I wonder wat could be the alternate for:
AT NEW (ON CHANGE OF)
ENDAT (ENDON)
AT END ???
thanks.
‎2006 Oct 19 9:18 AM
Hi
To replicate the END AT you will need to record the relevant column data on each iteration of the loop. Then on the next iteration you can compare the current field with the last one to see if they have changed. Something like the following:
DATA:
tmp_field TYPE itab-col1.
LOOP AT itab.
* This is like AT END
IF tmp_field <> itab-col1 AND
NOT tmp_field IS INITIAL.
* Do stuff here...
END IF.
tmp_field = itab-col1.
ENDLOOP.
However, why are you trying to eliminate the AT control breaks and replace them with ON CHANGE OF? As I understand it, SAP do not recommend using ON CHANGE OF for two reasons:
1. It does not work with ABAP objects.
2. It can produce unpredicatable results when used within a Loop structure. The following is extracted from the SAP online help:
"There are special control structures for processing control breaks in LOOP s on internal tables or extract datasets (AT).
ON CHANGE OF is unsuitable for recognizing control levels in loops of this type because it always creates a global auxiliary field which is used to check for changes. This global auxiliary field is only changed in the relevant ON CHANGE OF statement. It is not reset when the processing enters loops or subroutines, so unwanted effects can occur if the loop or subroutine is executed again. Also, since it is set to its initial value when created (like any other field), any ON CHANGE OF processing will be executed after the first test, unless the contents of the field concerned happen to be identical to the initial value. "
Hope this helps
Andy
‎2006 Oct 19 10:00 AM
I would advice you to use AT events rather than ON CHANGE, as they are now obselte. But in case you still wish to use them here is the code.
sort itable.
read table itable index 1.
Loop at itable.
if itable-col2 = '00'.
lcnt0 = lcnt0 + 1.
elseif itable-col2 = '01'.
lcnt1 = lcnt1 + 1.
endif.
on change of itable-col1 or itable-col2.
move itable-col1 to itab-col1.
move lcnt0 to itab-cnt0.
move lcnt1 to itab-cnt1.
append itab.
endon.
Endloop.
Message was edited by: Anurag Bankley
‎2006 Oct 18 11:23 AM
Hi,
Pl find eg below.
LOOP AT internal table .
l_count = l_count + 1.
at end of column 1 .
<logic >
sum.
on change of internal table- column 1.
endon.
endat.
ENDLOOP.
Pl reward if it helps.
Cheers.
‎2006 Oct 18 11:33 AM
Hi,
follow below logic.
sort itab by col1.
data:begin of itab1 occurs 0 ,
col1(2) type c,
count00 type i,
count01 type i,
end of itab1.
loop at itab.
at new col1.
clear lv_cnt,lv_cnt1.
endat.
if itab-col2 = '00'
lv_cnt = lv_cnt + 1.
elseif itab-col2 = '01'
lv_cnt1 = lv_cnt1 + 1.
endif.
. at end of col1.
itab1-col1 = itab-col1.
itab1-count00 = lv_cnt.
itab1-count01 = lv_cnt1.
append itab1.
clear itab1.
endat.
endloop.
loop at itab1.
write:itab1.
endloop.
itab1 will have output
AA 2 1
BB 1 2
Regards
amole
‎2006 Oct 18 11:39 AM
check this code it may help u.
report z_example.
data:begin of itab occurs 0,
col1(3) type c,
col2(3) type c,
end of itab.
data:begin of itab1 occurs 0,
col1(3) type c,
count1 type i,
count2 type i,
end of itab1.
itab-col1 = 'AA'.
itab-col2 = '01'.
append itab.
itab-col1 = 'AA'.
itab-col2 = '00'.
append itab.
itab-col1 = 'AA'.
itab-col2 = '01'.
append itab.
itab-col1 = 'BB'.
itab-col2 = '01'.
append itab.
itab-col1 = 'BB'.
itab-col2 = '00'.
append itab.
sort itab by col1.
loop at itab.
at new col1.
itab1-col1 = itab-col1.
endat.
if itab-col2 = '01'.
itab1-count1 = itab1-count1 + 1.
elseif itab-col2 = '00'.
itab1-count2 = itab1-count2 + 1.
endif.
at end of col1.
append itab1.
clear itab1.
endat.
endloop.
loop at itab1.
write:/ itab1-col1,itab1-count1,itab1-count2.
endloop.
Regards
‎2006 Oct 18 12:38 PM
Hi,
Below is the source code for this scenario.
Maintain swap internal table and also Counts internal table as shown in the below source code.
report abc.
data:
v_count type i,
v_tabix type sy-tabix.
*-- Actual Data
data:begin of it_actual occurs 0,
f1(3) type c,
f2(3) type c,
end of it_actual.
*-- Swap Actual Data
data:begin of it_swap occurs 0,
f1(3) type c,
f2(3) type c,
end of it_swap.
*-- Holds Counts for sorted data
data:begin of it_sort occurs 0,
col1(3) type c,
col2(3) type c,
count type i,
end of it_sort.
start-of-selection.
*-- Append Test Data
perform append.
*-- Sort Data
perform sort.
&----
*& Form append
&----
text
----
--> p1 text
<-- p2 text
----
FORM append .
it_actual-f1 = 'AA'.
it_actual-f2 = '01'.
append it_actual.
it_actual-f1 = 'AA'.
it_actual-f2 = '00'.
append it_actual.
it_actual-f1 = 'AA'.
it_actual-f2 = '01'.
append it_actual.
it_actual-f1 = 'BB'.
it_actual-f2 = '00'.
append it_actual.
it_actual-f1 = 'BB'.
it_actual-f2 = '01'.
append it_actual.
it_actual-f1 = 'BB'.
it_actual-f2 = '00'.
append it_actual.
*-- Your actual format
loop at it_actual.
write:/ it_actual-f1, it_actual-f2.
endloop.
skip 2.
*-- Populate other table which swaps the fields
loop at it_actual.
it_swap-f1 = it_actual-f2.
it_swap-f2 = it_actual-f1.
append it_swap.
clear it_swap.
endloop.
ENDFORM. " append
&----
*& Form sort
&----
text
----
--> p1 text
<-- p2 text
----
FORM sort .
*-- Sort as per requirement
sort it_swap by f1 f2.
loop at it_swap.
write:/ it_swap-f1, it_swap-f2.
endloop.
loop at it_swap.
*-- Populate Loop index
v_tabix = sy-tabix.
at new f2.
*-- Initialize counter
clear v_count.
endat.
*-- Add counter
v_count = v_count + 1.
at end of f2.
*-- Read to avoid STARS
read table it_swap index v_tabix.
*-- Append other table
it_sort-col1 = it_swap-f2.
it_sort-col2 = it_swap-f1.
it_sort-count = v_count.
append it_sort.
clear it_sort.
endat.
endloop.
*-- Now you have the below Internal table which holds counts
skip 2.
loop at it_sort.
write: / it_sort-col1, it_sort-col2, it_sort-count.
endloop.
*-- You can sort this again to have the proper output
skip 2.
sort it_sort by col1 col2.
loop at it_sort.
v_tabix = sy-tabix.
at new col1.
write: / it_sort-col1.
endat.
at end of col2.
read table it_sort index v_tabix.
write: it_sort-count.
endat.
endloop.
ENDFORM. " sort
‎2006 Oct 19 7:33 AM
Hi,
data:begin of itab occurs 0,
cl1(2) type c,
cl2(2) type c,
end of itab.
data:begin of itab2 occurs 0,
cl1(2) type c,
ca0 type i,
ca1 type i,
end of itab2.
data:ca0 type i value 0,ca1 type i value 0,
count type i value 0.
itab-cl1 = 'AA'.
itab-cl2 = '01'.
append itab.
itab-cl1 = 'AA'.
itab-cl2 = '00'.
append itab.
itab-cl1 = 'AA'.
itab-cl2 = '01'.
append itab.
itab-cl1 = 'BB'.
itab-cl2 = '00'.
append itab.
itab-cl1 = 'BB'.
itab-cl2 = '01'.
append itab.
itab-cl1 = 'BB'.
itab-cl2 = '00'.
append itab.
<b>sort itab.
loop at itab.
on change of itab-cl1.
clear: ca0,ca1,count.
endon.
if itab-cl2 = '00'.
ca0 = ca0 + 1.
else.
ca1 = ca1 + 1.
endif.
perform outdata.
endloop.</b>
&----
*& Form outdata
&----
text
----
--> p1 text
<-- p2 text
----
form outdata .
if count > 0.
if itab2-cl1 <> itab-cl1.
itab2-cl1 = itab-cl1.
itab2-ca0 = ca0.
itab2-ca1 = ca1.
append itab2.
else.
itab2-cl1 = itab-cl1.
itab2-ca0 = ca0.
itab2-ca1 = ca1.
append itab2.
write:/ itab2-cl1,itab2-ca0,itab2-ca1.
endif.
endif.
count = count + 1.
endform. " outdata
Please do reward points if it solves your problem
Regards,
Sowjanya
‎2006 Oct 19 7:49 AM
or in simple way without using any control breaks
loop at itab.
if itab-cl1 = 'AA'.
if itab-cl2 = '00'.
ca0 = ca0 + 1.
else.
ca1 = ca1 + 1.
endif.
else.
if itab-cl2 = '00'.
cb0 = cb0 + 1.
else.
cb1 = cb1 + 1.
endif.
endif.
endloop.
Regards,
Sowjanya