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]

st: RE: imputing dates into a string date


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
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. 

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/


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