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!
> 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 |
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.
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);
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;
> 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 |
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 |
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.
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.