一个sas 8.2 转化14位字符型时间的程序
发表于 : 2013-04-28 12:34
在平时的SAS应用中常遇到要计算两个非SAS标准格式时间字段的时间间隔问题,很麻烦,于是我在查询他人方法并进行揉合的基础上编写了以下程序,也算解决了一个牛皮癣问题吧。
shuru数据集中有两个14位字符型的时间字段datefrom和dateto,格式形如"20130428122805"。运行程序后相应输出sas标准时间类型的字段fromsj和tosj,以及二者的间隔分钟intev。
代码: 全选
data shuchu;
set shuru;
fromriqi=substr(datefrom,1,8);
toriqi=substr(dateto,1,8);
fromhh=substr(datefrom,9,2)||':'||substr(datefrom,11,2)||':'||substr(datefrom,13,2);
tohh=substr(dateto,9,2)||':'||substr(dateto,11,2)||':'||substr(dateto,13,2);
format fromtime totime time8.;
format fromdate todate yymmdd10.;
fromdate=input(fromriqi,yymmdd10.);
todate=input(toriqi,yymmdd10.);
fromtime=input(fromhh,time8.);
totime=input(tohh,time8.);
fromsj=fromdate*24*3600+fromtime;
tosj=todate*24*3600+totime;
format fromsj tosj datetime20.;
intev=intck('minute',fromsj,tosj);
keep datefrom dateto fromsj tosj intev;
run;