Bookmark and Share

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

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

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

From   Tim Evans <>
To   "''" <>
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


-----Original Message-----
From: [] On Behalf Of Nick Cox
Sent: 07 June 2012 15:32
To: ''
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.)

Then your code could be 

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. 


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:


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

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

*   For searches and help try:

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:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index