Today I found a pitfall. Function <datetime> works as documented but can be misused. I usually work with dates, so familiar with
g mydate = date(strvar, "MDY") // returns number of days
but today was converting strings provided in an SPSS file and for once interested in differences of minutes. A typical SPSS value is
"06/26/2025 06:55:08 PM" so I wrote
g t1 = datetime(Start,"MDYhms") // returns number of milliseconds
format t1 %tc
but found most of the values of t1 were slightly out! After re-checking the documentation a couple of times realised that <datetime> returns values so large they have to be stored as double - as shown in the documentation and recent responses on this list.
g double t1 = datetime(Start, "MDYhms") // type must be specified, and handles the AM/PM
It seems to me this is an arithmetic overflow, or a type violation. Indeed, under <recast> there is a warning that the <force> option "makes recast unsafe" as a double forced into a float "would lead to a slight rounding of values". This looks like a default <force>.
You have been warned!
g mydate = date(strvar, "MDY") // returns number of days
but today was converting strings provided in an SPSS file and for once interested in differences of minutes. A typical SPSS value is
"06/26/2025 06:55:08 PM" so I wrote
g t1 = datetime(Start,"MDYhms") // returns number of milliseconds
format t1 %tc
but found most of the values of t1 were slightly out! After re-checking the documentation a couple of times realised that <datetime> returns values so large they have to be stored as double - as shown in the documentation and recent responses on this list.
g double t1 = datetime(Start, "MDYhms") // type must be specified, and handles the AM/PM
It seems to me this is an arithmetic overflow, or a type violation. Indeed, under <recast> there is a warning that the <force> option "makes recast unsafe" as a double forced into a float "would lead to a slight rounding of values". This looks like a default <force>.
You have been warned!
Comment