Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: replace part of string variable

From   Nick Cox <>
To   "''" <>
Subject   st: RE: replace part of string variable
Date   Wed, 8 Jun 2011 19:44:28 +0100

There is no need for regex machinery here. This supports my prejudice that -substr()-, -subinstr()-, -length()- and -strpos()- (the best string quartet outside Vienna) are often overlooked because people suppose that the fancier regex stuff may be needed. 

replace dob = "30" + substr(dob, 3, .) if substr(dob, 1, 2) == "**"
replace dob = substr(dob, 1, 2) + "06" + substr(dob, 5, .) if substr(dob, 3, 2) == "**"

By the way, it doesn't seem consistent to replace an unknown day with "30" and an unknown month with "06". More importantly, 30 February will only cause problems downstream. In some similar problems, I used 15th as a guess at day of month. 


raoul reulen

I want to replace a specific character (let's say: **) of a string
variable, but only if it is in a certain position. I have a string
variable for date of birth (dob) and some of the month values and some
of the day values are unknown and have been substituted by an
asterisk, like:

dob (day, month, year)

Now, I want to replace the first two characters with the value "30" if
the first two values are an asterisk, the 3th and 4th with "06" if
they are an asterisk etc.  Now, I can obviously split the variable
into 3 components like below, but I am sure there is an easier way. Is
this a job for "regexm"? I looked into "subinstr" as well, and
although I can replace the asterisks, it seems to be independent of
the position/location the asterisks are in.

.gen str4 dobyear = substr(dob, 5, 4)
.gen str2  dobmth = substr(dob,3,2)
.gen str2 dobday = substr(dob,1,2)

.replace dobmth = "06" if dobmth=="**"
.replace dobday = "30" if dobday=="**"
*-----end example------*

Any help appreciated!

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index