Statalist The Stata Listserver


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

st: RE: converting dates formatted with dots


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: converting dates formatted with dots
Date   Mon, 29 Jan 2007 11:36:42 -0000

As far as Stata are concerned your -sdate- 
variable is an arbitrary string. 

To get a date from such data, you must 
spell out the interpretation rule, e.g. 

. di date("28.2.2003", "dmy")
15764

So try 

gen date = date(sdate, "dmy") 

That said, another way to get what 
you ask is 

split sdate, p(.) destring 
rename sdate1 day 
rename sdate2 month
rename sdate3 year 

Nick 
[email protected] 

Ana Gabriela Guerrero Serdan
  
> I want to convert a variable that includes dates as
> follows: day.month.year (e.g. 1.4.2005) into three
> different variables for day, month and year (day=1,
> month=4,year=2005)
>  
> I have tried the date commands but since I have dots
> stata does not recognize them. 
>  
> . list sdate incident sex agecat
> +-------------------------------------------+
> sdate   incident       sex    agecat 
> -------------------------------------------
> 1.   20.3.2003       x007   unknown   Unknown 
> 2.   20.3.2003       x038      male   Unknown 
> 3.   20.3.2003       x038      male   Unknown 
> 4.   20.3.2003       x038      male     child 
> 5.   20.3.2003       x038    female     child 
> -------------------------------------------
> 6.   20.3.2003       x038   unknown   Unknown 
> 7.   20.3.2003       x038   unknown   Unknown 
> 8.   20.3.2003       x038   unknown   Unknown 
> 9.   20.3.2003       x038   unknown   Unknown 
> 10.   20.3.2003       x038   unknown   Unknown 
> -------------------------------------------
> 11.   20.3.2003       x038   unknown   Unknown 
> 12.   20.3.2003       x038   unknown   Unknown 
> 13.   20.3.2003       x038   unknown   Unknown 
> 14.   20.3.2003       x038   unknown   Unknown 
> 15.   20.3.2003       x038   unknown   Unknown 
> -------------------------------------------
> 16.   20.3.2003       x038   unknown   Unknown 
> 17.   20.3.2003       x038   unknown   Unknown 
> etc... 
> 
> 
> Break--
> r(1);
> . generate date=year(sdate)
> type mismatch
> r(109);

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