/* Data Source: http://www-fars.nhtsa.dot.gov ; */ data work.fatalities; input date fatalities; informat date mmddyy8.; format date Date7.; datalines; 01012008 251 01022008 174 01032008 168 01042008 247 01052008 234 01062008 332 01072008 182 01082008 176 01092008 220 01102008 221 01112008 271 01122008 233 01132008 191 01142008 202 01152008 127 01162008 184 01172008 167 01182008 237 01192008 220 01202008 189 01212008 205 01222008 211 01232008 211 01242008 130 01252008 263 01262008 285 01272008 231 01282008 185 01292008 150 01302008 197 01312008 271 02012008 229 02022008 326 02032008 293 02042008 235 02052008 183 02062008 209 02072008 206 02082008 256 02092008 276 02102008 260 02112008 179 02122008 176 02132008 216 02142008 193 02152008 168 02162008 314 02172008 249 02182008 172 02192008 179 02202008 150 02212008 228 02222008 264 02232008 250 02242008 289 02252008 170 02262008 166 02272008 112 02282008 168 02292008 252 03012008 299 03022008 273 03032008 189 03042008 182 03052008 144 03062008 171 03072008 244 03082008 225 03092008 207 03102008 157 03112008 160 03122008 132 03132008 170 03142008 223 03152008 243 03162008 305 03172008 209 03182008 166 03192008 181 03202008 199 03212008 285 03222008 280 03232008 196 03242008 167 03252008 194 03262008 218 03272008 230 03282008 229 03292008 249 03302008 156 03312008 174 04012008 187 04022008 178 04032008 192 04042008 197 04052008 374 04062008 244 04072008 233 04082008 221 04092008 183 04102008 202 04112008 260 04122008 297 04132008 237 04142008 149 04152008 157 04162008 165 04172008 220 04182008 295 04192008 301 04202008 299 04212008 185 04222008 174 04232008 214 04242008 174 04252008 276 04262008 320 04272008 298 04282008 159 04292008 212 04302008 154 05012008 243 05022008 282 05032008 263 05042008 322 05052008 187 05062008 182 05072008 200 05082008 177 05092008 264 05102008 328 05112008 306 05122008 170 05132008 163 05142008 147 05152008 213 05162008 300 05172008 344 05182008 309 05192008 148 05202008 201 05212008 210 05222008 234 05232008 270 05242008 328 05252008 237 05262008 275 05272008 129 05282008 183 05292008 168 05302008 190 05312008 297 06012008 280 06022008 182 06032008 160 06042008 158 06052008 165 06062008 287 06072008 300 06082008 320 06092008 212 06102008 213 06112008 184 06122008 232 06132008 304 06142008 297 06152008 291 06162008 239 06172008 203 06182008 203 06192008 233 06202008 214 06212008 385 06222008 373 06232008 211 06242008 220 06252008 194 06262008 238 06272008 276 06282008 294 06292008 328 06302008 236 07012008 167 07022008 224 07032008 201 07042008 368 07052008 330 07062008 311 07072008 179 07082008 229 07092008 233 07102008 195 07112008 285 07122008 269 07132008 272 07142008 173 07152008 230 07162008 190 07172008 216 07182008 225 07192008 323 07202008 233 07212008 175 07222008 267 07232008 252 07242008 230 07252008 285 07262008 336 07272008 292 07282008 163 07292008 218 07302008 182 07312008 175 08012008 275 08022008 392 08032008 296 08042008 192 08052008 187 08062008 200 08072008 201 08082008 379 08092008 296 08102008 315 08112008 214 08122008 197 08132008 184 08142008 244 08152008 258 08162008 354 08172008 283 08182008 171 08192008 206 08202008 175 08212008 160 08222008 264 08232008 282 08242008 245 08252008 204 08262008 190 08272008 187 08282008 203 08292008 334 08302008 325 08312008 409 09012008 259 09022008 175 09032008 167 09042008 181 09052008 201 09062008 323 09072008 267 09082008 167 09092008 162 09102008 159 09112008 186 09122008 241 09132008 289 09142008 229 09152008 167 09162008 178 09172008 165 09182008 244 09192008 255 09202008 339 09212008 297 09222008 161 09232008 195 09242008 163 09252008 236 09262008 359 09272008 293 09282008 249 09292008 209 09302008 189 10012008 153 10022008 202 10032008 278 10042008 322 10052008 292 10062008 197 10072008 207 10082008 227 10092008 203 10102008 330 10112008 323 10122008 275 10132008 178 10142008 207 10152008 227 10162008 201 10172008 319 10182008 298 10192008 279 10202008 239 10212008 181 10222008 158 10232008 251 10242008 313 10252008 323 10262008 308 10272008 176 10282008 176 10292008 206 10302008 168 10312008 343 11012008 411 11022008 326 11032008 181 11042008 183 11052008 196 11062008 210 11072008 251 11082008 337 11092008 267 11102008 136 11112008 209 11122008 154 11132008 230 11142008 239 11152008 267 11162008 234 11172008 192 11182008 151 11192008 182 11202008 197 11212008 239 11222008 256 11232008 224 11242008 193 11252008 179 11262008 252 11272008 234 11282008 256 11292008 304 11302008 255 12012008 270 12022008 202 12032008 188 12042008 179 12052008 313 12062008 365 12072008 222 12082008 167 12092008 229 12102008 228 12112008 234 12122008 262 12132008 241 12142008 211 12152008 218 12162008 236 12172008 151 12182008 171 12192008 272 12202008 332 12212008 214 12222008 170 12232008 331 12242008 318 12252008 188 12262008 290 12272008 257 12282008 177 12292008 181 12302008 167 12312008 216 ; run; /* Create dataset for the starting day of each month spanning the original date range; */ data month_starts(keep=year month date weekday week enddate endwkday endweek); format year F4.; format date Date7.; format maxdate Date7.; format enddate Date7.; set fatalities end=lastobs; retain mindate 100000; retain maxdate 0; mindate = min(date, mindate); maxdate = max(date, maxdate); format enddate Date7.; if lastobs then do; startyear = year(mindate); endyear = year(maxdate); startmonth = month(mindate); mon = startmonth; year = startyear; do i=startyear to endyear by 1; do month=mon to 12 by 1; date = mdy( month, 1, i ); if( month EQ 12 ) then do; nm = 1; ny = i+1; end; else do; nm = month+1; ny = i; end; enddate = mdy( nm, 1, ny ) - 1; weekday = weekday( date ); week = week( date ); year = year( date ); endwkday = weekday( enddate ); endweek = week( enddate ); if( date < maxdate ) then output; else date = maxdate-1; end; mon = 1; end; if( enddate < maxdate ) then do; put date enddate maxdate; date = maxdate; weekday = weekday( date ); week = week( date ); year = year( date ); output; end; end; run; /* Create the data for the series plot to draw the line boundaries */ data month_boundaries (keep=year wd wk); set month_starts; retain pts 1; if weekday > 1 then do; if month = 12 then pts = 7; else pts = 7; do i=1 to pts by 1; select (i); when (1) do; wd = 1; wk = endweek; end; when (2) do; wd = 1; wk = week; end; when (3) do; wd = weekday; wk = week; end; when (4) do; wd = weekday; wk = week - 1; end; when (5) do; wd = 8; wk = week - 1; end; when (6) do; wd = 8; if( endwkday = 7 ) then wk = endweek; else wk = endweek - 1; end; when (7) do; wd = .; wk = week - 1; /* insert . to break line */ end; end; wd = wd - 0.5; wk = wk + 0.5; output; end; end; else if weekday = 1 then do; do i=1 to 5 by 1; select (i); when (1) do; wd = 1; wk = endweek; end; when (2) do; wd = 1; wk = week-1; end; when (3) do; wd = 8; wk = week-1; end; when (4) do; wd = 8; if( endwkday = 7 ) then wk = endweek; else wk = endweek - 1; end; when (5) do; wd = .; wk = week; end; end; wd = wd - 0.5; wk = wk + 0.5; output; end; end; /* Add closing boundary for december */ if( month = 12 ) then do; if( endwkday = 7 ) then do; wd = 7.5; wk = endweek + 0.5; output; wd = 0.5; wk = endweek + 0.5; output; wd = .; wk = week; output; end; else do; wd = 7.5; wk = endweek - 1 + 0.5; output; wd = endwkday + 0.5; wk = endweek - 1 + 0.5; output; wd = endwkday + 0.5; wk = endweek + 0.5; output; wd = 0.5; wk = endweek + 0.5; output; wd = .; wk = week; output; end; end; run; proc format; value wkdayname 1 = 'Sunday' 2 = 'Monday' 3 = 'Tuesday' 4 = 'Wednesday' 5 = 'Thursday' 6 = 'Friday' 7 = 'Saturday' ; value monthname 1 = 'JAN' 2 = 'FEB' 3 = 'MAR' 4 = 'APR' 5 = 'MAY' 6 = 'JUN' 7 = 'JUL' 8 = 'AUG' 9 = 'SEP' 10 = 'OCT' 11 = 'NOV' 12 = 'DEC' ; run; /* Create the data for the block plot */ data month_blocks(keep=year month midmonthweek prevwk); set month_starts end=lastobs; by year; month = month( date )-1; wkofyear = week( date ); retain prevwk .; format month monthname.; midmonthweek = (wkofyear + prevwk)/2; if first.year then do; end; else do; output; end; prevwk = wkofyear; if last.year then do; midmonthweek = (wkofyear + week(mdy(12,31,year)))/2; month = 12; output; end; run; /* Merge data sets for sgrender */ data combined; merge fatalities month_boundaries month_blocks; format weekday wkdayname. wd wkdayname.; weekday = weekday( date ); week = week( date ); label week='Week' weekday="Day of Week"; day = day(date); run; proc template; define statgraph cal_heatmap; begingraph / designheight=210 designwidth=1200; entrytitle 'Traffic Fatalities in the US during 2008'; rangeattrmap name="rmap"; range -0.5 - max / rangecolormodel=(white cxeeeeaa red); endrangeattrmap; rangeattrvar attrmap="rmap" attrvar=cvar var=fatalities; layout overlay / walldisplay=none xaxisopts=(display=none) yaxisopts=(display=(tickvalues) reverse=true linearopts=(tickvaluelist=(1 2 3 4 5 6 7)) ); heatmapparm x=week y=weekday colorresponse=cvar/ name="heatmap" xgap=1 ygap=1; seriesplot x=wk y=wd / connectorder=xvalues break=true lineattrs=(color=black thickness=1) primary=true; innermargin / align=bottom; blockplot x=prevwk block=month / valuehalign=center display=(values); endinnermargin; continuouslegend "heatmap" ; endlayout; endgraph; end; run; proc sgrender data=combined template=cal_heatmap; run;