SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for SAP HANA and ABAP, HANA Database, SQLScript, SAP UI5, Screen Personas, Web Dynpro, Workflow

Switch ABAP Variable Value with Translate using Mask

ABAP developers use Translate Using ' XX ' to switch flag variable values among 'X' and space or initial as IF or CASE command alternative in ABAP programs to control code. This ABAP tutorial shows programmers how they can ABAP Translate command with USING ' XX ' mask which enables them to change 'X' value to space or set the variable to its initial value or vice versa to set the space or initial variable value to 'X'

ABAP programmers can benefit the code shared here to set an active value to passive or just the opposite a passive variable to active value marked by 'X'

Before I start explaining how ABAP developers can use Translate command with an appropriate mask to switch the value of the input variable, we can look at the IF statement and CASE statement samples at first.

documentation for ABAP Translate command

Below I copied down a set of sample ABAP code blocks where a variable of XFELD data type is used for the sake of this ABAP tutorial.
Please note that you can use character data types like xfeld, char1 or c for replacing initial value which is space with 'X' or vice versa.
The variable is declared without any initial value assignment so its initial value is space or empty value.

An ABAP programmer can switch the value of a parameter or variable by using ABAP IF clause in general.
Below the ABAP code is readable and does its work properly. The developer changes the value of the variable conditionally according to its value and switches to alternative one.

DATA lv_flag TYPE xfeld.

IF lv_flag = 'X'.
 CLEAR lv_flag.
ELSE.
 lv_flag = 'X'.
ENDIF.
Code

Of course a second method to set parameter value among two options to replace the current value with its alternative is using ABAP CASE command. Here is a similar code where CASE ABAP statement is used.

DATA lv_flag TYPE xfeld.

CASE lv_flag.
 WHEN 'X'.
  CLEAR lv_flag.
 WHEN OTHERS.
  lv_flag = 'X'.
ENDCASE.
Code

On the other hand, following ABAP code statement is the one that I like to use most.
As ABAP programmers can see, I'm using the ABAP Translate command which we generally use to change a string value to upper case or lower case.

DATA lv_flag TYPE xfeld.
TRANSLATE lv_flag USING ' XX '. " value is now X
TRANSLATE lv_flag USING ' XX '. " value set to its initial value, space
TRANSLATE lv_flag USING 'X X'. " value is now X
TRANSLATE lv_flag USING 'X X'. " value set to its initial value, space
Code

In above code, we use ABAP Translate command with USING and provide ' XX ' or 'X X' as the translate mask.

The characters in the translate command mask are provided as character pairs.
This means for example for the ' XX ' mask, by respecting the first pair space characters are replaced with X. And for the second pair, the X characters are replaced with space character.

first check variable is initial or not and then set the opposite value

ABAP programmers can apply the same rule to 'X X' mask as well. But be careful, there are two spaces between the two X characters surrounding them.
This is a convenient and single line command to check the value of the variable at first, then switch to opposite value.
For example, if it is active set the variable value to passive. And if the variable is active, or marked as X then set it to passive value by using space or initial value.

One last note, if your variable has data type N which is character formed of numbers, you can use 0 instead of space and keep the X in the ABAP Translate command mask. Below is an other example with N type variable.

DATA lv_flag TYPE n. " ABAP variable is 0, initial value for N data type

TRANSLATE lv_flag USING '0XX0'. " variable value switched to X
TRANSLATE lv_flag USING '0XX0'. " second switch change the value back to 0
Code


SAP HANA and ABAP

Install SAP Free
CRM Companies List
Web Based CRM Software


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.