BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
HG
Calcite | Level 5 HG
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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

4 REPLIES 4
Linlin
Lapis Lazuli | Level 10

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

art297
Opal | Level 21

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;

Ksharp
Super User
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

HG
Calcite | Level 5 HG
Calcite | Level 5

Thanks a lot for your help.

HG

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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