Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Change a part of string variable
From 
 
Nick Cox <[email protected]> 
To 
 
"[email protected]" <[email protected]> 
Subject 
 
Re: st: Change a part of string variable 
Date 
 
Wed, 25 Jul 2012 04:52:14 +0100 
Try focusing on the fact that you want to change the second "word" of  
the string.
On 24 Jul 2012, at 22:55, Lucie Vlach <[email protected] 
> wrote:
Hello all!
I need to change a string var to add 0 where there is only 1 digit  
in the date:
visitdate_new (str12, %12s)
Mar 3 2012
Mar 3 2012
Mar 23 2012
Mar 23 2012
Mar 24 2012
Jan 6 2011
Apr 9 2012
etc…
I need to get this to look like this:
Mar 03 2012
Mar 03 2012
Mar 23 2012
Mar 23 2012
Mar 24 2012
Jan 06 2011
Apr 09 2012
etc…
I am trying the following, but it’s not working well in terms of whe 
re the 0 lands and it’s grabbing 23 or 13 where I just look for soli 
tary 3 (like Mar 3 2012)
replace visitdate_new = substr(visitdate_new, 1, 4) + "01" + substr 
(visitdate_new, 6, .) if substr(visitdate_new, 5, 1) == "1"
replace visitdate_new = substr(visitdate_new, 1, 4) + "02" + substr 
(visitdate_new, 6, .) if substr(visitdate_new, 5, 1) == "2"
replace visitdate_new = substr(visitdate_new, 1, 4) + "03" + substr 
(visitdate_new, 6, .) if substr(visitdate_new, 5, 1) == "3"
<snip>
replace visitdate_new = substr(visitdate_new, 1, 4) + "09" + substr 
(visitdate_new, 6, .) if substr(visitdate_new, 5, 1) == "9"
I think I am close, but there is something wrong with my code. Maybe  
there is a better solution?
Thank you very much!
Lucie
This message and any attached documents are only for the use of the  
intended recipient(s), are confidential and may contain privileged  
information. Any unauthorized review, use, retransmission, or other  
disclosure is strictly prohibited. If you have received this message  
in error, please notify the sender immediately, and then delete the  
original message. Thank you.
*
*   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/
*
*   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/