data l_sum type I.
sort itab1 by code.
loop at itab1 into wa.
at new code.
clear l_sum.
endat.
l_sum = l_sum + wa-qty.
if l_sum LE 5.
collect wa into itab2.
endif.
endloop.
delete itab2 where qty LT 3. -->itab2 will now just have all the records you need.
credit points if helpful. thanks.