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: imputing dates into a string date
From
Tim Evans <[email protected]>
To
"[email protected]" <[email protected]>
Subject
RE: st: imputing dates into a string date
Date
Fri, 7 Jun 2013 12:14:58 +0100
Hi all,
Some time ago I had a problem with imputing dates into a string variable where the date took the form:
XX/01/2012
In the thread below a solution was provided which worked great, however, I now have data takes the form:
/01/2012
To this, I would like to impute a day of "1", but having tried to amend the original code below
g dx_clean = subinstr(dx, "XX", "01", 1)
to
g dx_clean = subinstr(dx, "", "01", 1)
The result is that I return the same value i.e.
XX/01/2012
Does anyone have a suggestion of how I can handle this please?
Best wishes
Tim
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: 07 June 2012 17:01
To: [email protected]
Subject: Re: st: imputing dates into a string date
-subinstr()- is just acting as instructed, replacing the first occurrence of "xx". It's completely indifferent where that occurs if it's not constrained, e.g. by
... if substr(var1, 1, 2) == "xx"
Nick
On 7 Jun 2012, at 16:01, Tim Evans <[email protected]> wrote:
> Perhaps I am missing something here.
>
> When I have the following dates in Stata:
>
> var1
> 01/xx/2001
> xx/01/2001
> 01/01/xxxx
>
> and use the code
>
> g dx_clean = subinstr(var1, "xx", "01", 1)
>
> I get:
>
> var1 dx_clean
> 01/xx/2001 01/01/2001
> xx/01/2001 01/01/2001
> 01/01/xxxx 01/01/01xx
>
> The problem seems that both the month gets imputed and also the first
> part of the missing year.
>
> So I still prefer Nicks original example.
>
> Best wishes
>
> Tim
>
>
> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of Nick Cox
> Sent: 07 June 2012 15:35
> To: '[email protected]'
> Subject: RE: st: imputing dates into a string date
>
> I wouldn't do precisely that. There might be dates with missing months
> too, for all we know. That code would replace "XX/XX" with "01/01".
> Replacing a missing day with 1 is one thing; replacing a missing month
> with January messes up the data. So, ever picky, I recommend instead
>
> g dx_clean = subinstr(dx, "XX", "01", 1)
>
> Nick
> [email protected]
>
> daniel klein
>
> this is (probably) much simpler than you might assume. All you need to
> type is the one line
>
> g dx_clean = subinstr(dx, "XX", "01", .)
>
>
> Substitute -g dx_clean- in the code with -replace dx- if you want to
> change your original variable.
>
> Hi all,
> [...]
> This is working fine, until I come across a date that actually starts
> like this:
>
> xx/01/2010
>
> Then my code to generate a date returns a blank. What I would like to
> do is add in something that allows me to evaluate whether the day is
> xx and if so, insert 01.
>
> *
> * 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/
>
> _DISCLAIMER:
> This email and any attachments hereto contains proprietary
> information, some or all of which may be confidential or legally
> privileged. It is for the exclusive use of the intended recipient(s)
> only. If an addressing or transmission error has misdirected this e-
> mail and you are not the intended recipient(s), please notify the
> author by replying to this e-mail. If you are not the intended
> recipient you must not use, disclose, distribute, copy, print, or rely
> on this e-mail or any attachments, as this may be unlawful.
>
>
> *
> * 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/
_DISCLAIMER:
This email and any files transmitted with it are confidential. If you are not the intended recipient, any reading, printing, storage, disclosure, copying or any other action taken in respect of this e-mail is prohibited and may be unlawful. If you are not the intended recipient, please notify the sender immediately by using the reply function and then permanently delete what you have received.
The information contained in this e-mail may be subject to public disclosure under the Freedom of Information Act 2000. The confidentiality of this e-mail and your reply cannot be guaranteed, unless the information is legally exempt from disclosure.
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/