BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Zax7
Fluorite | Level 6

Hi All,

I am looking for a way to make time groups from datetime values.

Further I have to summarize observations in these groups (time intervals).

 

The result must be like this:

Date, timeInterval time
2017.10.25 6:406:00-7:00
2017.10.25 8:118:00-9:00
2017.10.25 10:2710:00-11:00
2017.10.25 11:0611:00-12:00
2017.10.25 12:5312:00-13:00
2017.10.25 13:2013:00-14:00

 

I tried this code:

data want;
set have;
interval=cats(put(hour(Date_time),2.),':00-',put(hour(Date_time)+1,2.),':00');
run;

 

It looks like this is very good for me but I don't know this is an appropriate solution or not.

I am a very beginner SAS programmer.

 

Thank you for your answers.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Looks fine for your required output.  What I would say is that for further processing, i.e. checking if time is within those hours or what not, it would be a good idea to have a numeric upper and lower boundary, e.g.:

data want;
  set have;
lower=hour(date_time);
upper=hour(date_time)+1;
interval=cats(put(lower,2.),':00-',put(upper,2.),':00'); run;

That way you can do comparisons on lower/upper and use the text for outputs. 

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Looks fine for your required output.  What I would say is that for further processing, i.e. checking if time is within those hours or what not, it would be a good idea to have a numeric upper and lower boundary, e.g.:

data want;
  set have;
lower=hour(date_time);
upper=hour(date_time)+1;
interval=cats(put(lower,2.),':00-',put(upper,2.),':00'); run;

That way you can do comparisons on lower/upper and use the text for outputs. 

Zax7
Fluorite | Level 6

Thank you and i consider your suggestion.

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

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
  • 2 replies
  • 1598 views
  • 0 likes
  • 2 in conversation