Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: RE: RE: imputing dates into a string date

 From Tim Evans To "'statalist@hsphsun2.harvard.edu'" Subject st: RE: RE: imputing dates into a string date Date Thu, 7 Jun 2012 15:45:36 +0100

```Nick, Daniel, Steve,

Many thanks for your invaluable comments and tips. Nicks version won out at the finish as it cleverly picked out the fact (without me posting!!) that I might have missing month to which I wouldn't want to impute a value for. It also converted into a numeric date as well.

Thanks once again.

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:32
To: 'statalist@hsphsun2.harvard.edu'
Subject: st: RE: imputing dates into a string date

Steve Samuels and I wrote a Tip on imputing day of month. (It arose out of a Statalist thread.)

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

Let's suppose that you want to impute "15" for "XX". (The import of the Tip just cited is that there are methods that are often better.)

gen xxflag = substr(date, 1, 2) == "XX"
gen double dx2 = cond(xxflag, date(subinstr(dx, "XX", "15",1), "DMY"), date(dx, "DMY"))

The variable -xxflag- is not absolutely needed for the bare -generate-. It is added to document where you imputed.

Nick
n.j.cox@durham.ac.uk

Tim Evans

I have dates in the form DMY in a string variable

i.e. 22/01/2010

I am generating numerical dates using the following code:

generate double dx2 = date(dx, "DMY")
format %tdDD/NN/CCYY dx2

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.

Am I to assume I need something like regexm

if regexm(dx2, "^XX") == 1
replace"XX" = 01

else
generate double dx2 = date(dx, "DMY")
format %tdDD/NN/CCYY dx2

*
*   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/
```