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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 2039 views
  • 0 likes
  • 2 in conversation