![]() Chapter Contents |
![]() Previous |
![]() Next |
| TRANWRD |
| 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) |
| 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 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 |
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 |
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:
|
![]() Chapter Contents |
![]() Previous |
![]() Next |
![]() Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.