BookmarkSubscribeRSS Feed
Sean_OConnor
Obsidian | Level 7

Folks,

 

I've a number of time variables in the following example time8. format; 17:34:34, 09:07:00.

 

What I would like to do is change them all to the nearest hour i.e. 17:00:00 and 09:00:00.

 

Is it possible to do this in an array similar to how you would do it for month variables e.g. 

 

array datevars {*} intdate1  previntdate;
do i=1 to dim(datevars);
datevars{i} = intnx('month',datevars{i},0,'begin');
end;

Any help is most welcome.

3 REPLIES 3
ChrisHemedinger
Community Manager

From your example, it looks like you want to "round down" to the hour the value is in, not necessarily the "nearest" hour boundary, right?

 

data _null_;
 x = intnx('hour', '05FEB2010:17:34:43'dt, 0, 'b'); 
 y = intnx('hour', '17:34:43't, 0, 'b');
 format x datetime20. y time8.;
 put x= y=;
run;

 

Output:

 

x=05FEB2010:17:00:00 y=17:00:00
SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Or maybe simpler:

want=hour(timevar) * (60 * 60);

I.e. take the hour and multiple it by minutes and seconds to get a time value.

 

 

Is it time variables you are dealing with or dates, as your question about time values does not match the example which uses date variables?  If its just time then:

want=input(cats(put(hour(timevar),z2.),":00:00"),tod11.);

 

Just chops off the min/sec and adds 00 again.

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1883 views
  • 0 likes
  • 4 in conversation