BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
kajal_30
Quartz | Level 8

I have a column datetime in sas dataset and want to create a graphs to show maximum number of times exists in a range .

For eg few events occur between 2pm and 4 pm so my graph should show maximum dots or peak line during that interval.

max  range for the graph should be 24 hours and each interval should be 1 hour apart. 

Also from the datetime column I just need to take time portion.

 

regards

kajal 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
call streaminit(123);
do datetime=dhms('01jan2022'd,21,0,0) to dhms('02jan2022'd,3,0,0) by 60*60;
 hour=scan(put(timepart(datetime),timeampm.),1,':');
 count=rand('integer',100,600);
 output;
end;
format datetime e8601dt.;
run;

proc sgplot data=have;
series x=hour y=count;
xaxis  type=discrete discreteorder=data label='Time';
run;

Ksharp_0-1724723165974.png

 

View solution in original post

10 REPLIES 10
PaigeMiller
Diamond | Level 26

A bunch of questions enter my mind:

 

You say: "want to create a graphs to show maximum number of times exists in a range". Maximum of what? If you take the word "maximum" out of your statement, then it makes sense to me what (I think) you want, you want the number of times this variable exists in the range. But I'm guessing.

 

You say "For eg few events occur between 2pm and 4 pm so my graph should show maximum dots or peak line during that interval." If there are few events, should that not be the minimum dots or lowest value on the line?

 

Are these always 2 hour intervals? Are we talking about multiple days, or just talking about time interval regardless of day?

 

 

--
Paige Miller
kajal_30
Quartz | Level 8

sorry correction to my question.

 

need to create a graph showing max frequencies of 'time of event '  in a particular time range 

most of the events occur between 2pm and 4 pm should show maximum dots on the graph between 2pm and 4 pm range.

Regards

kajal

PaigeMiller
Diamond | Level 26

I'm still not understanding your usage of "maximum" in your problem statement. Please explain in more detail.

 

Please show us a simple example, show us some example data and the plot you want.

--
Paige Miller
ballardw
Super User

In a data step the TIMEPART function will extract the time of day from a DATETIME value. Example;

data want;
   set have;
   timeofday = timepart(datetimevariable);
run;

By "each interval should be 1 hour " do you mean the tick marks on the time axis or the actual values of the time rounded to 1 hour to graph, or both?

 

What is going on the Y axis? A count of something? A measurement or recorded value?

PaigeMiller
Diamond | Level 26

Thanks for the plot. So we are not calculating a maximum of anything, we are calculating a count. Is that correct?

 

Try this:

 

/* UNTESTED CODE AS I DON"T HAVE EXAMPLE DATA */

data have2;
    set have;
    hour=hour(yourdatetimevariable);
run;
proc freq data=have2;
    table hour/noprint out=_counts_;
run;
proc sgplot data=_counts_;
     series x=hour y=count;
run;
--
Paige Miller
kajal_30
Quartz | Level 8

Thank you so much it also works 

 

Ksharp
Super User
data have;
call streaminit(123);
do datetime=dhms('01jan2022'd,21,0,0) to dhms('02jan2022'd,3,0,0) by 60*60;
 hour=scan(put(timepart(datetime),timeampm.),1,':');
 count=rand('integer',100,600);
 output;
end;
format datetime e8601dt.;
run;

proc sgplot data=have;
series x=hour y=count;
xaxis  type=discrete discreteorder=data label='Time';
run;

Ksharp_0-1724723165974.png

 

kajal_30
Quartz | Level 8

Thank you so much it works 

ChrisNZ
Tourmaline | Level 20

If you want this smooth plot, you need points between the hours. Do you have that detail? How many points?

 

Mind that the more points, the less volume by point. So if the volume is say 600 in an hour, a per-minute plot will show around 10 on the Y axis.

A per-30-minute plot will show around 300 on the Y axis. Etc.

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 10 replies
  • 1630 views
  • 0 likes
  • 5 in conversation