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

Declaring a variable of a type stored in a string variable

Former Member
0 Likes
2,328

Hi Gurus,

my need is to declare a variabile of a type stored in another string variable. Eg:

DATA: my_type(50) TYPE C.

my_type = 'MATNR'.

Now I want to declare another variable my_var of type MATNR (I fill the variable my_type at runtime)

Any ideas?

Thank you

Francesco

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,239

Answer by Susmitha is the way to do it. Further part of accessing it using field symbols is described below :

Creating variable dynamically :

DATA      my_var      TYPE REF TO      DATA.

CREATE DATA   my_var     TYPE     (my_type).

Accessing the dynamically created variable :

FIELD-SYMBOLS     <fs_my_var>     TYPE     (my_type). "(or u can use TYPE ANY)

ASSIGN     my_var->*     TO     <fs_my_var>.

Assigning values :

<fs_my_var> = xyz-matnr.

WRITE     <fs_my_var>.

Regards,

Ashish

5 REPLIES 5
Read only

Former Member
0 Likes
2,239

You can dynamically create data using data reference.

data dyn_field type ref to data.


DATA: my_type TYPE string.

my_type = 'MATNR'.


create data dyn_field type (my_type).


You then access it using field symbols.


Check resources on dynamic data declarations, you will find plenty of them.

Read only

Former Member
0 Likes
2,240

Answer by Susmitha is the way to do it. Further part of accessing it using field symbols is described below :

Creating variable dynamically :

DATA      my_var      TYPE REF TO      DATA.

CREATE DATA   my_var     TYPE     (my_type).

Accessing the dynamically created variable :

FIELD-SYMBOLS     <fs_my_var>     TYPE     (my_type). "(or u can use TYPE ANY)

ASSIGN     my_var->*     TO     <fs_my_var>.

Assigning values :

<fs_my_var> = xyz-matnr.

WRITE     <fs_my_var>.

Regards,

Ashish

Read only

Former Member
0 Likes
2,239

Thank you very much, now an additional question: I need to declare a type like this:

TYPES:
   BEGIN OF gt_s_data2,
     my_var
     FIELD1 TYPE c,
     FIELD2 TYPE c,
   END OF gt_s_data2.


where "my_var" is the variable dinamically declared before. How can I achieve this?


Thank you

Francesco

Read only

0 Likes
2,239

For that, create a dynamic internal table.

First create the structure and then the table.

Check this doc for reference.

Read only

Former Member
0 Likes
2,239

Hi Francesco,

There is a function module for that that should always be used:

CONVERSION_EXIT_MATN1_INPUT

That domain MATNR, as several others, has a conversion rule, namely MATN1. All fields with a conversion rule when read from a char like field should be converted to SAP internal format using funtion CONVERSION_EXIT_xxxxx_INPUT.

I guess this is not the point, so my suggestion to create your dynamic variable is to do the following:

DATA: l_type(30),

       l_text(10) VALUE '123'.

DATA: BEGIN OF ls_data,

         lr_field TYPE REF TO data,

         field1 TYPE c,

         field2 TYPE c,

       END OF ls_data.

FIELD-SYMBOLS <l_field> TYPE any.

l_type = 'MATNR'.

CREATE DATA ls_data-lr_field TYPE (l_type).

ASSIGN ls_data-lr_field->* TO <l_field>.

<l_field> = l_text.

you could use the create structure only part suggested by Susmitha, but if you're not displaying this data it might not be necessary.

regards,

Edgar