{"id":1042,"date":"2014-09-03T09:01:28","date_gmt":"2014-09-03T08:01:28","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=1042"},"modified":"2014-09-03T09:13:28","modified_gmt":"2014-09-03T08:13:28","slug":"abap-how-to-declare-a-variable-dynamically","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=1042","title":{"rendered":"ABAP &#8211; How to declare a variable dynamically"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"358\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=358\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" data-orig-size=\"44,50\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Picasa&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1365690880&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"SAP\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" class=\"size-full wp-image-358 alignleft\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" alt=\"SAP\" width=\"44\" height=\"50\" \/>In case you need to create a variable dynamically during runtime with different type, length or precision, you can use the following piece of code to achieve the same &#8211; the variable is created dynamically based on given type, length and number of decimals.<!--more--><\/p>\n<pre lang=\"abap\">DATA: lr_data TYPE REF TO data,\r\n      l_numchar(8) TYPE c.\r\n\r\nFIELD-SYMBOLS:\r\n  &lt;fs_data&gt; TYPE ANY.\r\n\r\nCLASS lcl_utils DEFINITION.\r\n  PUBLIC SECTION.\r\n    CLASS-METHODS:\r\n      create_variable\r\n        IMPORTING\r\n          i_type TYPE c\r\n          i_length TYPE i\r\n          i_decimals TYPE i\r\n        RETURNING value(r_data) TYPE REF TO data.\r\nENDCLASS.\r\n\r\nCLASS lcl_utils IMPLEMENTATION.\r\n  METHOD create_variable.\r\n    DATA:\r\n      lr_var_description TYPE REF TO cl_abap_elemdescr.\r\n \r\n    CASE i_type.\r\n      WHEN 'P'.\r\n        TRY.\r\n            CALL METHOD cl_abap_elemdescr=&gt;get_p\r\n              EXPORTING\r\n                p_length = i_length\r\n                p_decimals = i_decimals\r\n              RECEIVING\r\n                p_result = lr_var_description.\r\n          CATCH cx_parameter_invalid_range .\r\n*             ... some error handling\r\n        ENDTRY.\r\n      WHEN 'I'.\r\n        CALL METHOD cl_abap_elemdescr=&gt;get_i\r\n          RECEIVING\r\n            p_result = lr_var_description.\r\n      WHEN 'C'.\r\n        TRY.\r\n            CALL METHOD cl_abap_elemdescr=&gt;get_c\r\n              EXPORTING\r\n                p_length = i_length\r\n              RECEIVING\r\n                p_result = lr_var_description.\r\n          CATCH cx_parameter_invalid_range .\r\n*           ... some error handling\r\n        ENDTRY.\r\n    ENDCASE.\r\n\r\n    CREATE DATA r_data TYPE HANDLE lr_var_description.    \r\n  ENDMETHOD. \"create_variable\r\nENDCLASS. \"lcl_utils IMPLEMENTATION\r\n\r\nSTART-OF-SELECTION.\r\n  lr_data = lcl_utils=&gt;create_variable(\r\n    i_type = 'P'\r\n    i_length = '8'\r\n    i_decimals = '2'\r\n  ).\r\n  CHECK lr_data IS BOUND.\r\n  \r\n  l_numchar = '4.5689'.\r\n  \r\n  ASSIGN lr_data-&gt;* TO &lt;fs_data&gt;.\r\n  &lt;fs_data&gt; = l_numchar.\r\n  WRITE &lt;fs_data&gt;.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In case you need to create a variable dynamically during runtime with different type, length or precision, you can use the following piece of code to achieve the same &#8211; the variable is created dynamically based on given type, length &hellip; <a href=\"https:\/\/oprsteny.cz\/?p=1042\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"ABAP - How to declare a variable dynamically http:\/\/wp.me\/p3nYbe-gO","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[16,9,5,1],"tags":[446,315,316,29,221],"class_list":["post-1042","post","type-post","status-publish","format-standard","hentry","category-abap","category-development","category-tools","category-uncategorized","tag-abap","tag-cl_abap_elemdescr","tag-create-data","tag-dynamic","tag-oo-abap"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-gO","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1042","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1042"}],"version-history":[{"count":5,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1042\/revisions"}],"predecessor-version":[{"id":1047,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1042\/revisions\/1047"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}