简体   繁体   English

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

[英]Convert Month and Year and Day to SAS formatted date

Ciao, I am in need of assistance in converting month column, year column, and date column, into SAS format date. Ciao,在将月份列,年份列和日期列转换为SAS格式的日期方面,我需要帮助。 SAS date value is a value that represents the number of days between January 1, 1960, and a specified date. SAS日期值是代表1960年1月1日到指定日期之间的天数的值。 SAS can perform calculations on dates ranging from AD 1582 to AD 19,900. SAS可以执行从AD 1582到AD 19,900的日期的计算。 Dates before January 1, 1960, are negative numbers; 1960年1月1日之前的日期为负数; dates after January 1, 1960, are positive numbers.Example: month=9,year=1992,day = 1, date=11946 1960年1月1日之后的日期为正数。例如:month = 9,year = 1992,day = 1,date = 11946

You can use difftime 您可以使用difftime

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

You don't give a day for your sample date, but working backwards gives the 15th of September 1992. 您没有为采样日期指定一天,但是向后计算会得出1992年9月15日的信息。

To store as a numeric do numeric存储

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

Update 更新资料

If you have a data.frame with three columns day , month and year , you can do the following 如果您有一个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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM