Previous Page | Next Page

Functions and CALL Routines

TRANWRD Function



Replaces all occurrences of a substring in a character string.
Category: Character
Restriction: I18N Level 2

Syntax
Arguments
Details
Length of Returned Variable
The Basics
Comparisons
Examples
Example 1: Replacing All Occurrences of a Word
Example 2: Removing Blanks From the Search String
Example 3: Zero Length in the Third Argument of the TRANWRD Function
Removing Repeated Commas
See Also

Syntax

TRANWRD(source,target,replacement)


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 replaces all occurrences of a given substring within a character string. The TRANWRD function does not remove trailing blanks in the target string and 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;

Values Results
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;

SAS writes the following output to the log:

Output When the Third Argument of TRANWRD Has a Length of Zero

string1=* x *
string2=* x *

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;

SAS writes the following output to the log:

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

Function:

TRANSLATE Function

Previous Page | Next Page | Top of Page