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: imputing dates into a string date

From   Nick Cox <>
To   "''" <>
Subject   st: RE: imputing dates into a string date
Date   Thu, 7 Jun 2012 15:31:52 +0100

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:

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