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
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: imputing dates into a string date
Date
Fri, 7 Jun 2013 12:46:30 +0100
Interesting. Clearly this doesn't work; I spent a few minutes trying
to work out exactly why, and failed abysmally.
replace dx_clean = "1" + dx_clean if substr(dx_clean, 1, 1) == "/"
may be a good start.
However, Steve Samuels and I discussed exactly this problem in
SJ-12-1 dm0062 . . . . . . . . . Stata tip 105: Daily dates with missing days
. . . . . . . . . . . . . . . . . . . . . S. J. Samuels and N. J. Cox
Q1/12 SJ 12(1):159--161 (no commands)
presents strategies for dealing with daily dates where
some observations only provide the month and year
Imputing with the first of the month biases any differences between
two dates, which are likely to be too long or too short, depending on
which comes first. We had better suggestions.
Nick
[email protected]
On 7 June 2013 12:14, Tim Evans <[email protected]> wrote:
> 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/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/