Hello,
I have a set containing 2 types of injuries (coded 0 & 1), and a date/time field. I'm interested in creating a heat map showing the frequency of occurrence of each injury type by time of day in a week, and another map showing frequency of occurrence by month of year.
Is that doable? If so, what would be the code?
This is a sample of my set:
proc format;
value fmt
2='Monday'
3='Tuesday'
4='Wednesday'
5='Thursday'
6='Firday'
7='Saturday'
8='Sunday'
;
run;
data sample (compress=binary);
INPUT record_id Injury1 Injury2 year InjuryDateTime:DATETIME20.;
weekday=weekday(datepart(InjuryDateTime));
if weekday=1 then weekday=8; /*To make x value from Mon to Sun*/
hour=hour(timepart(InjuryDateTime));
format InjuryDateTime datetime20. weekday fmt.;
DATALINES;
2497728 1 0 2021 27APR2021:15:00:00
2590114 1 0 2023 25NOV2023:20:20:00
2590765 1 0 2023 18AUG2023:17:20:00
2590842 1 0 2023 06JUL2023:10:00:00
2591821 1 0 2023 16AUG2023:18:36:00
2591839 1 0 2023 31JUL2023:14:46:00
2591860 1 0 2023 04JUL2023:17:59:00
2599177 1 0 2023 01OCT2023:18:00:00
2599305 1 0 2023 01OCT2023:14:00:00
2600027 1 0 2023 22OCT2023:19:00:00
2600280 1 0 2023 22OCT2023:19:00:00
2502863 0 1 2021 13NOV2021:13:00:00
2507650 0 1 2021 24SEP2021:16:25:00
2507987 0 1 2021 12AUG2021:14:21:00
2508819 0 1 2021 07MAY2021:18:15:00
2516706 0 1 2021 21SEP2021:09:07:00
2516721 0 1 2021 20SEP2021:21:00:00
2516869 0 1 2021 05SEP2021:09:40:00
2517049 0 1 2021 14AUG2021:08:18:00
2517142 0 1 2021 05AUG2021:22:00:00
2517187 0 1 2021 31JUL2021:00:40:00
;
proc freq data=sample noprint;
table weekday*hour/out=freq1;
run;
proc sgplot data=freq1 noautolegend;
heatmapparm x=weekday y=hour colorresponse=count / colormodel=(CXEDF8E9 CXC7E9C0 CXA1D99B CX74C476 CX31A354 CX006D2C );
xaxis integer values=(2 to 8 by 1) valueshint;
yaxis integer values=(0 to 24 by 1) valueshint;
run;
Let this paper be an inspiration:
Paper SAS4341-2016
Graph A Million with the SGPLOT Procedure
Prashant Hebbar and Sanjay Matange, SAS Institute Inc.
https://support.sas.com/resources/papers/proceedings16/SAS4341-2016.pdf
It's using the HEATMAP statement on numerous occasions.
In the third maintenance release of SAS 9.4 (SAS 9.4 M3), the SGPLOT procedure supports a HEATMAP statement.
Koen
Thank you for responding. The referenced paper doesn't seem to contain a map similar to what I want, which would be something like this:
Did you try something like this?
data for_plot;
set sample;
day=weekday(datepart(InjuryDateTime));
hour=hour(InjuryDateTime);
run;
proc sgplot;
heatmap x=day y=hour / freq=Injury1;
run;
proc sgplot;
heatmap x=day y=hour / freq=Injury2;
run;
I did, but the graphs comes up weird:
I was hoping for Y axis labels of 0-23 and X axis labels of 1-7
proc format;
value fmt
2='Monday'
3='Tuesday'
4='Wednesday'
5='Thursday'
6='Firday'
7='Saturday'
8='Sunday'
;
run;
data sample (compress=binary);
INPUT record_id Injury1 Injury2 year InjuryDateTime:DATETIME20.;
weekday=weekday(datepart(InjuryDateTime));
if weekday=1 then weekday=8; /*To make x value from Mon to Sun*/
hour=hour(timepart(InjuryDateTime));
format InjuryDateTime datetime20. weekday fmt.;
DATALINES;
2497728 1 0 2021 27APR2021:15:00:00
2590114 1 0 2023 25NOV2023:20:20:00
2590765 1 0 2023 18AUG2023:17:20:00
2590842 1 0 2023 06JUL2023:10:00:00
2591821 1 0 2023 16AUG2023:18:36:00
2591839 1 0 2023 31JUL2023:14:46:00
2591860 1 0 2023 04JUL2023:17:59:00
2599177 1 0 2023 01OCT2023:18:00:00
2599305 1 0 2023 01OCT2023:14:00:00
2600027 1 0 2023 22OCT2023:19:00:00
2600280 1 0 2023 22OCT2023:19:00:00
2502863 0 1 2021 13NOV2021:13:00:00
2507650 0 1 2021 24SEP2021:16:25:00
2507987 0 1 2021 12AUG2021:14:21:00
2508819 0 1 2021 07MAY2021:18:15:00
2516706 0 1 2021 21SEP2021:09:07:00
2516721 0 1 2021 20SEP2021:21:00:00
2516869 0 1 2021 05SEP2021:09:40:00
2517049 0 1 2021 14AUG2021:08:18:00
2517142 0 1 2021 05AUG2021:22:00:00
2517187 0 1 2021 31JUL2021:00:40:00
;
proc freq data=sample noprint;
table weekday*hour/out=freq1;
run;
proc sgplot data=freq1 noautolegend;
heatmapparm x=weekday y=hour colorresponse=count / colormodel=(CXEDF8E9 CXC7E9C0 CXA1D99B CX74C476 CX31A354 CX006D2C );
xaxis integer values=(2 to 8 by 1) valueshint;
yaxis integer values=(0 to 24 by 1) valueshint;
run;
Thank you so much and my apologies for the delayed reply. This worked great!
Sorry. I just realize a BIG/SEVERITY issue.
Since SAS take Sunday as start of a week, the weak day of Sunday is 1, therefore the PROC FORMAT should be consist with it :
proc format; value fmt 2='Monday' 3='Tuesday' 4='Wednesday' 5='Thursday' 6='Firday' 7='Saturday' 1='Sunday' ; run;
It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.
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.
Ready to level-up your skills? Choose your own adventure.