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

Re: st: Formatting of dates

From   "Svend Juul" <SJ@SOCI.AU.DK>
To   <>
Subject   Re: st: Formatting of dates
Date   Fri, 9 Sep 2005 14:01:50 +0200

I. Mosca wrote:

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

    input str19 datetime
    "1-1-2002 00:00:00"
    "10-12-1998 13:32:17"

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

In Stata 8 it is not strpos(), but index():
    gen x = index(datetime," ")

Hope this helps


Svend Juul
Institut for Folkesundhed, Afdeling for Epidemiologi
(Institute of Public Health, Department of Epidemiology)
Vennelyst Boulevard 6
DK-8000  Aarhus C, Denmark
Phone: +45 8942 6090
Home:  +45 8693 7796

*   For searches and help try:

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