SAS中文论坛

标题: Question: How to assign time to different time period [打印本页]

作者: shiyiming    时间: 2009-7-31 22:11
标题: Question: How to assign time to different time period
Dear friends,

I have a data set about surgery. I have patient in time and patient out time. I need to calculate how many minutes are used in each time period
(from 7am to 3pm, 3pm to 5pm, 5pm to 7pm and 7pm to 11pm). The data like this:

date              intime                   outtime  
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   

The results should like this

date               intime                     outtime                  ut73 ut35 ut57 ut711
01/04/2009    01/04/2009 8:02     01/04/2009 12:52   290 0 0 0
01/04/2009    01/04/2009 20:51   02/04/2009 0:24          0 0 0 129
01/04/2009    01/04/2009 17:45   01/04/2009 20:39          0 0 75 99
01/04/2009    01/04/2009 15:27   01/04/2009 16:20    0 53 0 0
01/04/2009    01/04/2009 9:53     01/04/2009 11:48   115 0 0 0
02/04/2009    02/04/2009 11:35   03/04/2009 11:40    485 120 120 240
29/04/2009    29/04/2009 14:55   30/04/2009 15:01    485 121 120 240

Could anyone help this out? Many thanks.
作者: shiyiming    时间: 2009-8-1 12:53
标题: 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]
作者: shiyiming    时间: 2009-8-2 14:26
标题: Re: Question: How to assign time to different time period
死猪头
作者: shiyiming    时间: 2009-8-2 17:31
标题: Re: Question: How to assign time to different time period
<!-- s:( --><img src="{SMILIES_PATH}/icon_sad.gif" alt=":(" title="Sad" /><!-- s:( --> 我写了80行,死猪头就写了一行...
丢死人了...
学习了一下午刚明白了个大概,要是领会的不对一定告诉我昂.

[code:3rh0sysv]假设格式如下:
#表示根据公式计算的min(),max()的值
     LE                                RE
      I      #                         I
  INxxI 123456789012345678901234567890 Ixxxxxxx
xxxxxxI 123456789012345678901234567890 IxxOUT
      I                   #            I

intime&lt;=outtime时:

DATE       LE                               RE
           I      #                         I          =(3-1)*30+(25-5)=80
1-1        I    IN6789012345678901234567890 I xxxxxxx  --&gt;30(多加了5)
1-2 xxxxxx I 123456789012345678901234567890 I xxxxxxx  --&gt;30
1-3 xxxxxx I 1234567890123456789012345OUT   I          --&gt;25-5(已减去第1行多加的5)
           I                         #      I            

DATE       LE                               RE
           I #                              I          =(3-1)*30+(30-0)=90
1-1   INxx I 123456789012345678901234567890 I xxxxxxx  --&gt;30
1-2 xxxxxx I 123456789012345678901234567890 I xxxxxxx  --&gt;30
1-3 xxxxxx I 123456789012345678901234567890 I xxOUT    --&gt;30-0
           I                              # I           

intime&gt;outtime时:

DATE       LE                               RE
           I                          #     I          =(3-1)*30-(25-5)=40
1-1        I 12345                  IN67890 I xxxxxxx  --&gt;30(多加了20,已把第3行的5加这里)
1-2 xxxxxx I 123456789012345678901234567890 I xxxxxxx  --&gt;30
1-3 xxxxxx I ^^^^^OUT                       I          --&gt;-(25-5)(减去第1行多加的20)
           I     #                          I            

DATE       LE                               RE
           I                                I          =(3-1)*30-(30-0)=30
1-1        I                              # I   INxxx  --&gt;30(多加了30)
1-2 xxxxxx I 123456789012345678901234567890 I xxxxxxx  --&gt;30
1-3 xxOUT  I #                              I          --&gt;-(30-0)(减去多加的30)
           I                                I            
[/code:3rh0sysv]




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