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

Dynamic Variable Declaration

Former Member
0 Likes
10,167

Hi Experts,

Is there any way in ABAP to declare a variable based on user input.

Like below code.


DATA:var1 TYPE string.
DATA :varx(25) TYPE c.
PARAMETERS :  varname(20) TYPE c,
vartype TYPE c,
varlen(2) TYPE c,
vardec(2) TYPE c.

START-OF-SELECTION.
  CONCATENATE varname '('  varlen ')' INTO varx.
  CONDENSE varx NO-GAPS.
  IF vartype = 'P' AND vardec IS NOT INITIAL.
    CONCATENATE  'Data:' varx 'Type' vartype 'Decimals' vardec '.' INTO var1 SEPARATED BY space.
  ELSE.
    CONCATENATE 'Data:' varx 'Type' vartype '.' INTO var1 SEPARATED BY space.
  ENDIF.

  WRITE: var1.

1 ACCEPTED SOLUTION
Read only

kesavadas_thekkillath
Active Contributor
5,112

If you very much keen about the name entered you can check with the code below else you can remove the varaible name from screen and assign it to a field symbol of type any. Check the other methods available in class cl_abap_elemdescr


PARAMETERS :  varname(20) TYPE c,
              vartype TYPE c,
              varlen TYPE i,
              vardec TYPE i.

DEFINE mcr.
  field-symbols:<&1> type any.
END-OF-DEFINITION.

DEFINE write_val.
  assign wf_ref->* to <&1>.
  check <&1> is assigned.
  <&1> = '1234'.
  write: <&1>.
END-OF-DEFINITION.

DATA:data_type TYPE REF TO cl_abap_elemdescr,
     wf_ref TYPE REF TO data.

START-OF-SELECTION.

  mcr varname.
  IF vartype = 'P'.
    TRY.
        CALL METHOD cl_abap_elemdescr=>get_p
          EXPORTING
            p_length   = varlen
            p_decimals = vardec
          RECEIVING
            p_result   = data_type.
      CATCH cx_parameter_invalid_range .
    ENDTRY.
  ELSEIF vartype = 'I'.
    CALL METHOD cl_abap_elemdescr=>get_i
      RECEIVING
        p_result = data_type.
  ELSEIF vartype = 'C'.
    TRY.
        CALL METHOD cl_abap_elemdescr=>get_c
          EXPORTING
            p_length = varlen
          RECEIVING
            p_result = data_type.
      CATCH cx_parameter_invalid_range .
    ENDTRY.

  ENDIF.

  CREATE DATA wf_ref TYPE HANDLE data_type.
  CHECK wf_ref IS BOUND.
  write_val varname.

7 REPLIES 7
Read only

kesavadas_thekkillath
Active Contributor
5,113

If you very much keen about the name entered you can check with the code below else you can remove the varaible name from screen and assign it to a field symbol of type any. Check the other methods available in class cl_abap_elemdescr


PARAMETERS :  varname(20) TYPE c,
              vartype TYPE c,
              varlen TYPE i,
              vardec TYPE i.

DEFINE mcr.
  field-symbols:<&1> type any.
END-OF-DEFINITION.

DEFINE write_val.
  assign wf_ref->* to <&1>.
  check <&1> is assigned.
  <&1> = '1234'.
  write: <&1>.
END-OF-DEFINITION.

DATA:data_type TYPE REF TO cl_abap_elemdescr,
     wf_ref TYPE REF TO data.

START-OF-SELECTION.

  mcr varname.
  IF vartype = 'P'.
    TRY.
        CALL METHOD cl_abap_elemdescr=>get_p
          EXPORTING
            p_length   = varlen
            p_decimals = vardec
          RECEIVING
            p_result   = data_type.
      CATCH cx_parameter_invalid_range .
    ENDTRY.
  ELSEIF vartype = 'I'.
    CALL METHOD cl_abap_elemdescr=>get_i
      RECEIVING
        p_result = data_type.
  ELSEIF vartype = 'C'.
    TRY.
        CALL METHOD cl_abap_elemdescr=>get_c
          EXPORTING
            p_length = varlen
          RECEIVING
            p_result = data_type.
      CATCH cx_parameter_invalid_range .
    ENDTRY.

  ENDIF.

  CREATE DATA wf_ref TYPE HANDLE data_type.
  CHECK wf_ref IS BOUND.
  write_val varname.

Read only

0 Likes
5,112

I cant tell you how thankful i'am., I was searching for such a solution for 2 days. Thanks a lot

Read only

0 Likes
5,112

Hi,

I tried this strategy to create field-symbols that could be referenced in a WHERE (whr) string. Though my code didn't abort, the field-symbol i created with <&1> didn't seem to exist outside the define methods. Do you know why this would be? My code is like this

 

"create a field-symbol of the supplied name, assign lr_param->* to it so it can be modified outside the macro

DEFINE make_var.

FIELD-SYMBOLS: <&1> TYPE PIQ_SELOPT_T.

ASSIGN lr_param->* TO <&1>.

END-OF-DEFINITION.

DATA: lv_param TYPE selopt.

CREATE DATA lr_param.

LOOP AT lt_varnames INTO var.

REFRESH lr_param->*.

LOOP AT lt_screen INTO <val> WHERE selname = var.

MOVE-CORRESPONDING <val> TO lv_param.

APPEND lv_param TO lr_param->*.

ENDLOOP.

make_var var. "<var> gets assigned to <opts>

ASSIGN lr_param->* TO <so_mtart>.

CONCATENATE '<' var '>' INTO var. "new name

REPLACE <val>-selname IN whr WITH var.

ENDLOOP.

Thanks,

Lindsay

Read only

0 Likes
5,112

Write your code without a macro. It will work ( avoid make_var and declare your field symbol globally )

Read only

0 Likes
5,112

Hi,


Can you tell us what was the business requirement in this case .


Thank you in advance.

Eitan.

Read only

0 Likes
5,112

HI Kesavadas

Thanks for the quick reply. My question was based on your example above where you use macros (below) to allow for the creation of dynamically named variables...If I simple remove the macro, won't I lose the ability to name my field-symbols dynamically?

DEFINE mcr.

  field-symbols:<&1> type any.

END-OF-DEFINITION.

DEFINE write_val.

  assign wf_ref->* to <&1>.

  check <&1> is assigned.

  <&1> = '1234'.

  write: <&1>.

END-OF-DEFINITION.

Thanks,

Lindsay

Read only

Former Member
0 Likes
5,112

DATA r_elemdescr TYPE REF TO cl_abap_elemdescr.

r_elemdescr ?= cl_abap_elemdescr=>describe_by_name( 'DATA_ELEMENT_HERE' ).

DATA r_field TYPE REF TO data.

FIELD-SYMBOLS ‹field› TYPE ANY.

CREATE DATA r_field TYPE HANDLE r_elemdescr.

ASSIGN r_field->* TO ‹field›.

‹field› = .....

"now ‹field› is your dynamic field value