将月,年和日转换为SAS格式化日期

问题描述 投票:-1回答:1

Ciao,我需要帮助将月份列,年份列和日期列转换为SAS格式日期。 SAS日期值是一个值,表示1960年1月1日到指定日期之间的天数。 SAS可以在A.D. 1582到A.D.19,900之间的日期进行计算。 1960年1月1日之前的日期是负数; 1960年1月1日之后的日期是正数。例如:月= 9,年= 1992年,日= 1,日期= 11946

r date sas lubridate
1个回答
2
投票

你可以使用difftime

ss <- "1992-9-15"
difftime(as.Date(ss), as.Date("1960-01-01"))
#Time difference of 11946 days

你没有给你的样品日期一天,但倒退工作给了1992年9月15日。

要存储为numeric

date_for_SAS <- as.numeric(difftime(as.Date(ss), as.Date("1960-01-01")))
date_for_SAS
#[1] 11946

更新

如果你有一个data.frame有三列daymonthyear,你可以做以下

df <- data.frame(
    day = c(1:10),
    month = rep(9, 10),
    year = rep(1992, 10))

df$date_for_SAS <- as.numeric(difftime(
    as.Date(sprintf("%s-%s-%s", df$year, df$month, df$day)),
    as.Date("1960-01-01")))
#   day month year date_for_SAS
#1    1     9 1992        11932
#2    2     9 1992        11933
#3    3     9 1992        11934
#4    4     9 1992        11935
#5    5     9 1992        11936
#6    6     9 1992        11937
#7    7     9 1992        11938
#8    8     9 1992        11939
#9    9     9 1992        11940
#10  10     9 1992        11941
© www.soinside.com 2019 - 2024. All rights reserved.