Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Formatting of dates


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Formatting of dates
Date   Fri, 9 Sep 2005 15:12:04 +0100

Note that the operation can be telescoped, thus  
making less use of variables that will not 
be needed for later work. A nod also to Eric 
Wruck who points that you probably would prefer 
-year- to be numeric. 

gen year = real(substr(datetime, strpos(datetime," ") - 4, 4)) 


Nick 
[email protected] 

Svend Juul replied to I. Mosca

> we have an entry that looks like the following:
> 1-1-2002 00:00:00
> It is a string variable str19 %19s.
> 
> We'd like to transform it into a numerical/date format, but the
> information we want to keep is only the year.
> How can we do that?
> -----------
> 
> Looking at your date format it seems that the year precedes the first
> blank in the string. I create two observations, identify the 
> position of
> the first blank character with strpos(), and use that in the substr()
> function.
> 
>     clear
>     input str19 datetime
>     "1-1-2002 00:00:00"
>     "10-12-1998 13:32:17"
>     end
> 
>     gen x = strpos(datetime," ")
>     gen year = substr(datetime, x-4, 4)
> 
> In Stata 8 it is not strpos(), but index():
>     gen x = index(datetime," ")
> 

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