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 at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: imputing dates into a string date


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: imputing dates into a string date
Date   Thu, 7 Jun 2012 17:01:02 +0100

-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 <Tim.Evans@wmciu.nhs.uk> 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: owner-statalist@hsphsun2.harvard.edu [mailto:owner- statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: 07 June 2012 15:35
To: 'statalist@hsphsun2.harvard.edu'
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
n.j.cox@durham.ac.uk

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/


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