Statalist The Stata Listserver


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

Re: st: Importing date data


From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: Importing date data
Date   Sun, 25 Jun 2006 12:23:56 +0100 (BST)

--- Nuno Soares <[email protected]> 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
reads
> it as 112...

Nuno:
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 example.do--------------
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
list
*--------------end example.do--------------

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

HTH,
Maarten


-----------------------------------------
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

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


		
___________________________________________________________ 
Try the all-new Yahoo! Mail. "The New Version is radically easier to use" � The Wall Street Journal 
http://uk.docs.yahoo.com/nowyoucan.html
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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