Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: replace part of string variable


From   Keith Dear <keithdear4@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: replace part of string variable
Date   Thu, 9 Jun 2011 12:13:17 +1000

Does this do it?

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

(why 30 not 15, btw?)


On 9 June 2011 04:34, raoul reulen <r.c.reulen@gmail.com> wrote:
> Hello
>
> 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)
> "01**1978"
> "04**1987"
> "**011977"
> "03**1978"
>
> 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.
>
> *---example---*
> .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!
>
> Thanks
>
> Raoul
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>



-- 
Dr Keith Dear
National Centre for Epidemiology and Population Health
ANU College of Medicine, Biology and Environment
Australian National University
Canberra, ACT 0200 Australia
Phone +61 (02) 6273 2208

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index