BookmarkSubscribeRSS Feed
Sean_OConnor
Fluorite | Level 6

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 Innovate 2025: Call for Content! Submit your proposals before Sept 25. Accepted presenters get amazing perks to attend the conference!
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.

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 3 replies
  • 1400 views
  • 0 likes
  • 4 in conversation