Statalist


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

RE: st: parsing a variable positionally


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: parsing a variable positionally
Date   Tue, 3 Jun 2008 11:05:12 +0100

Oh, and there's an FAQ that is still instructive: 

FAQ     .  Handling date information when numbers are in a continuous
sequence
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D.
Reichel
        8/07    How do I convert date variables into Stata elapsed
                dates when the numbers run together, like 4151999?
                http://www.stata.com/support/faqs/data/dateseq.html

Nick Cox

Phil is right that -substr()- is the main tool for parsing (string)
variables positionally. -split- is only for situations in which you have
parsing characters. 

I once tried to think up a command that that would be a wrapper for
repeated -substr()- and had just as much flexibility and simpler syntax,
but decided that the quest was futile. 

But given integer variables say -monthday- and -year-, you have other
methods open to you. 

I guess that dates like 15 October and 31 December are held as 1015 and
1231. (That being so, you do not want to parse after the first
character.)  

1. Concatenate -monthday- and -year- (using -egen, concat()-, for
example) and apply -date()-. 

Or 

2. gen dailydate = mdy(floor(monthday/100), mod(monthday, 100), year) 

For the last, consider 1231. The monthly part is 12, and the daily part
is 31. The monthly part is thus floor(monthday/100) or int(monthday/100)
and the daily part is the remainder mod(monthday, 100). 

Philip Ryan

the  substr()  string function might be useful.

Rufus 

>I'm trying to convert a variable into a date.  Ordinarily, this would
>be simple using Stata's date() functions, but one of my variables
>contains both month and date (i.e. 403 is April 3rd), while another
>contains year.  I'm trying to split the month+date variable into two
>variables by parsing after the first character.  I tried converting
>the variable to a string and using the "split" command, but it does
>not allow me to parse positionally; it merely allows me to specify a
>parsing character.  Is there a way to parse by position?


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index