SAP Table for Cost Center Hierarchy and Groups: SETNODE / SETLEAF Tree Generator
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.
Fill in
0101 cost center group, 0102 cost element group, 0106 profit center group
Controlling area for 0101 and 0106, chart of accounts for 0102.
The standard hierarchy name is in TKA01-KHINR.
ABAP report 1: recursive read of SETNODE and SETLEAF
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 '0101',
gv_subclass TYPE setnode-subclass VALUE '1000',
gv_kokrs TYPE kokrs VALUE '1000',
gv_langu TYPE spras VALUE 'E',
gv_top TYPE setnode-setname VALUE 'H1'.
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
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 'H1',
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 '0101' = '0102'.
lv_ktopl = '1000'.
ELSE.
lv_kokrs = '1000'.
ENDIF.
CALL FUNCTION 'G_SET_ENCRYPT_SETID'
EXPORTING
setclass = '0101'
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 = 'E'
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.
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.
Get your hierarchy compared with CSKS
Send us your controlling area setup and we will check for cost centers outside the expected nodes and groups that overlap.
Optional. The tool and every export work without this. No spam.
Where groups and hierarchies are stored
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.
- 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
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.
- 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
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
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.
Frequently Asked Questions
Which SAP table stores the cost center hierarchy?
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?
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?
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?
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.
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.
Related Tools
SAP ECC Cost Center and Order Plan/Actual Cost ABAP Generator (COSP, COSS)
Enter controlling area, cost center or order, year, periods and version to get a report that sums plan and actual costs per cost element from COSP and COSS.
SAPSAP Cost Center Hierarchy Upload Worksheet
Lay out cost centers and their parent groups in one sheet, check lengths, and export the file for your upload tool.
SAPSAP Controlling (CO) Database Tables Reference
A searchable reference of the SAP tables behind cost centers, internal orders, product costing and material valuation.
SAPSAP Cost Center Assessment Allocation Calculator
Split a shared cost pool across receiver cost centers by headcount, area or any statistical key figure, and see the rate per unit.
More Developer Tools tools
See all- SAP ECC Enhancement Transactions and Exit Tables Reference (SMOD, CMOD, SE18, MODSAP, SXC_EXIT)
- SAP ECC Data Dictionary and Repository Tables Reference (DD02L, DD03L, T100, E070, TSTC)
- SAP ECC ABAP Runtime Errors (ST22 Dumps) and Error Log Transactions Reference
- SAP ECC Background Job History ABAP Query Generator (TBTCO + TBTCP)
- SAP ECC Background Job Creation ABAP Generator (JOB_OPEN, SUBMIT, JOB_CLOSE)
- SAP ECC IDoc Segment Data Reader ABAP Generator (EDIDC, EDID4, EDIDS)
- SAP ECC Custom Inbound IDoc Function Module Skeleton Generator
- SAP ECC BAPI_SALESORDER_CHANGE ABAP Generator
- SAP ECC BAPI_PO_CHANGE ABAP Generator
Keep exploring
Free tools
- SAP Cost Center Hierarchy Upload WorksheetLay out cost centers and their parent groups in one sheet, check lengths, and export the file for your upload tool.
- SAP ECC Cost Center and Order Plan/Actual Cost ABAP Generator (COSP, COSS)Enter controlling area, cost center or order, year, periods and version to get a report that sums plan and actual costs per cost element from COSP and COSS.
- SAP ECC BAPI_PO_CHANGE ABAP GeneratorGenerate a working ABAP program that changes an SAP purchase order item with BAPI_PO_CHANGE, with POITEM/POITEMX, schedule line date conversion, test run and commit handling.
ERP fixes and how-tos
- SAP S/4HANA Material Groups: Configure, Assign and ReportHow to define material groups in SAP S/4HANA (OMSF), assign them in MM01 and report on spend by group using tables T023, MARA-MATKL and EKPO-MATKL.
- How to add AI to SAP ECC 6.0 on-prem without moving data to a cloudReference architecture for a private on-prem AI assistant on SAP ECC 6.0: RFC and BAPI reads, SLT replica, retrieval index, local LLM, permission mirroring.
- SAP KI 235: G/L Account Requires an Assignment to a CO ObjectSAP KI 235 fires when a P&L G/L account has no cost element or the posting carries no valid CO account assignment. Fix it with KA01/KA02 or a default assignment.
Runs with your ERP
- Loftware with SAP ECC and S/4HANA: label design and printing from SAP transactionsHow Loftware and the NiceLabel ABAP package drive labels from SAP ECC and S/4HANA, including SAP GLM Plus, data flows, setup steps and pitfalls to avoid.
- Prometheus Planning & Scheduling with SAP Plant MaintenanceHow Prometheus Planning and Scheduling works with SAP Plant Maintenance work orders on S/4HANA and ECC: data flows, setup, pitfalls and a private AI layer.
Integrations
- How to use ChatGPT with SAP ECC 6.0Using ChatGPT with SAP ECC 6.0: custom GPT actions, exports versus live access, an OData or MCP bridge, enterprise data controls, and when a private model is safer.
- How to add AI agents to SAP ECC 6.0How to add AI agents to SAP ECC 6.0: a tool layer over BAPIs, RFC and IDocs, human approval before any posting, and an on-prem model that never leaves your network.