TRANWRD Function


TRANWRD Function

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

Category: Character

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

If the TRANWRD function returns a value to a variable that has not yet been assigned a length, by default the variable is assigned a length of 200.

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 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  replacement ;     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 Function' on page 858




SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3
SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 704

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net