substring - Stata: Systematically replace characters in a string variable -
substring - Stata: Systematically replace characters in a string variable -
i have observations list criminal codes string variables, not in format need. using stata 12, want replace substrings in string variable. example, need alter instances of cc
18
, vc
75
, , pc
35
. so:
orginal variable cc547a1 | vc549f| pc5297 new variable 18547a1 | 75549f | 355297
the characters need alter in beginning. original variables not need changed.
i tried figuring out using substring
command, couldn't adapt code correctly.
the substr()
function (not substring()
; not command) not helpful here sibling, subinstr()
. documented in same place: start @ help functions
.
. clear . input str7 myvar myvar 1. cc547a1 2. vc549f 3. pc5297 4. end . replace myvar = subinstr(myvar, "cc", "18", .) (1 real alter made) . replace myvar = subinstr(myvar, "vc", "75", .) (1 real alter made) . replace myvar = subinstr(myvar, "pc", "35", .) (1 real alter made) . list +---------+ | myvar | |---------| 1. | 18547a1 | 2. | 75549f | 3. | 355297 | +---------+
n.b. previous thread how find , replace part of string variable in stata?
substring stata
Comments
Post a Comment