我想做一些日期计算。但以下数据类型不适用于日期计算。
data character;
length id time1 time2 $ 30;
input id $ 1 time1 $ 4-23 time2 $ 28-46;
cards;
7 2013-11-07 11:49:28 2014-11-01 10:29:20
6 2014-09-14 07:36:21 2014-11-01 10:29:20
1 2014-10-14 09:36:21 2014-11-01 10:29:20
4 2011-04-14 04:36:21 2014-11-01 10:29:20
8 2012-02-14 17:36:21 2014-11-01 10:29:20
;
run;
我想要做的是计算这个间隔之间的分钟数或小时数。
data character2;
set character;
informat dateVar1 dateVar2 date9.;
format dateVar1 dateVar2 date9.;
dateVar1 = input(substr(time1,1,10), ymddttm24.);
dateVar2 = input(substr(time2,1,10), ymddttm24.);
/* interval=intck('day',dateVar1,dateVar2);*/
run;
答案 0 :(得分:1)
你可以这样做:
data character;
input @1 id $1. @4 date1 yymmdd10. @15 time1 hhmmss10. @27 date2 yymmdd10. @38 time2 hhmmss10.;
datetime1 = date1 *24*3600 + time1;
datetime2 = date2 *24*3600 + time2;
format datetime1 datetime2 datetime.;
drop date1 time1 date2 time2;
cards;
7 2013-11-07 11:49:28 2014-11-01 10:29:20
6 2014-09-14 07:36:21 2014-11-01 10:29:20
1 2014-10-14 09:36:21 2014-11-01 10:29:20
4 2011-04-14 04:36:21 2014-11-01 10:29:20
8 2012-02-14 17:36:21 2014-11-01 10:29:20
;
run;
答案 1 :(得分:0)
您还可以将DHMS功能与HOUR,MINUTE和SECOND功能配合使用。
DATA CHARACTER;
INPUT @1 ID $1. @3 DATE1 : YYMMDD10. @14 TIME1 : HHMMSS10. @24 DATE2 : YYMMDD10. @35 TIME2 : HHMMSS10.;
DATETIME1 = DHMS(DATE1,HOUR(TIME1),MINUTE(TIME1),SECOND(TIME1));
DATETIME2 = DHMS(DATE2,HOUR(TIME2),MINUTE(TIME2),SECOND(TIME2));
FORMAT DATE1 DATE2 DATE9. TIME1 TIME2 TIME8. DATETIME1 DATETIME2 DATETIME.;
/*DROP DATE1 TIME1 DATE2 TIME2;*/
CARDS;
7 2013-11-07 11:49:28 2014-11-01 10:29:20
6 2014-09-14 07:36:21 2014-11-01 10:29:20
1 2014-10-14 09:36:21 2014-11-01 10:29:20
4 2011-04-14 04:36:21 2014-11-01 10:29:20
8 2012-02-14 17:36:21 2014-11-01 10:29:20
;
RUN;