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
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
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
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;
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
Thanks a lot for your help.
HG
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.