Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Importing date data

From   "Nuno Soares" <>
To   <>
Subject   RE: st: Importing date data
Date   Sun, 25 Jun 2006 13:09:07 +0100

Thanks Maarten! I'll try your solution! 

-----Original Message-----
[] On Behalf Of Maarten buis
Sent: 25 June 2006 12:24
Subject: Re: st: Importing date data

--- Nuno Soares <> wrote:
> I have a file that has a variable date using the format YYMMDD, and I 
> haven't been able to import it as a date format to Stata. I've been 
> using the -infix- command. I don't have any problem to import it as 
> number but then I would loose the date information. For instance, I 
> have one observation that has 000112, i.e., 2000/01/12 and Stata
> it as 112...

I would read the dates in as a string, that way you keep the leading zeros,
use the substr function to extract the year, month, and day to seperate
variables, and than use the mdy fundtion to convert that to a Stata date.
Since you represent year by two digits only you have to take care to prevent
a Y2K problem and decide below which value the year occured in this milenium
and above which it occured in the previous milenium. see the example below:

*--------------begin infix using c:\temp\date.dct
desc list gen year = substr(date,1,2) gen month = substr(date,3,2) gen day =
substr(date,5,2) destring year month day, replace

/*dealing with a Y2K problem
  I assume that that all values for year
  more than 90 are from the previous milenium
  and the that all values less than 90 are
  from this milenium*/
replace year = year + 1900 if year >  90 replace year = year + 2000 if year
<= 90

gen time = mdy(month,day,year)
format time %d

*--------------begin date.dct-------------- infix dictionary {
	str date 1-6
*-------------end date.dct-----------------


Maarten L. Buis
Department of Social Research Methodology Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting adress:
Buitenveldertselaan 3 (Metropolitan), room Z214

+31 20 5986715

Try the all-new Yahoo! Mail. "The New Version is radically easier to use" -
The Wall Street Journal
*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2022 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index