sas datetime到R date格式

sas datetime到R date格式,r,sas,datetime-format,R,Sas,Datetime Format,我有一个包含datetime变量的SAS数据集。我已使用sas7bdat包将此数据集移植到R中,但datetime变量以整数格式显示(例如1706835972)。有没有办法将此整数转换为日期格式?要精确匹配SAS输出的默认日期时间结构,您需要使用注释中提到的as.POSIXct,并另外使用tz=UTC参数: sasDateTimes <- c(1706835972, 1716835972, 1726835972, 1736835972, 1746835972, 1756835972, 1

我有一个包含datetime变量的SAS数据集。我已使用sas7bdat包将此数据集移植到R中,但datetime变量以整数格式显示(例如1706835972)。有没有办法将此整数转换为日期格式?

要精确匹配SAS输出的默认日期时间结构,您需要使用注释中提到的as.POSIXct,并另外使用tz=UTC参数:

sasDateTimes <- c(1706835972, 1716835972, 1726835972, 1736835972, 1746835972, 
1756835972, 1766835972, 1776835972, 1786835972, 1796835972, 1806835972, 
1816835972, 1826835972, 1836835972, 1846835972, 1856835972, 1866835972, 
1876835972, 1886835972, 1896835972, 1906835972, 1916835972, 1926835972, 
1936835972, 1946835972, 1956835972, 1966835972, 1976835972, 1986835972, 
1996835972)

rPOSIX <- as.POSIXct(sasDateTimes,origin='1960-01-01',tz="UTC")
rPOSIX
 [1] "2014-02-01 01:06:12 UTC" "2014-05-27 18:52:52 UTC" "2014-09-20 12:39:32 UTC" "2015-01-14 06:26:12 UTC" "2015-05-10 00:12:52 UTC" "2015-09-02 17:59:32 UTC"
 [7] "2015-12-27 11:46:12 UTC" "2016-04-21 05:32:52 UTC" "2016-08-14 23:19:32 UTC" "2016-12-08 17:06:12 UTC" "2017-04-03 10:52:52 UTC" "2017-07-28 04:39:32 UTC"
[13] "2017-11-20 22:26:12 UTC" "2018-03-16 16:12:52 UTC" "2018-07-10 09:59:32 UTC" "2018-11-03 03:46:12 UTC" "2019-02-26 21:32:52 UTC" "2019-06-22 15:19:32 UTC"
[19] "2019-10-16 09:06:12 UTC" "2020-02-09 02:52:52 UTC" "2020-06-03 20:39:32 UTC" "2020-09-27 14:26:12 UTC" "2021-01-21 08:12:52 UTC" "2021-05-17 01:59:32 UTC"
[25] "2021-09-09 19:46:12 UTC" "2022-01-03 13:32:52 UTC" "2022-04-29 07:19:32 UTC" "2022-08-23 01:06:12 UTC" "2022-12-16 18:52:52 UTC" "2023-04-11 12:39:32 UTC"

如果在SAS中输入了带有时区修正的数据,则应省去
tz
参数。然而,根据我的经验,SAS日期时间通常不分别存储在特定时区。不幸的是,它们是否存在通常不作为数据集的一部分存储;您可以查看数据集上使用的格式(如果它是时区显示格式之一,很可能它确实包含时区),但即使这样也不能保证。您必须使用数据源验证日期时间是否与本地日期时间相关(即,从1960年1月1日当地时间午夜起的秒数,或从1960年1月1日UTC午夜起的秒数)。

这看起来像是UNIX时间戳。Try SAS自1960年1月1日起使用秒数,因此请尝试以下操作:
as.POSIXct(1706835972,origin=“1960-01-01”)
在Hmisc中也使用
convertDateTime
转换SAS日期时间。如果您有SAS中的一些值,您知道正确答案,我们可以对这些值进行双重检查。两人一组,左值右格式:x=1706835972格式:01FEB14:01:06:12 x=1716835972格式:27MAY14:18:52:52 x=1726835972格式:20SEP14:12:39:32 x=1736835972格式:14JAN15:06:26:12 x=1746835972格式:10MAY15:00:12:52 x=1756835972格式:02SEP15:17:59:32 x=1766835972格式:27DEC15:11:46:12 x=1776835972格式:21APR16:05:32:52 x=1786835972格式:14AUG16:23:19:32 x=1796835972格式:08DEC16:17:06:12 x=1806835972格式:03APR17:10:52:52 x=1816835972格式:28JUL17:04:39:32 x=1826835972格式:20NOV17:22:26:12还要注意,SAS基本时间值没有时区划分,因此上面的值应该是UTC/GMT值,而不是特定于本地时区的。
x=1706835972, Formatted: 01FEB14:01:06:12
x=1716835972, Formatted: 27MAY14:18:52:52
x=1726835972, Formatted: 20SEP14:12:39:32
x=1736835972, Formatted: 14JAN15:06:26:12
x=1746835972, Formatted: 10MAY15:00:12:52
x=1756835972, Formatted: 02SEP15:17:59:32
x=1766835972, Formatted: 27DEC15:11:46:12
x=1776835972, Formatted: 21APR16:05:32:52
x=1786835972, Formatted: 14AUG16:23:19:32
x=1796835972, Formatted: 08DEC16:17:06:12
x=1806835972, Formatted: 03APR17:10:52:52
x=1816835972, Formatted: 28JUL17:04:39:32
x=1826835972, Formatted: 20NOV17:22:26:12
x=1836835972, Formatted: 16MAR18:16:12:52
x=1846835972, Formatted: 10JUL18:09:59:32
x=1856835972, Formatted: 03NOV18:03:46:12
x=1866835972, Formatted: 26FEB19:21:32:52
x=1876835972, Formatted: 22JUN19:15:19:32
x=1886835972, Formatted: 16OCT19:09:06:12
x=1896835972, Formatted: 09FEB20:02:52:52
x=1906835972, Formatted: 03JUN20:20:39:32
x=1916835972, Formatted: 27SEP20:14:26:12
x=1926835972, Formatted: 21JAN21:08:12:52
x=1936835972, Formatted: 17MAY21:01:59:32
x=1946835972, Formatted: 09SEP21:19:46:12
x=1956835972, Formatted: 03JAN22:13:32:52
x=1966835972, Formatted: 29APR22:07:19:32
x=1976835972, Formatted: 23AUG22:01:06:12
x=1986835972, Formatted: 16DEC22:18:52:52
x=1996835972, Formatted: 11APR23:12:39:32