|
I would like to convert a STRING varible to a DATE variable (with the
hh:mm:ss format) using Syntax. I can do this easily using the Variable View window. I click on the Type for the variable, select Date, select hh:mm:ss from the menu, and click OK. There is no PASTE button, though, and I have not been able to find instructions on how to do this Type conversion using Syntax. Here is some sample data 12:19:18 12:11:31 1:12:42 2:26:25 3:54:44 5:23:36 7:38:21 3:57:31 |
|
Hi Kim,
Try this code which uses a NUMBER function and a FORMAT command. data list free/ strtime (a8). begin data. 12:19:18 12:11:31 1:12:42 2:26:25 3:54:44 5:23:36 7:38:21 3:57:31 end data. COMPUTE numtime = NUMBER(strtime,time8) . FORMAT numtime (time8). EXECUTE. Florio At 04:41 PM 9/12/2007, [hidden email] wrote: >I would like to convert a STRING varible to a DATE variable (with the >hh:mm:ss format) using Syntax. I can do this easily using the Variable >View window. I click on the Type for the variable, select Date, select >hh:mm:ss from the menu, and click OK. There is no PASTE button, though, >and I have not been able to find instructions on how to do this Type >conversion using Syntax. Here is some sample data > >12:19:18 >12:11:31 > 1:12:42 > 2:26:25 > 3:54:44 > 5:23:36 > 7:38:21 > 3:57:31 |
|
In reply to this post by kim.barchard
At 04:41 PM 9/12/2007, [hidden email] wrote:
>I would like to convert a STRING varible to a DATE variable (with the >hh:mm:ss format) using Syntax. [In] the Variable View window. I >click on the Type for the variable, select Date, select hh:mm:ss from >the menu, and click OK. There is no PASTE button, though, Right. That's because that transformation is NOT done via background syntax, so there isn't any to paste (see below) >and I have not been able to find instructions on how to do this Type >conversion using Syntax. You can't change the type of a variable using syntax, though I understand that may be coming. What you can do, and is usually done, is create a new variable of the desired type, and convert the value. SPSS 15 draft output (WRR:not saved separately): >Here is some sample data |-----------------------------|---------------------------| |Output Created |12-SEP-2007 22:45:31 | |-----------------------------|---------------------------| CharTime 12:19:18 12:11:31 1:12:42 2:26:25 3:54:44 5:23:36 7:38:21 3:57:31 Number of cases read: 8 Number of cases listed: 8 * ... Since your string is formatted cleanly as a time ... . * ... already, you can simply use the NUMBER function, ... . * ... instead of fancier parsing or string processing. ... . NUMERIC My_Time (TIME8). COMPUTE My_Time = NUMBER(CharTime,TIME8). LIST. List |-----------------------------|---------------------------| |Output Created |12-SEP-2007 22:49:34 | |-----------------------------|---------------------------| CharTime My_Time 12:19:18 12:19:18 12:11:31 12:11:31 1:12:42 1:12:42 2:26:25 2:26:25 3:54:44 3:54:44 5:23:36 5:23:36 7:38:21 7:38:21 3:57:31 3:57:31 Number of cases read: 8 Number of cases listed: 8 =================== APPENDIX: Test data =================== DATA LIST FIXED / CharTime 01-08 (A). BEGIN DATA 12:19:18 12:11:31 1:12:42 2:26:25 3:54:44 5:23:36 7:38:21 3:57:31 END DATA. LIST. |
|
In reply to this post by kim.barchard
While this question has been answered by others, I would like to point out that the easiest way to figure out a task like this is to use the Date/Time wizard available on the Transform menu and paste the syntax.
Besides simplifying a lot of tasks involving dates and times using the built-in d/t formats, the wizard actually creates syntax for some things that go beyond those. And, for those who use programmability, the extendedTransforms module available on SPSS Developer Central (www.spss.com/devcentral) provides a simple pattern-based language that can be used to describe and transform dates. strtodatetime converts a date/time string to an SPSS datetime value, and datetimetostr does the reverse. The pattern language can handle date/time representations that do not fit any of the standard SPSS formats. HTH, Jon Peck -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of [hidden email] Sent: Wednesday, September 12, 2007 3:42 PM To: [hidden email] Subject: [SPSSX-L] Changing String to Date using Syntax I would like to convert a STRING varible to a DATE variable (with the hh:mm:ss format) using Syntax. I can do this easily using the Variable View window. I click on the Type for the variable, select Date, select hh:mm:ss from the menu, and click OK. There is no PASTE button, though, and I have not been able to find instructions on how to do this Type conversion using Syntax. Here is some sample data 12:19:18 12:11:31 1:12:42 2:26:25 3:54:44 5:23:36 7:38:21 3:57:31 |
| Free forum by Nabble | Edit this page |
