I am pleased we are solving some of the problems.
Thank you for your confirmation that you are using Stata 9.2.
The Statalist FAQ advises [3.3]:
"The current version of Stata is 10. Please specify if you are using an earlier version; otherwise, the answer to your question is likely to refer to commands or features unavailable to you."
In Stata 9.2 you can set up your time variable as time in seconds. You don't need any special features to do that.
There are also some extra things, including those in -egenmore-, which will work in Stata 9.2. That includes the -egen- function -hms()-. See also
SJ-6-1 dm0018 . . . . . . . . . . . . . . . . . Speaking Stata: Time of day
(help ntimeofday, stimeofday if installed) . . . . . . . . N. J. Cox
Q1/06 SJ 6(1):124--137
converting back and forth between string and numeric
representations for time-of-day calculations
But there are no special formats available for date-time variables before Stata 10. You need to upgrade to Stata 10 to get full support for date-time variables.
Nick
[email protected]
Katia Bobulova
For what concern question 1, i.e. hms command, I want to clarify that
I am using Stata9.2.
Is this the problem? Because I succeed to generate hours, minutes and
seconds but then I cannot transform my time in something like this:
6:30:00 because hms command does not work.
Is there any other command to achieve the same result in Stata 9.2?
Thank you very much for your time and your attention.
Best,
Katia
2008/7/17 [email protected] <[email protected]>:
> As a footnote to this:
>
> Katia's error message on trying to use a function -hms()- indicated to me that she was trying to use an -egen- function with -generate-.
>
> She could have been trying to use a Stata 10 function -hms()- within an earlier version of Stata. (Else why did she get an error message?)
>
> In Stata 10, users should note that the official function -hms()- differs from the user-written -egen- function -hms()-, and not just in so far as you can use the latter only with -egen-.
>
> In Stata 10 you can do this, if you have installed -egenmore- from SSC:
>
> . set obs 1
> obs was 0, now 1
>
> . gen seven = 7
>
> . gen thirty = 30
>
> . gen fifty = 50
>
> . egen hms_egen = hms(seven thirty fifty)
>
> . gen double hms_gen = hms(seven, thirty, fifty)
>
> . l
>
> +----------------------------------------------+
> | seven thirty fifty hms_egen hms_gen |
> |----------------------------------------------|
> 1. | 7 30 50 27050 27050000 |
> +----------------------------------------------+
>
> The result of -egen, hms()- is in seconds. The result of the function -hms()- is in milliseconds.
>
> From other details here I also surmise that Katia has access to documentation for Stata 10, but is trying to use the date-time stuff introduced in Stata 10 within an earlier version. That won't work.
>
> Nick
> [email protected]
>
> Nick Cox
>
> On 1)
>
> "didn't work" can mean about twenty different things. Always precisely say what you mean, e.g. error message, incorrect values, puzzling results, computer burst into flames.
>
> -egen, hms()- takes a varlist and varlists don't include commas. That is explicit in the help.
>
> -egen, hms()- works out the time as seconds after midnight, which is exactly what my -generate- statement also did.
>
> It thus beats me why you regard -egen, hms()- as what you want to do, but not my statement.
>
> You will clearly need to use the date (i.e. day) information as well. That step was not included in my advice.
>
> Nick
> [email protected]
>
> Katia Bobulova
>
> I tried to curry out your instructions, however I have still some
> problems with question 1 and 2:
>
> 1)I have the time in this format: 63000 and i want to construct a
> "timedate" variable, which contains both the data and the time.
>
> You told me that hms works only with egen, so I tired to do this:
>
> g hours=int(time/10000)
> g minutes=int((time-hours*10000)/100)
> g seconds=int(time-hours*10000-minutes*100)
> egen newtime=hms(hours, minutes, seconds)
> format newtime%tc
>
> But it doesn't work and the solution that you suggested me (i.e. gen
> long time_in_sec = 3600 * hours + 60 * minutes + seconds) is not
> appropriate because it changes time in seconds.
>
> 2008/7/14 [email protected] <[email protected]>:
>
>> Katia Bobulova asked three questions. My answers below.
>>
>> I have some problems in managing my dataset. I have a dataset with
>> high frequency data, with observations about time, date, price,
>> quantity and a code for the type of security.
>>
>> Question 1
>> ==========
>> I have the time in this format: 63000 and i want to construct a
>> "timedate" variable, which contains both the data and the time.
>>
>> First af all I tried to modify the format of the time typing:
>>
>> g hours=int(time/10000)
>> g minutes=int((time-hours*10000)/100)
>> g seconds=int(time-hours*10000-minutes*100)
>> g newtime=hms(hours, minutes, seconds)
>> format newtime%tc
>>
>> However, after typing hms() I receive this message:
>> Unknown function hms()
>> r(133);
>>
>> The next step would be typing something like:
>>
>> gen double timedate=date*24*60*60*1000+time
>> format timedate %tcNN/DD/CCYY_HH:MM:SS
>>
>> Answer 1
>> ========
>>
>> -hms()- is an -egen- function written by Kit Baum and is included in the
>> -egenmore- package from SSC. It will _only_ work with -egen-, not with
>> -generate-.
>>
>> But you don't need it. You did almost all the work yourself.
>>
>> Assuming that e.g. 63000 is 06:30:00 then given your variables -hours-,
>> -minutes-, -seconds-,
>>
>> gen long time_in_sec = 3600 * hours + 60 * minutes + seconds
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/