New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

writing dates to sav #25

Closed
afleishman opened this Issue Feb 26, 2015 · 12 comments

Comments

Projects
None yet
5 participants
@afleishman
Copy link

afleishman commented Feb 26, 2015

I am unable to write dates using the haven package. I've tried formatting the dates as dates, POSIX, and character types. Any ideas?

library("haven")

id <- c(1, 2, 3)
date <- as.Date(c("2014-09-23", "2014-09-24", "2014-09-25"))
date.pos <- as.POSIXct(date)
date.char <- as.character(date)
write_sav(data.frame(id, date, date.pos, date.char), "test.sav")

data view
variable view

@hadley

This comment has been minimized.

Copy link
Member

hadley commented Feb 26, 2015

Haven currently doesn't have any support for date/times on either input or output :(

@afleishman

This comment has been minimized.

Copy link

afleishman commented Feb 26, 2015

I sort of guessed this was the case. Converting dates to different pieces of software is quite unnerving!

Thanks for the quick response.

@afleishman afleishman closed this Feb 26, 2015

@hadley hadley reopened this Feb 26, 2015

@hadley

This comment has been minimized.

Copy link
Member

hadley commented Feb 26, 2015

No need to close, I'll work on eventually and this will remind me that I need to do it.

@afleishman

This comment has been minimized.

Copy link

afleishman commented Feb 26, 2015

Oh, great. Thanks so much!

@evanmiller

This comment has been minimized.

Copy link
Contributor

evanmiller commented Feb 26, 2015

@hadley to convert POSIX timestamps I think all you need to do is add the # of seconds between 10/14/1582 and 1/1/1970, then supply "DATE" or "DATETIME" as the format.

@hadley

This comment has been minimized.

Copy link
Member

hadley commented Feb 26, 2015

@evanmiller thanks! Also need to re-look at our email thread on reading dates

@afleishman

This comment has been minimized.

Copy link

afleishman commented Mar 3, 2015

When I export Date vector from R to SPSS it gives me the number of days between the date and January 1, 1970. I can write very simple syntax in SPSS to correct this. Figured this tidbit may help on your end!

If you look at the screenshot above, you will see the first case in the "date" variable is 16336. January 1, 1970 + 16336 days = September 23, 2014!

@larmarange

This comment has been minimized.

Copy link
Contributor

larmarange commented Jul 21, 2015

The issue is similar when exporting to Stata.

It seems that when haven is exporting to SPSS or Stata, no format are exported. Therefore, the software (SPSS or Stata) don't know that this numerical variable should be considered as a date.

@huftis

This comment has been minimized.

Copy link
Contributor

huftis commented Jan 21, 2016

Note that SPSS does not support time zones or daylight saving time. R does, so the following results in the correct answer (23 hours instead of 24 hours) (in my default time zone):

start = as.POSIXct("2015-03-28 07:00")
end = as.POSIXct("2015-03-29 07:00")
start
[1] "2015-03-28 07:00:00 CET"
end
[1] "2015-03-29 07:00:00 CEST"
end-start
Time difference of 23 hours

Because the times may be used for calculations after exporting to SPSS, I think it’s better to save all date-times as UTC date-times. Here’s a function that can be used (for POSIXct objects):

dt2spss = function(x)
  as.numeric(x) - as.numeric(as.POSIXct("1582-10-14", tz="UTC"))
# same as `as.numeric(x) + 12219379200`, which is faster

This converts the date-time into the number of seconds sincce 1582-10-14, i.e. the internal format SPSS uses for all date-times (and dates). Any calculations done in SPSS would then work correctly. Example:

> (dt2spss(end) - dt2spss(start))/60/60
[1] 23

For dates, the following should do the trick:

d2spss = function(x)
  (as.numeric(x) - as.numeric(as.Date("1582-10-14")))*24*60*60
# same as `86400 * (as.numeric(x) + 141428)`, which is faster

Having an option for saving the date-times in ‘local time’ would also be nice, for applications where one only wants to see a date-time in SPSS, not doing any calculations on them (or only doing within-day day-time calculations, e.g. calculating hours worked). This can be accomplished by force_tz(x, "UTC") before calling dt2spss() above (force_tz() is from the lubridate package).

@hadley

This comment has been minimized.

Copy link
Member

hadley commented May 30, 2016

@huftis all date times in R are stored as number of seconds since unix epoch - time zones only affect how the times are displayed

@huftis

This comment has been minimized.

Copy link
Contributor

huftis commented May 30, 2016

@hadley Yes, I know. Did I write anything that implied something else?

@hadley

This comment has been minimized.

Copy link
Member

hadley commented May 30, 2016

It was your comment "I think it’s better to save all date-times as UTC date-times" that threw me off because in R all date-times are in UTC.

@hadley hadley closed this in 1d3fa74 May 30, 2016

@lock lock bot locked and limited conversation to collaborators Jun 27, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.