DATA Step, Macro, Functions and more

different time points

Accepted Solution Solved
Reply
Contributor HG
Contributor
Posts: 23
Accepted Solution

different time points

Dear all,

We collected data at 5 time points. Sometime, the time points are beyond midnight. In that case, I need to add 24 hours on the time. My data step is like,

data have;

input id $4. (time1-time5) (:$5.);

cards;

1001 9:30 12:05 15:00 17:45 19:00

1002 11:30 13:00 16:40 0:15 2:45

;

run;

my final data set is like,

ID      time1  time2 time3 time4 time5

1001 9:30 12:05 15:00 17:45 19:00

1002 11:30 13:00 16:40 24:15 26:45

Thanks in advance!

HG


Accepted Solutions
Solution
‎01-19-2012 12:58 AM
Super User
Posts: 10,046

different time points

data have;
input id $4. (time1-time5) (: time.);
format time1-time5 time5.;
cards;
1001 9:30 12:05 15:00 17:45 19:00
1002 11:30 13:00 16:40 0:15 2:45
;
run;
data have(drop=i need);
 set have;
 array _t{*} time1-time5;
 do i=2 to dim(_t);
  if _t{i-1} gt _t{i} then need=1;
  if need then _t{i}=_t{i}+'24:00:00't;
 end;
run;



Ksharp

View solution in original post


All Replies
Super Contributor
Posts: 1,636

Re: different time points

Hi,

I think time should not be greater than 24. If you want to convert the time into hours, you can use the following code:

data have;

input id $4. (time1-time5) (:$5.);

cards;

1001 9:30 12:05 15:00 17:45 19:00

1002 11:30 13:00 16:40 0:15 2:45

;

run;

data want (drop=i k );

set have;

  format hour1-hour5 5.2;

  array t(*)$ time:;

  array hour(*) hour1-hour5;

  do i=1 to 5;

   hour(i)=input(scan(t(i),1,':'),2.)+(input(scan(t(i),2,':'),2.)/60);

  end;

  do i=1 to 4;

    if hour(i+1) <hour(i) then do;

           do k=i+1 to 5;

                       hour(k)=hour(k)+24;

                       end;

                     end;

            end;

run;

options nocenter;

proc print;run;

Obs   id    time1   time2   time3  time4   time5  hour1  hour2  hour3  hour4  hour5

   1   1001  9:30   12:05  15:00  17:45  19:00   9.50  12.08  15.00  17.75  19.00

   2   1002  11:30  13:00  16:40  0:15   2:45   11.50  13.00  16.67  24.25  26.75

Linlin

PROC Star
Posts: 7,492

different time points

Similar to Linlin's suggestion, but I would read the times in with a time informat and then convert them to a datetime.  This way, you can handle times that extend over two days:

data have;

input id $4. (time1-time5) (:time5.);

cards;

1001 9:30 12:05 15:00 17:45 19:00

1002 11:30 13:00 16:40 0:15 2:45

;

run;

data want;

  set have;

  array times(5) time1-time5;

  array dtimes(5);

  start=1;

  dtimes(1)=dhms(mdy(1,start,2012),hour(times(1)),minute(times(1)),0);

  start=1;

  do i=2 to dim(times);

    if times(i) lt times(i-1) then start+1;

    dtimes(i)=dhms(mdy(1,start,2012),hour(times(i)),minute(times(i)),0);

  end;

run;

Solution
‎01-19-2012 12:58 AM
Super User
Posts: 10,046

different time points

data have;
input id $4. (time1-time5) (: time.);
format time1-time5 time5.;
cards;
1001 9:30 12:05 15:00 17:45 19:00
1002 11:30 13:00 16:40 0:15 2:45
;
run;
data have(drop=i need);
 set have;
 array _t{*} time1-time5;
 do i=2 to dim(_t);
  if _t{i-1} gt _t{i} then need=1;
  if need then _t{i}=_t{i}+'24:00:00't;
 end;
run;



Ksharp

Contributor HG
Contributor
Posts: 23

different time points

Thanks a lot for your help.

HG

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 196 views
  • 6 likes
  • 4 in conversation