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
It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
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-2024.png

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.

 

Register now!

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
  • 1230 views
  • 0 likes
  • 4 in conversation