BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aminkarimid
Lapis Lazuli | Level 10

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!

 
1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

 > 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

5 REPLIES 5
Reeza
Super User

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 😉

 

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.
Jagadishkatam
Amethyst | Level 16

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
Patrick
Opal | Level 21

@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

ChrisNZ
Tourmaline | Level 20

 > 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

 

 

ChrisNZ
Tourmaline | Level 20

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 6970 views
  • 2 likes
  • 5 in conversation