How To Use Memory ID (ABAP Memory)

We can use memory id technique to export/import data to/from ABAP memory. ABAP memory is a memory area in SAP where all ABAP programs within the same session/LUW can has access. We can access/write the ABAP memory using the statement EXPORT and IMPORT.

Please find below an example on how to export and import data to/from the ABAP memory:

REPORT  zmemoryid_demo.

  lc_memidcprog           TYPE char11   VALUE \'ZMEMIDCPROG\'.

  lv_cprog                TYPE sy-cprog,
  lv_cprog_imported       TYPE sy-cprog.

*Assigning the program name for demo purposes.
lv_cprog = sy-cprog.

*Free the memory ID data before usage.
FREE MEMORY ID lc_memidcprog.

*Exporting the program name via memory id.
*Please note that \'LV_DUMMY_CPROG\' is just
*a dummy field, no need to declare it.
EXPORT lv_dummy_cprog FROM lv_cprog
TO MEMORY ID lc_memidcprog.

*For demo purposes, we will import the variable
*in the same program.
IMPORT lv_dummy_cprog TO lv_cprog_imported
FROM MEMORY ID lc_memidcprog.

IF sy-subrc EQ 0.

* If the import was successful, write the
* importing program name.
  WRITE lv_cprog_imported.

* Its good to free the memory ID data
* after usage.
  FREE MEMORY ID lc_memidcprog.


In the example code above, we have export the program name to memory id and the memory is then imported. The value imported is then display.

After executing the program above, we can noticed that the name of the program imported via memory id is being displayed on the report: