|
Dear list members I have two date variables - imported from an excel file with the format under format/cell/custom (d/mm/yyyy h:mm) as follows: Timefrom Timeto 5-Feb-2009 07:30:00.00 5-Feb-2009 17:30:29.00 I have to extract only the number of hours and seconds from each of them. Both the variables are in date format in SPSS. Do I use the substring function? Will that help if the variable is a date format? I tried to use date/time wizard but was not successful. I would be grateful if someone could help me with this? Many thanks thara
|
|
Hi Thara,
In this situation, it helps if the variable is a date variable. Using the XDATE commands is your best bet....here is an example. Compute TimefromHour=XDATE.HOUR(Timefrom). Compute TimefromSecond=XDATE.SECONDS(Timefrom). Execute. Best, Ari On Thu, Sep 24, 2009 at 8:47 PM, Thara Vardhan <[hidden email]> wrote:
|
|
In reply to this post by thara vardhan-2
The Xdate.time function will give you the time portion. Then
you can use the Ctime functions, such as Ctime.hours to get the hours, etc. E.g., compute hours = ctime.hours(xdate.time(timefrom)). This assumes, which seems likely from your description, that the
variable is already a regular SPSS date/time variable. That would not be
a string, so the substring function would not be relevant. HTH, Jon Peck From: SPSSX(r) Discussion
[mailto:[hidden email]] On Behalf Of Thara Vardhan
|
|
In reply to this post by ariel barak
Hi Ariel Thank you so much for helping me with the syntax problem. However when i tried the syntax you sent I get the result as follows: time from hour time from sec 7:00 0:00 where as the actual number of hours is 10 hours between timefrom timeto 07:30:00.00 17:30:29.00 How do i overcome this? many thanks thara
Hi Thara, In this situation, it helps if the variable is a date variable. Using the XDATE commands is your best bet....here is an example. Compute TimefromHour=XDATE.HOUR(Timefrom). Compute TimefromSecond=XDATE.SECONDS(Timefrom). Execute. Best, Ari On Thu, Sep 24, 2009 at 8:47 PM, Thara Vardhan <vard1tha@...> wrote: Dear list members I have two date variables - imported from an excel file with the format under format/cell/custom (d/mm/yyyy h:mm) as follows: Timefrom Timeto 5-Feb-2009 07:30:00.00 5-Feb-2009 17:30:29.00 I have to extract only the number of hours and seconds from each of them. Both the variables are in date format in SPSS. Do I use the substring function? Will that help if the variable is a date format? I tried to use date/time wizard but was not successful. I would be grateful if someone could help me with this? Many thanks thara
|
|
Hi Thara,
Initially it sounded like you wanted to extract the hours and seconds from two date variable, which the code I sent did correctly (hours=7 and seconds =0 7:30:00.00). Now, it sounds like you're trying to calculate the difference between these two values. This is why it's usually more helpful when posters give the list some context regarding what they're trying to do, and why. I think you may want to explore the DATEDIFF command. COMPUTE DifferenceinSeconds=DATEDIFF(timeto, timefrom, "seconds"). EXECUTE. Keep in mind that DATEDIFF "calculates the difference between two date values and returns an integer (with any fraction component truncated) in the specified date/time units" This code above will give you the number of seconds between the two dates, which you can also divide by 3600 to get the difference in hours if you don't want to truncate. Hope this helps, Ari On Thu, Sep 24, 2009 at 9:22 PM, Thara Vardhan <[hidden email]> wrote:
|
| Free forum by Nabble | Edit this page |
