# SAP Table for Cost Center Hierarchy and Groups: SETNODE / SETLEAF Tree Generator

> Source: https://netray.co/tools/sap-ecc-cost-center-group-hierarchy-setnode-abap-generator
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Free Tools

## Short answer

Choose set class, controlling area or chart of accounts and the top node to get ABAP that prints the full group tree, expanding cost center ranges with names.

## Category

developer

## Erp

SAP ECC 6.0

## Audience

- ABAP developer
- SAP CO consultant
- Controller
- BI developer

## Tool

**Kind:** template

**Output:** code

**Language:** abap

**Fields:**

#### Set class

**Key:** setclass

**Type:** select

**Options:**

- 0101
- 0102
- 0106

**Default:** 0101

**Help:** 0101 cost center group, 0102 cost element group, 0106 profit center group

#### Subclass

**Key:** subclass

**Type:** text

**Default:** 1000

**Help:** Controlling area for 0101 and 0106, chart of accounts for 0102.

#### Top node (group or standard hierarchy name)

**Key:** top_node

**Type:** text

**Default:** H1

**Help:** The standard hierarchy name is in TKA01-KHINR.

#### Language (internal key)

**Key:** langu

**Type:** text

**Default:** E

**Blocks:**

#### ABAP report 1: recursive read of SETNODE and SETLEAF

**Body:** REPORT z_set_hierarchy_tree.

* Prints a cost center, cost element or profit center group as a tree
* from SETHEADERT, SETNODE and SETLEAF, recursively.
* Set class 0101 cost center group, 0102 cost element group,
* 0106 profit center group.
* SUBCLASS = controlling area for 0101 and 0106, chart of accounts for 0102.

DATA: gv_setclass TYPE setnode-setclass VALUE '{{setclass}}',
      gv_subclass TYPE setnode-subclass VALUE '{{subclass}}',
      gv_kokrs    TYPE kokrs VALUE '{{subclass}}',
      gv_langu    TYPE spras VALUE '{{langu}}',
      gv_top      TYPE setnode-setname VALUE '{{top_node}}'.

START-OF-SELECTION.
  PERFORM read_node USING gv_top 0.

FORM read_node USING iv_setname TYPE setnode-setname
                     iv_level   TYPE i.
  TYPES: BEGIN OF ty_cc,
           kostl TYPE csks-kostl,
           ktext TYPE cskt-ktext,
         END OF ty_cc.
  DATA: lt_nodes  TYPE STANDARD TABLE OF setnode,
        ls_node   TYPE setnode,
        lt_leaves TYPE STANDARD TABLE OF setleaf,
        ls_leaf   TYPE setleaf,
        lt_cc     TYPE STANDARD TABLE OF ty_cc,
        ls_cc     TYPE ty_cc,
        lv_text   TYPE setheadert-descript,
        lv_sub    TYPE setnode-setname,
        lv_indent TYPE i,
        lv_pos2   TYPE i,
        lv_next   TYPE i.

  lv_indent = iv_level * 2 + 1.
  lv_pos2   = lv_indent + 4.

  CLEAR lv_text.
  SELECT SINGLE descript FROM setheadert INTO lv_text
    WHERE setclass = gv_setclass
      AND subclass = gv_subclass
      AND setname  = iv_setname
      AND langu    = gv_langu.
  WRITE AT /lv_indent iv_setname.
  WRITE lv_text.

* Child groups in their maintained order
  SELECT * FROM setnode INTO TABLE lt_nodes
    WHERE setclass = gv_setclass
      AND subclass = gv_subclass
      AND setname  = iv_setname
    ORDER BY seqnr.
  lv_next = iv_level + 1.
  LOOP AT lt_nodes INTO ls_node.
    lv_sub = ls_node-subsetname.
    PERFORM read_node USING lv_sub lv_next.
  ENDLOOP.

* Value ranges assigned directly to this group
  SELECT * FROM setleaf INTO TABLE lt_leaves
    WHERE setclass = gv_setclass
      AND subclass = gv_subclass
      AND setname  = iv_setname
    ORDER BY seqnr.
  LOOP AT lt_leaves INTO ls_leaf.
    IF ls_leaf-valto IS INITIAL.
      ls_leaf-valto = ls_leaf-valfrom.
    ENDIF.
    IF gv_setclass = '0101'.
