TRANSTRN Function

Replaces or removes all occurrences of a substring in a character string.

Category: Character

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.

Details

Length of Returned Variable

In a DATA step, if the TRANSTRN 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 TRANSTRN, to change the length of the value.

The Basics

The TRANSTRN function replaces or removes all occurrences of a given substring within a character string. The TRANSTRN function does not remove trailing blanks in the target string and the replacement string. To remove all occurrences of target, specify replacement as TRIMN("").

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 TRANSTRN function differs from TRANSLATE in that TRANSTRN 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=transtrn(name, "Mrs.", "Ms.");
   name=transtrn(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 TRANSTRN 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=transtrn(salelist,target,replacement);
   put salelist;
   datalines;
CATFISH
;
The LENGTH statement pads target with blanks to the length of 10, which causes the TRANSTRN 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=transtrn(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 TRANSTRN Function

The following example shows the results of the TRANSTRN function when the third argument, replacement, has a length of zero. In the DATA step, a character constant that consists of two quotation marks represents a single blank, and not a zero-length string. In the following example, the results for string1 are different from the results for string2.
data _null_;
   string1='*' || transtrn('abcxabc', 'abc', trimn('')) || '*';
   put string1=;
   string2='*' || transtrn('abcxabc', 'abc',  '') || '*';
  put string2=;
run;
SAS writes the following output to the log:
Output When the Third Argument of TRANSTRN Has a Length of Zero
string1=*x*
string2=* x *

See Also

Functions: