Command Reference : String and Date Function Reference
Replace characters in each word in a string list.
Syntax: @wreplace(str1, src_pat, rep_pat[, "all"])
str_list: string
src_pat: string
rep_pat: string
"all": (optional) string literal
Return: string
Replaces instances of str_list in str_list with rep_pat. The patterns may be made up of any number of “?” (indicates any single character) or “*” (indicates any number of characters).
The pattern is case-sensitive and must exactly match the src_pat characters to be replaced.
Only the first instance of src_pat within each word of str_list is replaced unless the optional flag “all” is specified (enclosed in quotes), in which case all instances within each word are replaced.
@wreplace("ABBC AB", "*B*", "*X*")
replaces the first instance of “B” with “X”, returning the string “AXBC AX”.
@wreplace("ABBC AB", "*B*", "*X*", "all")
replaces all instances of “B” with “X”, returning the string “AXXC AX”.
@wreplace("ABC DDBC", "??B?", "??X?")
replaces all instances of “B” which have two leading characters and one following character, returning the string “ABC DDXC”.
If ALPHA1 is an alpha series,
alpha alphaltrim = @wreplace(alpha1, "*ABC*", "*X*", "all")
replaces all instances of “ABC” with “X” in each word of the entries of ALPHA1 for each observation in the workfile sample.
If SVEC1 is an string vector,
svector sveclen = @wreplace(svec1, "In*", "")
returns a string vector containing elements of SVEC1 with the leading “In” in each word removed.
See also @wdrop, @wkeep, @wnotin, and @replace.