Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Collect statement error

Former Member
0 Likes
1,064

Hi all,

i have the fallowing problem.

I'm trying to do a collect statement as fallow:

SORT i_tran BY cuenta ASCENDING.
LOOP AT i_tran.             
                            
AT NEW cuenta.              
COLLECT i_tran INTO i_tran2.
APPEND i_tran2.             
CLEAR i_tran.               
CLEAR i_tran2.              
ENDAT.                      
endloop.

where i_tran is a collect internal table and i_tran2 is another internal table.

when I execute the collect statement all the fields are fill with asterics and the field that Im interested in is set to zero.

another problem is that is not doing the collect per cuenta. (is doing it per record, ex. if cuenta 123 has three records it executes at the collect statement for each one of them.

this is the structure of i_tran:

* Registro de Transacciones
TYPES: BEGIN OF E_TRAN,
      RECTYPE TYPE C,             "Tipo Registro N
      CCODE(15) TYPE C,           "Id Empresa (RNC)
      SEQNR(7) TYPE C,            "Secuencia Header
      SECTR(7) TYPE C,            "Secuencia Transaccion
      CUENTA(20) TYPE C,          "Cuenta Suplidor
      MONTRN TYPE I,              "Montro Transaccion (13)
      TIPCTA TYPE C,              "Tipo de Cuenta
      WAERS(3) TYPE C,            "Moneda
      CODBAN(8) TYPE C,           "Banco Destino (No. Cuenta Banco)
      DIGVER TYPE C,              "Digito Verificacion
      CODOPR(2) TYPE C,           "Codigo Operacion
      TIPIDN(2) TYPE C,           "Tipo de Identificacion
      IDENTF(15) TYPE C,          "Identificacion
      NOMBRE(35) TYPE C,          "Nombre Beneficiario
      REFERN(12) TYPE C,          "Referencia
      DESCRED(40) TYPE C,         "Descripcion Estado Destino
      FECVEN(4) TYPE C,           "Fecha Vencimiento
      FCONTC TYPE C,              "Forma de Contacto
      EMAIL(40) TYPE C,           "E-mail Beneficiario
      NUMFAX(12) TYPE C,          "Numero de Fax
      RESERV(2) TYPE C,           "Reservado para uso futuro
      NUMAUT(15) TYPE C,          "Numero Autorizacion
      CODRET(3) TYPE C,           "Codigo Retorno Remoto
      CODRZR(3) TYPE C,           "Codigo Razon Remoto
      CODRZI(3) TYPE C,           "Codigo Razon Interno
      PROCTR TYPE C,              "Procesador Transaccion
      STATS(2) TYPE C,            "Status Transaccion
      FILLER(52),                 "En blanco
END OF E_TRAN.
DATA: IT_TRAN TYPE  E_TRAN.
DATA: I_TRAN TYPE STANDARD TABLE OF E_TRAN WITH HEADER LINE,
      I_TRAN2 TYPE STANDARD TABLE OF E_TRAN WITH HEADER LINE.

if anyone can help me I'll appreciated.

thanks in advanced,

Gregorio.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
793

Hi again,

1. As u have said, u want totals

per cuenta (only cuenta)

2. do somewhat like this.

3. Declare ITAB1 having CUENTA, and other number fields

(for whom u want total)

4. Declare another table, same as ITAB1

(say SUMTAB, for collect purpose)

5. Now Loop at your original table i_tran2 .

CLEAR ITAB1.

MOVE-CORRESPONDING I_TRAN2 TO ITAB1.

APPEND ITAB1.

6. NOW FINALLY,

Loop at itab1.

collect itab1 into sumtab.

endloop.

This will achieve what u want.

I have tried such thing

and it works fantastic.

regards,

amit m.

5 REPLIES 5
Read only

suresh_datti
Active Contributor
0 Likes
793

Hi Gregorio,

You have only one field 'MONTRN' in the table.. all the rest are CHAR fields.. I guess you want to total this field for eahc 'CUENTA'. In that case, you don't need to use COLLECT (it is an expensive statement). Also, when you use AT NEW, you must ensure that the CHAR fields are to the left of the field you are controlling on.( this is the reason for the ASTERICS you see). In your type declaration, move the CUENTA as the last field.You can then use the SUM statement. I manot logged into SAP, otherwise, I would have put in some code.

Regards,

Suresh Datti

Read only

Former Member
0 Likes
793

Hi Gergorio,

1. I think that the way you have used COLLECT

is incorrect.

(There is no need to use AT NEW for collect)

(only Loop, Collect will do the work)

2. U have said that :

tran is a collect internal table and i_tran2 is

another internal table.

3. Meaning, that,

i_trans2 has many records

tran is the SUMMARY table (to gather totals)

4. Now, on what base,

do u want to do summary ?

ie. What will be the key/main/criteria fields?

(all char fields of your internal table,

or some of them)

(If only some are required, then your logic won't work)

5. Moreover, we should have numeric/integer

fields in our internal table so that

summation is done automatically.

6. The simple logic is like this.

LOOP AT DETAILITAB.

COLLECT DETAILTAB INTO SUMMARYTAB.

ENDLOOP.

7. The above will take care of

all summations

(with respect to DISTINCT COMBAINATION

of ALL CHAR FIELDS)

8. If your criteria for summation is

less fields then some little more work

is required. If this is so, let me know.

I hope it helps.

Regards,

Amit M.

Read only

Former Member
0 Likes
794

Hi again,

1. As u have said, u want totals

per cuenta (only cuenta)

2. do somewhat like this.

3. Declare ITAB1 having CUENTA, and other number fields

(for whom u want total)

4. Declare another table, same as ITAB1

(say SUMTAB, for collect purpose)

5. Now Loop at your original table i_tran2 .

CLEAR ITAB1.

MOVE-CORRESPONDING I_TRAN2 TO ITAB1.

APPEND ITAB1.

6. NOW FINALLY,

Loop at itab1.

collect itab1 into sumtab.

endloop.

This will achieve what u want.

I have tried such thing

and it works fantastic.

regards,

amit m.

Read only

Former Member
0 Likes
793

Hi,

when using AT NEW , u have to read the internal table

IF U DONT USE READ STATEMENT , IT WILL DISPLAY AS ASTREIK.

LOOP AT I_TRAN.

L_TABIX = SY_TABIX.

AT NEW cuenta.

<b>READ TABLE i_tran index l_TABIX.</b> ENDAT.

ENDLOOP.

Read only

Former Member
0 Likes
793

Change your code to this.


SORT i_tran BY cuenta ASCENDING.
LOOP AT i_tran.
  COLLECT i_tran INTO i_tran2.
  CLEAR i_tran2.
ENDLOOP.

Or if for some reason, you need to do the collect only for new CUENTA, then you have to change your i_tran2 to have only two fields, CUENTA and MONTRN. This is the only way you can get the totals for unique CUENTA.

SORT i_tran BY cuenta ASCENDING.
LOOP AT i_tran.
  COLLECT i_tran INTO i_tran2.
  CLEAR i_tran2.              
ENDLOOP.

Srinivas