DATA Step, Macro, Functions and more

How can I round time stamp to the bottom hour

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

How can I round time stamp to the bottom hour

Hello SAS experts,

 

I have a numeric datetime variable like this 18AUG2015:16:25:00. I want to round this to bottom hour. Output should be like 18AUG2015:16:00:00. How can i do this. Thanks in advance


Accepted Solutions
Solution
‎07-14-2017 07:29 PM
Super User
Posts: 10,484

Re: How can I round time stamp to the bottom hour

INTNX function with alignment 'B' for begin:

data example;
   dt = '18AUG2015:16:25:00'dt;
   y  = intnx('hour',dt,0,'B');
   format y datetime.;
   put y= datetime.;
run;

View solution in original post


All Replies
Solution
‎07-14-2017 07:29 PM
Super User
Posts: 10,484

Re: How can I round time stamp to the bottom hour

INTNX function with alignment 'B' for begin:

data example;
   dt = '18AUG2015:16:25:00'dt;
   y  = intnx('hour',dt,0,'B');
   format y datetime.;
   put y= datetime.;
run;
Contributor
Posts: 28

Re: How can I round time stamp to the bottom hour

Thanks ballardw for your reply. I tried with your piece of code. There is
one problem, I have a huge dataset and with your code SAS is running from
last 5 minutes. I can see in the log that it worked and created
the timestamp variable the way I wanted. But it is running and running and
not producing output in the results window.

Thanks

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Super User
Posts: 17,784

Re: How can I round time stamp to the bottom hour

Remove the PUT statement. There will be no OUTPUT in the results window because you're creating a data set and there is no 'results'. 

 

You can also use the ROUND function, but I doubt that will help. Datetime are stored in seconds, so the nearest hour is the nearest, 

60seconds*60minutes=3600 seconds. 

 

Time = round(time_var, 3600);
Contributor
Posts: 28

Re: How can I round time stamp to the bottom hour

Thanks Reeza.

 I tried round function but it didnt produce the desired results. But after removing the PUT statement i got the desired results. I do not why i was expexting output in the results window in a data step, that was such a lame question. I think I am lost. Thanks again

Super User
Posts: 10,484

Re: How can I round time stamp to the bottom hour


AMFR wrote:

Thanks Reeza.

 I tried round function but it didnt produce the desired results. But after removing the PUT statement i got the desired results. I do not why i was expexting output in the results window in a data step, that was such a lame question. I think I am lost. Thanks again


AND the original reason I had PUT was for demostration. I expected you to only use the function as needed in your actual code.

 

I believe the issue with @Reeza's suggestion was using ROUND which would round up when seconds exceed 1800 with in any hour  interval. Floor might work but you'd need to do more arithemetic to get the right trim and with the existing solution likely not worth headache.

Contributor
Posts: 28

Re: How can I round time stamp to the bottom hour

Thanks for the details, I appreciate it
Super User
Super User
Posts: 6,499

Re: How can I round time stamp to the bottom hour

Not sure what "bottom hour" means. Does that mean when the minute hand is pointing down at the 6?  Do you want to convert '06:25' and '06:35' to '06:00' or '06:30'?

Contributor
Posts: 28

Re: How can I round time stamp to the bottom hour

I meant to  convert '06:25' and '06:35' to '06:00'?

 

Thanks.

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 233 views
  • 2 likes
  • 4 in conversation