Statalist The Stata Listserver


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

Re: st: RE: Date: Thu, 7 Dec 2006 14:48:31 +1100


From   Kyle Hood <[email protected]>
To   [email protected]
Subject   Re: st: RE: Date: Thu, 7 Dec 2006 14:48:31 +1100
Date   Thu, 7 Dec 2006 11:26:48 -0500 (EST)

There is a function that allows you to pick out portions of a string variable. Because your variable appears to be in a fixed format, you might find this function useful. This function is substr():

substr(s,n1,n2) returns the substring of s starting at n1 for a length of n2. If n1<0, the starting position is interpreted as distance from the end of the string. If n2 is missing (.), the remaining portion of the string is returned.

Thus, you can pick out the minutes digits, save this in a new variable, and then destring this variable.

Kyle

On Thu, 7 Dec 2006, Nick Cox wrote:


There is no way that -destring- can do what you want here,
and I speak as its original author still proud of my offspring,
despite all that StataCorp programmers have done to it since
it left home.

-destring- is basically for numbers trapped inside string bodies,
such as the string "42" which manifestly would convince all
concerned if remodelled as numeric 42. But even shorn of all spaces and
punctuation, there is no sense in which

2004/01/01 04:05:00

would be much use to you reborn as

20040101040500

In othr words, -destring- has an -ignore()- option which
could be used to get numbers out of your strings, but it is not
the answer here. You need code that is smart about seconds, minutes,
hours, etc., etc.

What you want is reasonable, but what is available to do it
consists only of user-written programs. Still, you might
-search- for -ntimeofday- and -stimeofday- and try them out.

Once you have a date-time expressed in minutes, produce
a version that is divided by 5 for -tsset-. You'll need to
juggle three versions of the same information, your string
for meaning, a date-time number, and something -tsset- will
treat as you want.

Nick
[email protected]

Rodrigo Martell

I've loaded some data into Stata. The date variable is a
string and looks like this (yyy/mm/dd hh:mm:ss):
2004/01/01 04:05:00

2004/01/01 04:10:00

2004/01/01 04:15:00

I've been trying to convert the string to Stata dates (and
time) to no avail. I want to eventually -tsset- the
(5-minute) time variable. -destring- won't do because it
doesn't like the / and : (or the spaces I assume) in the date.

Does anyone have any hints? I'd appreciate it.
*
*   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/

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