sas datetime到R日期格式 [英] sas datetime to R date format

查看:151
本文介绍了sas datetime到R日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a SAS dataset containing a datetime variable. I have ported this dataset into R using the sas7bdat package but the datetime variable is shown in integer number format (e.g. 1706835972). Is there any way to convert this integer into a date format?

推荐答案

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

To match exactly what SAS outputs for default datetime structure, you need to use as.POSIXct as was mentioned in comments, and additionally use the tz=UTC argument:

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以默认格式( DATETIME17。)显示的内容完全匹配。 SAS的输出:

Those will perfectly match what SAS displays in the default format (DATETIME17.). Output from SAS:

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

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

If the data was entered with time zone corrections into SAS, you should leave out the tz argument. In my experience, however, SAS datetimes usually are not stored respective to a particular timezone. Whether they are or not is not normally stored as part of the dataset, unfortunately; you might be able to look at the format that was used on the dataset (if it is one of the timezone displaying formats, odds are it does include timezones), but even that is not a guarantee. You would have to verify with the data source whether the datetimes are relative to local datetime (ie, # of seconds from 1/1/1960 local time midnight, or # of seconds from 1/1/1960 UTC midnight).

这篇关于sas datetime到R日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