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

how to declare variables dynamically?

nmsmk
Explorer
0 Likes
3,011

Hi all,

I want to declare variables dynamically in the sense variable names should be dynamic. But I am not sure how to do it.

as an example,

data gv_index type char3.

do 10 times.

gv_index = sy-index.

concatenate 'var' gv_index into dtype.

data : (dtype) type char10.

enddo.

by the above code I intend to declare variables of type char10 whose names would be var1,var2,var3......var10.

but the system is not allowing me to do like this. can anyone of you please help me out.

Thanks & Regards,

Krishna.

7 REPLIES 7
Read only

kesavadas_thekkillath
Active Contributor
0 Likes
1,646

You can view my reply here

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,646

Hello Krishna,

You can define an internal table having a data-reference component to achieve this. And since you want your variables to of static type CHAR10 you need not use the class CL_ABAP_ELEMDESCR  as suggested by . Else you need to use the technique mentioned by him.

TYPES:
BEGIN OF ty_dyn_var,
  name TYPE string,
  data TYPE REF TO data,
END OF ty_dyn_var.

DATA: gv_indx     TYPE n LENGTH 2,
      gt_dyn_var  TYPE SORTED TABLE OF ty_dyn_var WITH UNIQUE KEY name,
      gwa_dyn_var TYPE ty_dyn_var.

* Create the data reference for the dyn. variables
CREATE DATA gwa_dyn_var-data TYPE c LENGTH 10.

* Fill the table with the dynamically created variables
DO 10 TIMES.
  gv_indx = sy-index.
  CONCATENATE 'VAR' gv_indx INTO gwa_dyn_var-name.
  INSERT gwa_dyn_var INTO TABLE gt_dyn_var.
ENDDO.

BR,

Suhas

Read only

kumud
Active Contributor
0 Likes
1,646

Hello,

If the intention is just to declare the variables dynamically, then your code works with little modification as below:

REPORT  ztest_chmuk.
DATA gv_index TYPE CHAR3.
DATA : dtype TYPE char10.


DO 10 TIMES.
   gv_index = sy-index.
   CONDENSE GV_INDEX.
   CONCATENATE 'var' gv_index INTO dtype .
ENDDO.

Not sure why were you giving (dtype) and not dtype? However, I think the code shown in the previous reply too should suffice your question!

Regards,

Kumud

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,646

In your case you've a static variable dtype will be assigned different values - var1, var2, ..., var10. How are 10 different variables created at runtime as required by the OP?

Read only

kumud
Active Contributor
0 Likes
1,646

Ok, so the question is not to declare 10 variables rather he is talking of defining something similar to an array! Yep, in that case what have I suggested is incorrect   . Then internal table with name-value pair field would be required.

Thanks.

Kumud

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
1,646

you can also do it like below, but i do not like it this way

define test.

   data:&1(&2) type c.

end-of-definition.

test var 10.

var = '1234567890'.

write var.

Read only

0 Likes
1,646

Kool