TRANWRD Function

Replaces all occurrences of a substring in a character string.

Category: Character
Restriction: I18N Level 2 functions are designed for use with SBCS, DBCS, and MBCS (UTF8).

Syntax

Required Arguments

source

specifies a character constant, variable, or expression that you want to translate.

target

specifies a character constant, variable, or expression that is searched for in source.

Requirement The length for target must be greater than zero.

replacement

specifies a character constant, variable, or expression that replaces target. When the replacement string has a length of zero, TRANWRD uses a single blank instead.

Details

Length of Returned Variable

In a DATA step, if the TRANWRD function returns a value to a variable that has not previously been assigned a length, then that variable is given a length of 200 bytes. You can use the LENGTH statement, before calling TRANWRD, to change the length of the value.

The Basics

The TRANWRD function copies the value in source to the result string while searching for all non-overlapping substrings in source that are equal to the value in target. Each of these substrings is omitted from the result and the value of replacement is copied in its place. The TRANWRD function does not remove trailing blanks in the target string or the replacement string.

Comparisons

The TRANWRD function differs from the TRANSTRN function because TRANSTRN allows the replacement string to have a length of zero. TRANWRD uses a single blank instead when the replacement string has a length of zero.
The TRANSLATE function converts every occurrence of a user-supplied character to another character. TRANSLATE can scan for more than one character in a single call. In doing this scan, however, TRANSLATE searches for every occurrence of any of the individual characters within a string. That is, if any letter (or character) in the target string is found in the source string, it is replaced with the corresponding letter (or character) in the replacement string.
The TRANWRD function differs from TRANSLATE in that TRANWRD scans for substrings and replaces those substrings with a second substring.

Examples

Example 1: Replacing All Occurrences of a Word

These statements and these values produce these results:
   name=tranwrd(name, "Mrs.", "Ms.");
   name=tranwrd(name, "Miss", "Ms.");
   put name;
Value
Result
Mrs.  Joan Smith
Ms.  Joan Smith
Miss Alice Cooper
Ms. Alice Cooper

Example 2: Removing Blanks From the Search String

In this example, the TRANWRD function does not replace the source string because the target string contains blanks.
data list;
   input salelist $;
   length target $10 replacement $3;
   target='FISH';
   replacement='NIP';
   salelist=tranwrd(salelist,target,replacement);
   put salelist;
   datalines;
CATFISH
;
The LENGTH statement pads target with blanks to the length of 10, which causes the TRANWRD function to search for the character string 'FISH ' in SALELIST. Because the search fails, this line is written to the SAS log:
CATFISH
You can use the TRIM function to exclude trailing blanks from a target or replacement variable. Use the TRIM function with target:
salelist=tranwrd(salelist,trim(target),replacement);
put salelist;
Now, this line is written to the SAS log:
CATNIP

Example 3: Zero Length in the Third Argument of the TRANWRD Function

The following example shows the results of the TRANWRD function when the third argument, replacement, has a length of zero. In this case, TRANWRD uses a single blank. In the DATA step, a character constant that consists of two consecutive quotation marks represents a single blank, and not a zero-length string. In this example, the results for string1 and string2 are the same:
data _null_;
   string1='*' || tranwrd('abcxabc', 'abc', trimn('')) || '*';
   put string1=;
   string2='*' || tranwrd('abcxabc', 'abc', '') || '*';
   put string2=;
run;
Output When the Third Argument of TRANWRD Has a Length of Zero
string1=* x *
string2=* x *

Example 4: Removing Repeated Commas

You can use the TRANWRD function to remove repeated commas in text, and replace the repeated commas with a single comma. In the following example, the TRANWRD function is used twice: to replace three commas with one comma, and to replace the ending two commas with a period:
data _null_;
   mytxt='If you exercise your power to vote,,,then your opinion will be heard,,';
   newtext=tranwrd(mytxt, ',,,', ',');
   newtext2=tranwrd(newtext, ',,' , '.');
   put // mytxt= / newtext= / newtext2=;
run;
Output from Removing Repeated Commas
mytxt=If you exercise your power to vote,,,then your opinion will be heard,,
newtext=If you exercise your power to vote,then your opinion will be heard,,
newtext2=If you exercise your power to vote,then your opinion will be heard.

See Also

Functions: