SAS中文论坛

标题: 日期差怎么计算 [打印本页]

作者: shiyiming    时间: 2003-10-2 21:52
标题: 日期差怎么计算
请教各位大侠:
sas里面的日期差计算怎么办
好像用减法或是intck可以的
但对于跨千年的时间好像结果有问题啊
比如1/1/99与1/1/02的时间差系统计算出的不对啊
怎么个设置法或是还有别的其他方法?
谢谢!
作者: shiyiming    时间: 2003-10-2 22:43
用适当的informats可以解决。

比如计算1/1/99到1/1/02之间的间隔天数,用如下的语句:

data _null_;
input @1 x ddmmyy6. @8 y ddmmyy6.;
days=y-x;
put x=;
put y=;
put days=;
datalines;
010199 010102
;
run;

输出结果(log窗口)如下:

x=14245
y=15341
days=1096
作者: shiyiming    时间: 2003-10-3 10:02
Your SAS was confused by 1/1/02, becasue it dose not know your date is 1/1/2002 or 1/1/1902. You can use yearcutoff option to tells SAS how to explain your two digits year.

"Yearcutoff=" specifies the first year of a hundred-year span for SAS to use.

So you can write the following option before your program.

options yearcutoff=1950;

It tells SAS all your dates will be between 1950 and 2049.
作者: shiyiming    时间: 2003-10-3 13:11
我再回去试一试吧
不过我记得好像SAS系统不是默认1960的么
也就是说百年指1960-2059的呀
我也记得不是太清楚了
实在不行就多加个回合
反正都是本世纪前后的数据
作者: shiyiming    时间: 2003-10-3 14:21
标题: Re: ask for help
[quote="阿P":bd7b9]请教各位大侠:
sas里面的日期差计算怎么办
好像用减法或是intck可以的
但对于跨千年的时间好像结果有问题啊
比如1/1/99与1/1/02的时间差系统计算出的不对啊
怎么个设置法或是还有别的其他方法?
谢谢![/quote:bd7b9]

data test;
t1='1jan98'd;
t2='1jan03'd;
t3=t2-t1;
t4=intck('day',t1,t2);
;
proc print;run;

我用的是8.2,没发现有问题.
是不是你的版本太低了?
作者: shiyiming    时间: 2003-10-6 15:54
我用的也是8.2啊
用前面的程序算是没有错误的
不过我的是数据集
数据量比较大,20多万条
发现出错的规律:
世纪前的相减有问题,(比如99年减96年的)相差100年
都是世纪后的没有问题
世纪后的减世纪前的没有问题
世纪前的减世纪后的有问题,(比如99年减00年的)相差100年
所有结果都是正值
到现在还没有确诊什么问题啊
望继续帮忙!
作者: shiyiming    时间: 2003-10-6 17:10
标题: 回复:
能否把你数据的一部分(包括出错的部分)传上来?
作者: shiyiming    时间: 2003-10-7 18:23
标题: 回复:
libname test 'j:\test' ;
data test1;
informat MAXJH YYMMDD10. MAXSS YYMMDD10.;
format MAXJH YYMMDD10. MAXSS YYMMDD10.;
set test.gao;
d1=MAXSS-MAXJH;
d2=intck('day',MAXJH,MAXSS);
run;
proc print;run;

/*Then you will find the error.  But how to correct it?*/

data test2;
informat MAXJH YYMMDD10. MAXSS YYMMDD10.;
format MAXJH YYMMDD10. MAXSS YYMMDD10.;
set test.gao;
if year(MAXJH)>2900 then MAXJH=MDY(month(MAXJH),day(MAXJH),(year(MAXJH)-1000));
if year(MAXSS)>2900 then MAXSS=MDY(month(MAXSS),day(MAXSS),(year(MAXSS)-1000));
d1=MAXSS-MAXJH;
d2=intck('day',MAXJH,MAXSS);
run;
proc print;run;


OUTPUT:

                                                           FIRST_
        Obs         MAXJH         MAXSS    SERIAL_NO         IMPR         d1         d2

          1    2999-01-01    2002-11-16    1000000016    08/01/96    -363828    -363828
          2    2999-01-01    2999-01-01    1000000017    01/01/96          0          0
          3    2999-01-01    2999-01-01    1000000020    08/01/96          0          0
          4    2999-01-01    2999-01-01    1000000021    08/01/96          0          0
          5    2999-01-01    2999-01-01    1000000022    08/01/96          0          0
          6    2999-01-01    2999-01-01    1000000023    08/01/96          0          0
          7    2999-01-01    2999-01-01    1000000025    08/01/96          0          0
          8    2999-01-01    2999-01-01    1000000026    08/01/96          0          0
          9    2999-01-01    2999-01-01    1000000027    08/01/96          0          0
         10    2003-02-17    2003-08-18    1000000036    08/01/93        182        182


从以上结果可看出你的原始数据集好象有问题.

                                                              FIRST_
           Obs         MAXJH         MAXSS    SERIAL_NO         IMPR      d1      d2

             1    1999-01-01    2002-11-16    1000000016    08/01/96    1415    1415
             2    1999-01-01    1999-01-01    1000000017    01/01/96       0       0
             3    1999-01-01    1999-01-01    1000000020    08/01/96       0       0
             4    1999-01-01    1999-01-01    1000000021    08/01/96       0       0
             5    1999-01-01    1999-01-01    1000000022    08/01/96       0       0
             6    1999-01-01    1999-01-01    1000000023    08/01/96       0       0
             7    1999-01-01    1999-01-01    1000000025    08/01/96       0       0
             8    1999-01-01    1999-01-01    1000000026    08/01/96       0       0
             9    1999-01-01    1999-01-01    1000000027    08/01/96       0       0
            10    2003-02-17    2003-08-18    1000000036    08/01/93     182     182







比较笨的办法 <!-- s:D --><img src="{SMILIES_PATH}/icon_biggrin.gif" alt=":D" title="Very Happy" /><!-- s:D -->  抛砖引玉而已
作者: shiyiming    时间: 2003-10-7 21:21
能捉老鼠的猫都是好猫
谢谢了
作者: bird    时间: 2014-8-19 12:49
datdif(begindate,enddate,'act/act')




欢迎光临 SAS中文论坛 (http://www.mysas.net/forum/) Powered by Discuz! X3.2