knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(datasetjson) library(knitr)
Dataset JSON Version 1.1 provides a significant improvement of the handling of dates and times. In version 1.0, there wasn't a clear instruction around anchoring the origin of numeric dates and date times, and given that SAS uses 1960-01-01 and R uses the POSIX date of 1970-01-01, this created a slightly complex discrepancy. Version 1.1 instead opts to use ISO8601 formatted dates, times, and date times, and the target data type is clearly stated within the column metadata. This makes the true date value unambiguous.
Starting in {datasetjson} v0.3.0 we've introduce support for Dataset JSON v1.1.0. As such, we automatically handle date, time, and date time conversions. There are a few considerations you need to make when dealing with these types to make things work properly.
Version 5 SAS Transport Files didn't have a notion of a "date", "time" or "datetime" type. Instead, using the SAS convention these were just Integer values with a display format attached. Dataset JSON Version 1.1 explicitly clarifies numeric date types using the dataType
and targetDataType
fields in the columns metadata. Consider these variables.
tibble::tribble( ~itemOID, ~name, ~label, ~dataType, ~length, ~targetDataType, ~displayFormat, ~keySequence, 'IT.DF.CHARDT', 'CHARDT', 'Character Date', 'date', 8L, NA_character_, NA_character_, NA_integer_, 'IT.DF.CHARTM', 'CHARTM', 'Character Time', 'time', 10L, NA_character_, NA_character_, NA_integer_, 'IT.DF.CHARDTM', 'CHARDTM', 'Character Datetime', 'datetime', 19L, NA_character_, NA_character_, NA_integer_, 'IT.DF.NUMDT', 'NUMDT', 'Numeric date', 'time', NA_integer_, "integer", "TIME8", NA_integer_, 'IT.DF.NUMTM', 'NUMTM', 'Numeric time', 'time', NA_integer_, "integer", "TIME8", NA_integer_, 'IT.DF.NUMDTM', 'NUMDTM', 'Numeric datetime', 'datetime', NA_integer_, "integer", "E8601DT", NA_integer_ ) |> kable()
In the table above, we have the metadata for both character and numeric dates, times, and date times. Both sets of variables have the same values within dataType
. The difference is the optional field of targetDataType
, where the value for the numeric variables is set to integer
. Both read_dataset_json()
and write_dataset_json()
rely on these fields and as such they must be set properly. This comes with a few assumption and requirements.
Date
(see help("Date", package="base")
)hms
POSIXct
and anchored to the UTC timezone.If any of these assumption don't work for your purpose or if you find other situations we need to handle, please leave an issue on Github as we want to make sure we support the community as best we can.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.