SAP Help Information and source code of how to implement the varrying command









VARYING command - Example ABAP code to demonstrate the VARYING command

The SAP / ABAP varying command is a function of the DO loop which allows you to move horizontaly along a table row without actually having to specify each individual field name. i.e. if the table row had the fields: Field1 Field2 Field3 Field4 etc you would be able to loop around each field using only one DO..VARYING command.


*Code to demonstrate VARYING command
*  The varying comand works on the position within the row, therfore
*  each field must be the same number of fields apart.
DATA: ld_p0121 type p0121-rfp01,
      ld_numcont type i.


  DO 21 TIMES VARYING ld_p0121 FROM p0121-RFP01 NEXT p0121-RFP02.
    IF not ld_p0121 IS INITIAL.
      ld_numcont = ld_numcont + 1.
    ENDIF.
  ENDDO.


*Code to demonstrate multiple field VARYING command
DATA: ld_kst0x LIKE p0027-kst02,
      ld_auf0x LIKE p0027-auf02,
      ld_psp0x LIKE p0027-psp02.

  DO 25 TIMES VARYING ld_kst0x FROM p0027-kst02 NEXT p0027-kst03
              VARYING ld_auf0x FROM p0027-auf02 NEXT p0027-auf03
              VARYING ld_psp0x FROM p0027-psp02 NEXT p0027-psp03.

    PERFORM get_cost_center_and_text USING ld_kst0x
                                           ld_auf0x
                                           ld_psp0x.
    APPEND output_tab.
  ENDDO.