* Expand the range against cost centers valid today
      SELECT c~kostl t~ktext
        FROM csks AS c
        LEFT OUTER JOIN cskt AS t
          ON  t~kokrs = c~kokrs
          AND t~kostl = c~kostl
          AND t~datbi = c~datbi
          AND t~spras = gv_langu
        INTO TABLE lt_cc
        WHERE c~kokrs = gv_kokrs
          AND c~kostl BETWEEN ls_leaf-valfrom AND ls_leaf-valto
          AND c~datbi >= sy-datum
          AND c~datab <= sy-datum.
      LOOP AT lt_cc INTO ls_cc.
        WRITE AT /lv_pos2 ls_cc-kostl.
        WRITE ls_cc-ktext.
      ENDLOOP.
    ELSE.
      WRITE AT /lv_pos2 ls_leaf-valfrom.
      WRITE: 'to', ls_leaf-valto.
    ENDIF.
  ENDLOOP.
ENDFORM.

#### ABAP report 2: G_SET_ENCRYPT_SETID and G_SET_TREE_IMPORT

**Body:** REPORT z_set_hierarchy_fm.

* Alternative: read the whole group tree with function modules.
* G_SET_ENCRYPT_SETID builds the SETID; G_SET_TREE_IMPORT returns
* one SET_HIERARCHY row per node with LEVEL, SHORTNAME and DESCRIPT.

DATA: lv_setid TYPE sethier-setid,
      lv_short TYPE c LENGTH 24 VALUE '{{top_node}}',
      lv_kokrs TYPE sethier-kokrs,
      lv_ktopl TYPE sethier-ktopl,
      lt_hier  TYPE STANDARD TABLE OF sethier,
      ls_hier  TYPE sethier,
      lt_vals  TYPE STANDARD TABLE OF setvalues,
      lv_ind   TYPE i.

START-OF-SELECTION.

  IF '{{setclass}}' = '0102'.
    lv_ktopl = '{{subclass}}'.
  ELSE.
    lv_kokrs = '{{subclass}}'.
  ENDIF.

  CALL FUNCTION 'G_SET_ENCRYPT_SETID'
    EXPORTING
      setclass             = '{{setclass}}'
      shortname            = lv_short
      kokrs                = lv_kokrs
      ktopl                = lv_ktopl
    IMPORTING
      setid                = lv_setid
    EXCEPTIONS
      no_co_area_specified = 1
      illegal_setclass     = 2
      OTHERS               = 3.
  IF sy-subrc <> 0.
    WRITE: / 'Could not build set ID, sy-subrc', sy-subrc.
    RETURN.
  ENDIF.

  CALL FUNCTION 'G_SET_TREE_IMPORT'
    EXPORTING
      setid                     = lv_setid
      langu                     = '{{langu}}'
    TABLES
      set_hierarchy             = lt_hier
      set_values                = lt_vals
    EXCEPTIONS
      set_not_found             = 1
      illegal_field_replacement = 2
      illegal_table_replacement = 3
      set_is_damaged            = 4
      OTHERS                    = 5.
  IF sy-subrc <> 0.
    WRITE: / 'G_SET_TREE_IMPORT failed, sy-subrc', sy-subrc.
    RETURN.
  ENDIF.

  LOOP AT lt_hier INTO ls_hier.
    lv_ind = ls_hier-level * 2 + 1.
    WRITE AT /lv_ind ls_hier-shortname.
    WRITE ls_hier-descript.
  ENDLOOP.

**Notes:** Verify in SE11 the SETNODE, SETLEAF and SETHEADERT fields (SETCLASS, SUBCLASS, SETNAME, SUBSETNAME, SEQNR, VALFROM, VALTO, DESCRIPT) and in SE37 the G_SET_ENCRYPT_SETID and G_SET_TREE_IMPORT signatures on your release. Report 1 expands ranges to cost centers only for set class 0101. Leaves with VALOPTION other than EQ or BT (rare in CO groups) are printed as ranges without interpretation.

## Content

