Chapter Contents

Previous

Next
TRANWRD

TRANWRD



Replaces or removes all occurrences of a word in a character string

Category: Character


Syntax
Arguments
Details
Comparisons
Examples
Example 1: Replacing All Ocurrences of a Word
Example 2: Removing Blanks From the Search String
See Also

Syntax

TRANWRD(source,target,replacement)

Arguments

source
specifies the source string that you want to translate.

target
specifies the string searched for in source.

replacement
specifies the string that replaces target.


Details

The TRANWRD function replaces or removes all occurrences of a given word (or a pattern of characters) within a character string. The TRANWRD function does not remove trailing blanks in the target string and the replacement string.

The value that the TRANWRD function returns has a default length of 200. You can use the LENGTH statement, before calling TRANWRD, to change the length of the value.


Comparisons

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, 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 it scans for words (or patterns of characters) and replaces those words with a second word (or pattern of characters).


Examples

Example 1: Replacing All Ocurrences 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 left-aligns TARGET and pads it with blanks to the length of 10. This 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

See Also

Function:
TRANSLATE


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.