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 Abap

0 Likes
1,557

Hello.

I'm trying to validate some programs, but I came across the following error.

data: v1 type string.

FIELD-SYMBOLS: <fs> type any.

v1 = 'sy-datum (6)'.

ASSIGN (v1) to <fs>.

write: / <fs>.

This returns me the current year / month.

v2 = 'sy-datum (6) - 1'.

ASSIGN (v2) to <fs>.

write: / <fs>.

when i try to do the code above, it returns the same value as the first example, instead of year / month -1.

Is there any way to do this assignment?

1 ACCEPTED SOLUTION
Read only

joltdx
Active Contributor
1,457

Hi!

We are talking about the ASSIGN statement here, and specifically with a dynamic area.

You have spaces in your example, but I guess that's a typo. For

v1 = 'sy-datum(6)'.

you will get the year and month from the assign statement.

What you can put in v1, or (name) as the documentation states it, must be the name of a data object which may contain offsets and lengths, structure component selectors, and component selectors for assigning structured data objects, attributes in classes or attributes in objects.

Calculations or other operations can not be specified there, so sy-datum(6) - 1 would not work, even if it had worked. Furthermore, the ASSIGN statement assigns a memory are to a field-symbol. And the result of a calculation is not a memory area unless you first put that result in a variable.

It is not in the documentation, but I assume that the field it takes stops at the first non-character symbol in the string v1. Like it would be expected from a character string with a fixed length. So that's why you still get '202103' and a successful assignment after also the second ASSIGN.

That's basically the answer to why your code behaves the way it does, and no, there is no way to do that calculation assignment that way. 🙂

Now, I'm curious as to why you WANT to do it this way. Why do this dynamically?

And a word of warning regarding sy-datum(6) - 1, even though that was not really a part of your question... For instance for January of this year, that would be 202101 - 1 = 202100, and I doubt that is what you want? I wrote a bit about date handling in the blog post Safe dating in ABAP.

4 REPLIES 4
Read only

joltdx
Active Contributor
1,458

Hi!

We are talking about the ASSIGN statement here, and specifically with a dynamic area.

You have spaces in your example, but I guess that's a typo. For

v1 = 'sy-datum(6)'.

you will get the year and month from the assign statement.

What you can put in v1, or (name) as the documentation states it, must be the name of a data object which may contain offsets and lengths, structure component selectors, and component selectors for assigning structured data objects, attributes in classes or attributes in objects.

Calculations or other operations can not be specified there, so sy-datum(6) - 1 would not work, even if it had worked. Furthermore, the ASSIGN statement assigns a memory are to a field-symbol. And the result of a calculation is not a memory area unless you first put that result in a variable.

It is not in the documentation, but I assume that the field it takes stops at the first non-character symbol in the string v1. Like it would be expected from a character string with a fixed length. So that's why you still get '202103' and a successful assignment after also the second ASSIGN.

That's basically the answer to why your code behaves the way it does, and no, there is no way to do that calculation assignment that way. 🙂

Now, I'm curious as to why you WANT to do it this way. Why do this dynamically?

And a word of warning regarding sy-datum(6) - 1, even though that was not really a part of your question... For instance for January of this year, that would be 202101 - 1 = 202100, and I doubt that is what you want? I wrote a bit about date handling in the blog post Safe dating in ABAP.

Read only

0 Likes
1,457

Hey Jorgen.

Thanks for the answer.

I am developing some classes where I will parameterize the name of the program and the fields to be filled with the desired values (for this reason I would like to fill in the table the sy-datum (6) - 1).

These classes will be used in my monthly closing jobs.

Read only

joltdx
Active Contributor
1,457

Hah, I hope you put together a blog post about this when you're done... 🙂

And I hope you mind all the security aspects, when it comes to dynamic programming and these kind of things. And speaking of those, since you should have control over what is used and allowed, you could create methods for some of the calculations you need, and allow. You could call these dynamically with CALL METHOD (meth).

And, as I said before, doing sy-datum(6) - 1 will not work for january...

Read only

Sandra_Rossi
Active Contributor
0 Likes
1,457

ASSIGN ('something') ... is not meant to have operations in "something". See ABAP documentation:

  • something "must be the name of a data object which may contain offsets and lengths, structure component selectors, and component selectors for assigning structured data objects, attributes in classes or attributes in objects. The content of name does not have to be in uppercase letters."

If you want a fully-dynamic code then you must generate the ABAP code with GENERATE SUBROUTINE-POOL (only supported way)