**Intro:** This generator writes ABAP for SAP ECC 6.0 that prints a cost center group, cost element group or profit center group as an indented tree, starting from any node such as the standard hierarchy. Report 1 reads the set tables directly and expands cost center ranges with names; report 2 uses the standard set function modules.

**Sections:**

#### Where groups and hierarchies are stored

**Body:** CO groups are sets. Each group is a row in SETHEADER with text in SETHEADERT, identified by set class, subclass and set name. SETNODE links a group to its child groups (SUBSETNAME) in maintained order (SEQNR). SETLEAF holds the values assigned directly to a group as single values or ranges (VALSIGN, VALOPTION, VALFROM, VALTO). The standard hierarchy of cost centers is simply the top group named in the controlling area (TKA01-KHINR, maintained in OKKP), so the same tables hold both the standard hierarchy and alternative groups.

**Bullets:**

- 0101: cost center groups, subclass = controlling area.
- 0102: cost element groups, subclass = chart of accounts.
- 0106: profit center groups, subclass = controlling area.

#### How to use it

**Body:** Pick the set class, enter the controlling area (or the chart of accounts for cost element groups), the top node and language. To print the whole standard hierarchy, use the name from TKA01-KHINR, or look it up in OKKP. Paste report 1 into SE38 and run it: every group prints indented by level with its text, followed by its cost centers, each with the name valid today. Report 2 returns the same structure through G_SET_TREE_IMPORT and is useful when you want the SAP-resolved tree rather than raw table rows.

**Bullets:**

- Transactions: KSH1 create, KSH2 change, KSH3 display cost center groups.
- OKEON changes the standard hierarchy in a graphical editor.
- KAH3 displays cost element groups, KCH3 profit center groups.

#### Showing all cost centers and finding orphans

**Body:** To list all cost centers created in a controlling area, read CSKS by KOKRS; each cost center can have several validity periods (DATAB, DATBI), so filter on today's date or take the latest. Every cost center must sit in the standard hierarchy, so expanding the standard hierarchy with report 1 and comparing with CSKS shows any center outside the expected nodes. CSKS-KHINR stores the standard hierarchy area (the node) each cost center is assigned to, which gives the same answer without walking the tree.

#### Gotchas

**Body:** SETNAME is unique only within set class and subclass, so always filter all three: the same group name can exist in two controlling areas. SETTYPE on SETHEADER separates basic sets from multi-sets, but it does not reliably tell the standard hierarchy from alternative groups; use TKA01-KHINR. Ranges in SETLEAF compare as text, so mixed alphanumeric cost center numbers can fall inside a range unexpectedly. Recursion depth equals the hierarchy depth, which is small in practice, so the recursive FORM is safe for consistent sets.

**Cta:** If controllers want to ask ECC which cost centers roll up where, and what they spent, in plain English on your own servers, we can build that over these set tables.

**Faq:**

#### Which SAP table stores the cost center hierarchy?

**Answer:** The hierarchy is stored as sets: SETNODE holds parent and child group links, SETLEAF the cost center values or ranges per group, SETHEADER and SETHEADERT the groups and their texts, all with set class 0101 and the controlling area as subclass. The top node is the standard hierarchy named in TKA01-KHINR.

#### Which table stores cost element groups?

**Answer:** The same set tables with set class 0102. The subclass is the chart of accounts, not the controlling area, because cost elements belong to the chart of accounts. Cost element texts are in CSKU by chart of accounts and language.

#### What is the standard hierarchy area of a cost center?

**Answer:** It is the node of the standard hierarchy to which the cost center is assigned, stored in CSKS-KHINR and shown as Hierarchy area in KS03. Every cost center must have one. The standard hierarchy itself is defined per controlling area in OKKP and maintained with OKEON or KSH2.

#### How do I show all cost centers created in SAP ECC 6.0?

**Answer:** KS13 or the cost center master data report lists them, and KSH3 shows them inside the standard hierarchy. From SQL, read CSKS for the controlling area with names from CSKT. Filter on DATAB and DATBI, because a cost center with changed time-dependent data has several rows.

## Search terms

sap table for cost center hierarchy, sap table for cost center group, sap ecc 6.0 cost center hierarchy area, sap table for cost element group, sap ecc 6.0 show all cost center created, setnode setleaf setheadert, g_set_tree_import, cost center standard hierarchy table

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
