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 Kodyaz SAP and ABAP Programming Tutorials
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Windows, Windows Phone, SAP and ABAP, like SAP UI5, Screen Personas, etc.




CRM Companies List
Web Based CRM Software


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.

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.

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

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





SAP Tutorials

SAP Tutorial

SAP Forums

SAP Tools

SAP Transaction Codes Table






Related Articles and Tutorials(http://www.kodyaz.com/sap-abap/switch-variable-value-with-translate-using-mask-instead-of-if-or-case-in-abap.aspx)
Activate Fixed Point Arithmetic Flag for Open SQL Statement
SAPSQL_DATA_LOSS ABAP Programming Error
Find Number of Rows Selected in ABAP SELECT Statement
Check Existence using ABAP SELECT
ABAP Select Statement using Space instead of Null or Initial
How to Find Internal Table Row Count in ABAP
Select-Statement can be Transformed
Update Multiple Variables in ABAP Select Single Statement
Update Database Table Data using ABAP SE16N_INTERFACE
Enable Remote Debugging in ABAP using RSRDEBUG
ABAP LOOP Statement with At New and At End
Find Length of a String in ABAP using STRLEN function
ABAP Shared Memory Objects Tutorial with Sample ABAP Code
ABAP Tutorial for SET and GET PARAMETER ID Command
Save Table Contents into Text File and Download File
Dynamic Programming using Field Symbols in ABAP Reports
Set Default Single Values List for SAP Selection Screen Parameter
ABAP Tutorial - ABAP String Split Example Code
How to Delete from Internal Table using ABAP
ABAP Tutorial - SAP Split and ABAP Split String Function
SAP Email Send using ABAP efg_gen_send_email Function Call
Built-in ABAP Type List or Predefined ABAP Types for SAP Systems
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement


Copyright © 2004 - 2017 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems