Statalist The Stata Listserver


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

Re: st: separating string of fixed length into sections


From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: separating string of fixed length into sections
Date   Sat, 30 Dec 2006 18:26:13 +0000 (GMT)

--- Michael McCulloch <[email protected]> wrote:
> I have a date string which I'd like to convert to Stata date.
> . list datedx in 1/5
> 
>       +----------+
>       |   datedx |
>       |----------|
>    1. | 19900417 |
>    2. | 19880107 |
>    3. | 19930407 |
>    4. | 19880819 |
>    5. | 19880927 |
>       +----------+
> 
> Before I can use mdy() and date(), I need to split the string into
> sections:
>          the first four characters for year,
>          then the next two for month,
>          then the last two for day.
> 
> How is that done? 

Michael:

You can achieve what you want by using the dates as numbers (i.e. use
-destring- first) in combination with the floor function. See the
example below:

hope this helps,
Maarten

*---------- begin example ---------------
clear
input str8 datedx 
19900417 
19880107 
19930407 
19880819 
19880927 
end

destring datedx, replace

gen int year = floor(datedx/10000)
gen int month = floor((datedx-year*10000)/100)
gen int day = datedx - year*10000 - month*100
list datedx year month day
*---------- end example ----------------


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

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

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

Send instant messages to your online friends http://uk.messenger.yahoo.com 
*
*   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