How to round up time variable to nearest half-hour periods?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 86
Accepted Solution

How to round up time variable to nearest half-hour periods?

Hello everybody.

I have a time variable which its attributs are showed below:

Variable:TRD_EVENT_TM

Type: Char

Len: 8

Format: $8.

Informat: $8.

Label: TRD_EVENT_TM

 

 I want to round up time data to nearest half-hour periods like example which is described below:

10:05:30 ---> 10:30 and 11:42:21 ---> 12:00

I have used the code which is showed below, however it does not work.

TRD_EVENT_TIME = input(TRD_EVENT_TM,time16.);
TRD_EVENT_ROUNDED = round(TRD_EVENT_TIME,'00:30't); *This code rounds all times near half-hour periods;
TRD_EVENT_ROUFOR = put(TRD_EVENT_ROUNDED,hhmm.);

How can I do that?

Thanks in advance!

 

Accepted Solutions
Solution
‎06-12-2017 04:43 PM
Trusted Advisor
Posts: 1,519

Re: How to round up time variable to nearest half-hour periods?

 > I want to round up time data to nearest half-hour periods

 

If I understand what you want, you actually want to round up time data to next half-hour period

 

Like this?

 

data TEST;
  format TIME TIMEALIGNED time8.;
  do BASETIME='10:00:00't to '12:00:00't by '00:15:00't;
    do SECOND=-1 to 1;
      TIME       =sum(BASETIME ,SECOND);
      TIMEALIGNED=intnx('minute30',TIME,1,'b');
      output;
    end;
  end;
run;

proc print noobs;
  var TIME TIMEALIGNED;
run;

 

 

TIME TIMEALIGNED
9:59:59 10:00:00
10:00:00 10:30:00
10:00:01 10:30:00
10:14:59 10:30:00
10:15:00 10:30:00
10:15:01 10:30:00
10:29:59 10:30:00
10:30:00 11:00:00
10:30:01 11:00:00
10:44:59 11:00:00
10:45:00 11:00:00
10:45:01 11:00:00
10:59:59 11:00:00
11:00:00 11:30:00
11:00:01 11:30:00
11:14:59 11:30:00
11:15:00 11:30:00
11:15:01 11:30:00
11:29:59 11:30:00
11:30:00 12:00:00
11:30:01 12:00:00
11:44:59 12:00:00
11:45:00 12:00:00
11:45:01 12:00:00
11:59:59 12:00:00
12:00:00 12:30:00
12:00:01 12:30:00

 

 

View solution in original post


All Replies
Grand Advisor
Posts: 17,466

Re: How to round up time variable to nearest half-hour periods?

[ Edited ]

There's a trick you can use, add 15 or 30 minutes first and then round. Or something like that...I can't remember the specifics and would have to test it, so I'll just leave that to you, since you have to do that anyways Smiley Wink

 

EDIT: It's 15minutes. Add 15 minutes and then round.

 

Spoiler
To add 15 minutes, remember that 15 minutes is 15 minutes *60 seconds/minute -> 900 seconds. Add 900 to the time and then round as previously indicated.
Super User
Posts: 1,117

Re: How to round up time variable to nearest half-hour periods?

Please try the if then else as below 

 

if minute(trd_event_time)<=30 then trd_event_rounded=round(trd_event_time,'0:30:00'T);
else trd_event_rounded=round(trd_event_time,'0:60:00'T);
Thanks,
Jag
Respected Advisor
Posts: 3,841

Re: How to round up time variable to nearest half-hour periods?

[ Edited ]

@aminkarimid

Once you've got your string converted to a SAS time value (count of seconds) you can use the round() function to round to the closest multiple of 1800 seconds (=30 minutes).

In below code the value to round to is expressed as '00:30:00't. SAS will resolve this to a numerical value of 1800 and you could also pass in 1800 or 30*60 instead.

data test;
  format time timeAligned time8.;

  do baseTime='10:00:00't to '12:00:00't by '00:15:00't;
    do second=-1 to 1;
      time=sum(baseTime ,second);
      /* round to the closest 1/2 hour */
      timeAligned=round(time,'00:30:00't);
      output;
    end;
  end;
run;

proc print data=test;
run;

 

Capture.JPG

Solution
‎06-12-2017 04:43 PM
Trusted Advisor
Posts: 1,519

Re: How to round up time variable to nearest half-hour periods?

 > I want to round up time data to nearest half-hour periods

 

If I understand what you want, you actually want to round up time data to next half-hour period

 

Like this?

 

data TEST;
  format TIME TIMEALIGNED time8.;
  do BASETIME='10:00:00't to '12:00:00't by '00:15:00't;
    do SECOND=-1 to 1;
      TIME       =sum(BASETIME ,SECOND);
      TIMEALIGNED=intnx('minute30',TIME,1,'b');
      output;
    end;
  end;
run;

proc print noobs;
  var TIME TIMEALIGNED;
run;

 

 

TIME TIMEALIGNED
9:59:59 10:00:00
10:00:00 10:30:00
10:00:01 10:30:00
10:14:59 10:30:00
10:15:00 10:30:00
10:15:01 10:30:00
10:29:59 10:30:00
10:30:00 11:00:00
10:30:01 11:00:00
10:44:59 11:00:00
10:45:00 11:00:00
10:45:01 11:00:00
10:59:59 11:00:00
11:00:00 11:30:00
11:00:01 11:30:00
11:14:59 11:30:00
11:15:00 11:30:00
11:15:01 11:30:00
11:29:59 11:30:00
11:30:00 12:00:00
11:30:01 12:00:00
11:44:59 12:00:00
11:45:00 12:00:00
11:45:01 12:00:00
11:59:59 12:00:00
12:00:00 12:30:00
12:00:01 12:30:00

 

 

Trusted Advisor
Posts: 1,519

Re: How to round up time variable to nearest half-hour periods?

Or like this?

data TEST;
  format TIME TIMEALIGNED time8.;
  do BASETIME='10:00:00't to '12:00:00't by '00:15:00't;
    do SECOND=-1 to 1;
      TIME       =sum(BASETIME ,SECOND);
      TIMEALIGNED=intnx('minute30',TIME-1,1,'b');
      output;
    end;
  end;
run;

proc print noobs;
  var TIME TIMEALIGNED;
run;

 

TIME TIMEALIGNED
9:59:59 10:00:00
10:00:00 10:00:00
10:00:01 10:30:00
10:14:59 10:30:00
10:15:00 10:30:00
10:15:01 10:30:00
10:29:59 10:30:00
10:30:00 10:30:00
10:30:01 11:00:00
10:44:59 11:00:00
10:45:00 11:00:00
10:45:01 11:00:00
10:59:59 11:00:00
11:00:00 11:00:00
11:00:01 11:30:00
11:14:59 11:30:00
11:15:00 11:30:00
11:15:01 11:30:00
11:29:59 11:30:00
11:30:00 11:30:00
11:30:01 12:00:00
11:44:59 12:00:00
11:45:00 12:00:00
11:45:01 12:00:00
11:59:59 12:00:00
12:00:00 12:00:00
12:00:01 12:30:00
☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 189 views
  • 2 likes
  • 5 in conversation