Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: Coverting variable to date and time


From   Neophytos Stylianou <[email protected]>
To   "[email protected]" <[email protected]>
Subject   RE: st: Coverting variable to date and time
Date   Fri, 24 May 2013 14:39:49 +0000

Nick,

Thanks anyway.

Sometimes documentation is not that clear and it is easier to get some personal advice.

Anyway thanks for all your help.

Neo
________________________________________
From: [email protected] [[email protected]] on behalf of Nick Cox [[email protected]]
Sent: 24 May 2013 13:55
To: [email protected]
Subject: Re: st: Coverting variable to date and time

documentation.  Please assume goodwill on my part, but I think this is
a case of "three strikes, and you're out".


Nick
[email protected]


On 24 May 2013 13:03, Neophytos Stylianou
<[email protected]> wrote:
> Nick
>
> I used the following to change a string time variable to numeric.
> gen time5=clock(time12, "hms")
> format time5 %tc_hh:mm:ss
>
> it does display time as hh:mm:ss
> the variable type is float. Is that ok?
> Is there a way of getting the time in 24hr format?
> For example string variable "19:05:22" the numeric gives me 7:5:22
> Is it possible to get numeric variable as 19:05:22?
>
> Thanks,
> Neo
>
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
> Sent: 24 May 2013 12:44
> To: [email protected]
> Subject: Re: st: Coverting variable to date and time
>
> Sure. Just keep reading the documentation. For example
>
> . di clock("09:38:00.000", "hms")
> 34680000
>
> . di %tc clock("09:38:00.000", "hms")
> 01jan1960 09:38:00
>
> . di %tc_hh:mm clock("09:38:00.000", "hms")
>   9:38
>
> The day of 1 Jan 1960 can just be ignored.
> Nick
> [email protected]
>
>
> On 24 May 2013 12:34, Neophytos Stylianou <[email protected]> wrote:
>> Thanks,
>>
>> I found out that the data source uses the atomic clock time measurement therefore %tc would be fine.
>>
>> Thanks Nick for the ampm command.
>>
>> If I only have time as string variable is there a way of changing it to numeric?
>>
>> For date you follow this command:
>> gen datevar=date(mydatevar, "YMD")
>> format date %d
>>
>> but for time only?
>> Variable string 08:54:22
>>
>> Neo
>>
>> -----Original Message-----
>> From: [email protected]
>> [mailto:[email protected]] On Behalf Of Nick Cox
>> Sent: 24 May 2013 12:02
>> To: [email protected]
>> Subject: Re: st: Coverting variable to date and time
>>
>> The difference between %tc and %tC is documented. If it's not clear to you, then perhaps Google "leap seconds" or go beyond the help and look at the manual.
>>
>> Your second -replace- won't work even in principle, and neither will work in practice.
>>
>> gen ampm = cond(hh(datetime) < 12, "am", "pm")
>>
>> might be one way of doing what I think you want.
>>
>> Nick
>> [email protected]
>>
>>
>> On 24 May 2013 11:45, Neophytos Stylianou <[email protected]> wrote:
>>> Nick I have some more questions about time and date.
>>>
>>> I have read through the help file but it is not clear to me what is
>>> the difference of %tc and %tC
>>>
>>> I have converted my data to YMDhms and it works
>>>
>>> Just for my practise I have also split the variable and converted the
>>> two splits into date and time variable form strings
>>>
>>> I have entries for the same date but for morning as well as afternoon.
>>>
>>> How will I separate them in AM and PM?
>>>
>>> Is it something like:
>>> Gen time2= AM if time1 >=00:00:00 & time1<=12:00:00 Replace time2=PM
>>> if time1>12:00:00 &time1<00:00:00
>>>
>>> Thnaks
>>> Neo
>>>
>>> -----Original Message-----
>>> From: [email protected]
>>> [mailto:[email protected]] On Behalf Of Nick Cox
>>> Sent: 24 May 2013 11:00
>>> To: [email protected]
>>> Subject: Re: st: Coverting variable to date and time
>>>
>>> That is no problem: it is in many ways the easiest and best way to import dates and/or  times into Stata.
>>>
>>> From your example, the -split- command is sufficient to split your variable into date and time strings: its default is to parse on spaces. See -help split-.  However, that is a red herring here.
>>>
>>> Stata has a rich variety of date and time functions making anything that you want. See -help dates- for a full introduction. What is below shows how to start. The help I have referenced shows how to go further.
>>>
>>> . set obs 1
>>> obs was 0, now 1
>>>
>>> . gen double datetime = clock("2005-02-19 09:38:00.000", "YMD hms")
>>>
>>> . format datetime %tc
>>>
>>> . l
>>>
>>>      +--------------------+
>>>      |           datetime |
>>>      |--------------------|
>>>   1. | 19feb2005 09:38:00 |
>>>      +--------------------+
>>>
>>> Nick
>>> [email protected]
>>>
>>>
>>> On 24 May 2013 10:35, Neophytos Stylianou <[email protected]> wrote:
>>>> Hello,
>>>>
>>>> I am using Stata 12.1 and I have a variable containing date and time.
>>>>
>>>> The problem is that it is a string variable and it contains both date and time within it.
>>>>
>>>> I want to split them into date variable and time variable but also have the opportunity to have a date time combined variable.
>>>>
>>>> Example of what the variable contains :
>>>>
>>>> 2005-02-19 09:38:00.000
>>>> 2005-02-19 09:46:00.000
>>>>
>>>> I want a variable of date i.e 19/02/2005 and time 09:38:00
>>>>
>>>> I would do it manually if it was a small number of records but there
>>>> are more than 1.3million rows
>>>>
>>>> I know that I can combine the date and time variables which will be
>>>> created using gen double datetime = date*24*60*60*1000 + time format
>>>> datetime %tcNN/DD/CCYY_HH:MM:SS
>>>>
>>> *
>>> *   For searches and help try:
>>> *   http://www.stata.com/help.cgi?search
>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>> *   http://www.ats.ucla.edu/stat/stata/
>>>
>>> *
>>> *   For searches and help try:
>>> *   http://www.stata.com/help.cgi?search
>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>> *   http://www.ats.ucla.edu/stat/stata/
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
>>
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index