|
|
沙发

楼主 |
发表于 2009-8-1 12:53:07
|
只看该作者
Re: Question: How to assign time to different time period
[code:jwtajwyx]data original;
input date:ddmmyy10. indate:ddmmyy10. intime:time5. outdate:ddmmyy10. outtime:time5.;
id=_n_;
intime=dhms(indate,hour(intime),minute(intime),second(intime));
outtime=dhms(outdate,hour(outtime),minute(outtime),second(outtime));
drop indate outdate;
format date yymmdd10. intime outtime datetime13.;
datalines;
01/04/2009 01/04/2009 8:02 01/04/2009 12:52
01/04/2009 01/04/2009 20:51 02/04/2009 0:24
01/04/2009 01/04/2009 17:45 01/04/2009 20:39
01/04/2009 01/04/2009 15:27 01/04/2009 16:20
01/04/2009 01/04/2009 9:53 01/04/2009 11:48
02/04/2009 02/04/2009 11:35 03/04/2009 11:40
29/04/2009 29/04/2009 14:55 30/04/2009 15:01
;
/* 创建内部开始、结束时间 */
data temp;
set original;
inner_intime=intime-dhms(date,0,0,0);
inner_outtime=inner_intime+(outtime-intime);
format inner_intime inner_outtime datetime13.;
run;
/* 拆分跨天记录 */
data temp(drop=temp i);
set temp;
day_int=ceil(inner_outtime/86399)-1;
if day_int>0 then do;
temp=inner_outtime;
inner_outtime=86399;
output;
if day_int>1 then do;
do i=1 to day_int-1;
inner_intime=dhms(i,0,0,0);
inner_outtime=dhms(i,23,59,59);
output;
end;
end;
inner_intime=dhms(day_int,0,0,0);
inner_outtime=temp;
output;
end;
else output;
run;
/* 计算分钟数 */
data temp(drop=i start end);
set temp;
start=hour(inner_intime);
end=hour(inner_outtime);
array arr{0:23} hour0-hour23;
do i=start to end;
arr(i)=60;
if i=start then arr(i)=60-minute(inner_intime);
if i=end then do;
if second(inner_outtime)=59 then arr(i)=minute(inner_outtime)+1;
else arr(i)=minute(inner_outtime);
end;
if start=end then do;
arr(i)=minute(inner_outtime-inner_intime);
if arr(i)=0 then arr(i)=1;
end;
end;
temp_ut73=sum(of hour7-hour14);
temp_ut35=sum(of hour15-hour16);
temp_ut57=sum(of hour17-hour18);
temp_ut711=sum(of hour19-hour22);
run;
/* 合计分钟数 */
data final;
do _n_=1 by 1 until(last.id);
set temp;
by id;
ut73=sum(temp_ut73,ut73);
ut35=sum(temp_ut35,ut35);
ut57=sum(temp_ut57,ut57);
ut711=sum(temp_ut711,ut711);
end;
keep date intime outtime ut:;
run;[/code:jwtajwyx] |
|