Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: replace two characters within an eight-character string


From   "Tim Wade" <[email protected]>
To   [email protected]
Subject   Re: st: replace two characters within an eight-character string
Date   Sun, 4 Nov 2007 17:53:32 -0500

Michael, I think the substr function can help, assuming all your
strings are the same number of characters:

. input str10 datestr

        datestr
  1. 01991994
  2. 99011994
  3. 99991994
  4. 03151994
  5. end

. replace datestr="01"+substr(datestr, 3, 8) if substr(datestr, 1, 2)=="99"
(2 real changes made)

. replace datestr=substr(datestr, 1, 2)+"01"+substr(datestr, 5, 4) if
substr(datestr, 3, 2)=="99"
(2 real changes made)

. list

     +----------+
     |  datestr |
     |----------|
  1. | 01011994 |
  2. | 01011994 |
  3. | 01011994 |
  4. | 03151994 |
     +----------+

Hope this helps, Tim



On 11/3/07, Michael McCulloch <[email protected]> wrote:
> Dear Statalist members,
> How can I replace characters at specific locations within an
> eight-character string?
>
> I'm using -todate- with mostly good success to convert an
> eight-character string to Stata date, except when a month or day
> within that string is unknown. That would be coded "99", as in:
>         "01991994", for "some day in January, 1994"
>
> I'd like to edit that string to "01011994". Can this be done?
>
> Tangentially, my code is:
>         todate datevar, gen(newdatevar) p(mmddyyyy) f(%dd_m_cy)
>         format datechemo %d
>
> Thank you,
> Michael
>
>
>
> --
>
> Michael McCulloch
> Pine Street Foundation
> 124 Pine St., San Anselmo, CA 94960-2674
> Tel:    (415) 407-1357
> Fax:    (415) 485-1065
> [email protected]
>
> *
> *   For searches and help try:

> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index