Published on :
ETL CREATION_INTERNE

Using the TRANWRD() Function

This code is also available in: Deutsch Español Français
Awaiting validation
This simple SAS© script illustrates the use of the TRANWRD() function for character string manipulation. It creates a new SAS© table named `phrase` from internal data provided via `DATALINES`. In this table, a new variable `better_phrase` is generated by replacing all occurrences of the word 'cat' with 'dog' in the `original_phrase` variable. The TRANWRD() function is an efficient tool for multiple text replacements within a string.
Data Analysis

Type : CREATION_INTERNE


Data is created directly within the script via the DATALINES instruction. A single observation is defined with the variables ID and original_phrase.

1 Code Block
DATA STEP Data
Explanation :
This DATA step block creates the `phrase` table. It reads `ID` (numeric) and `original_phrase` (character) data from `DATALINES`. The line `better_phrase = TRANWRD(original_phrase, 'cat', 'dog');` uses the TRANWRD() function to search for and replace all occurrences of 'cat' with 'dog' in `original_phrase` and stores the result in the new variable `better_phrase`.
Copied!
1DATA phrase;
2 INPUT ID original_phrase $ 2-50;
3 better_phrase = TRANWRD(original_phrase, 'cat', 'dog');
4 DATALINES;
51 A cat is the best pet ever, I love my cat.
6;
7RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.