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]

st: Change a part of string variable


From   Lucie Vlach <Lucie.Vlach@albertahealthservices.ca>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   st: Change a part of string variable
Date   Tue, 24 Jul 2012 15:55:08 -0600

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 where the 0 lands and it’s grabbing 23 or 13 where I just look for solitary 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/


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