/* SAS program to accompany the article "Create a stacked band plot in SAS" by Rick Wicklin, published 31JAN2018 on The DO Loop blog: https://blogs.sas.com/content/iml/2018/01/31/create-stacked-band-plot-sas.html This program shows how to construct a stacked band plot in SAS. The data were obtained from http://robslink.com/SAS/democd93/us_co2_emissions.htm and are discussed in https://blogs.sas.com/content/sastraining/2017/07/11/u-s-co2-emissions-are-on-the-decline/ */ /* 1. Original data is in "wide form": CO2 emission for five sources for each year. */ data CO2; informat Transportation Commercial Residential Industrial Electric COMMA5.; input Year Transportation Commercial Residential Industrial Electric; datalines; 2016 1,879 235 308 928 1,821 2015 1,845 240 321 942 1,913 2014 1,821 233 347 955 2,050 2013 1,803 223 333 952 2,050 2012 1,776 201 286 934 2,034 2011 1,813 221 325 915 2,170 2010 1,844 221 335 911 2,270 2009 1,827 223 338 839 2,158 2008 1,893 226 357 960 2,373 2007 2,016 217 344 999 2,425 2006 2,009 208 323 1,012 2,358 2005 1,981 227 364 1,006 2,416 2004 1,954 238 371 1,057 2,350 2003 1,888 241 385 1,020 2,319 2002 1,889 231 367 1,029 2,288 2001 1,848 230 367 1,043 2,273 2000 1,869 239 380 1,070 2,310 1999 1,825 226 360 1,074 2,204 1998 1,779 223 339 1,103 2,192 1997 1,741 240 371 1,130 2,101 1996 1,722 240 389 1,125 2,033 1995 1,678 231 361 1,092 1,960 1994 1,644 229 364 1,081 1,944 1993 1,604 226 372 1,063 1,919 1992 1,588 229 357 1,069 1,843 1991 1,565 228 347 1,023 1,830 1990 1,584 227 340 1,057 1,831 1989 1,588 235 380 1,041 1,826 1988 1,576 238 374 1,039 1,758 1987 1,516 227 353 990 1,680 1986 1,469 224 347 954 1,613 1985 1,418 224 355 983 1,619 1984 1,387 243 360 1,035 1,588 1983 1,357 233 336 931 1,521 1982 1,352 232 355 985 1,481 1981 1,383 232 357 1,123 1,551 1980 1,398 250 383 1,197 1,544 1979 1,452 266 422 1,320 1,505 1978 1,461 266 455 1,267 1,448 1977 1,404 265 457 1,279 1,442 1976 1,352 269 471 1,264 1,351 1975 1,289 249 449 1,208 1,244 1974 1,279 264 449 1,332 1,251 1973 1,313 275 472 1,389 1,286 ; /* 2. Convert from wide to long format, as shown in https://blogs.sas.com/content/iml/2015/02/25/plotting-multiple-series-transforming-data-from-wide-to-long.html */ proc sort data=CO2 out=Wide; by Year; /* sort X categories */ run; proc transpose data=Wide out=Long(rename=(Col1=Value)) name=Source; by Year; /* original X var */ var Transportation Commercial Residential Industrial Electric; /* original Y vars */ run; /* 3. create a stacked bar chart: https://blogs.sas.com/content/iml/2014/04/08/construct-a-stacked-bar-chart-in-sas-where-each-bar-equals-100.html */ ods graphics / subpixel=on; title "U.S. Carbon Dioxide Emissions from Energy Consumption"; title2 "Stacked Bar Chart"; proc sgplot data=Long; vbar Year / response=Value group=Source groupdisplay=stack grouporder=data; xaxis type=linear thresholdmin=0; yaxis grid values=(0 to 6000 by 1000); label Source = "Source of CO2" Value = "CO2 (mmt)"; run; /* Looks good, but with 43 years of data, the bar chart starts to lose its discrete nature. All those bar begin to clutter the display and the vertical lines become a distraction. You can replace the stacked bar chart with a stacked band plot. */ /* 4. Accumulate the contributions of each group in the 'cumValue' variable. Add the lag(cumValue) variable and call it 'Previous'. Create a band plot for each group with LOWER=Previous and UPPER=cumValue. */ data Energy; set Long; by Year; if first.Year then cumValue=0; /* initialize cumulative amount to 0 for each year */ cumValue + Value; Previous = lag(cumValue); if first.Year then Previous=0; /* initialize baseline value for each year */ label Source = "Source of CO2" cumValue = "CO2 (mmt)"; run; /* By default, the legend appears at the bottom of the graph. I used the POSTITION= option to move the graph to the right side and used the SORTORDER=REVERSEAUTO option to reverse the order of the groups, which matches the order of the display. The REVERSEAUTO option was introduced in SAS 9.4M5. */ title2 "Stacked Band Plot"; proc sgplot data=Energy; band x=Year lower=Previous upper=cumValue / group=Source; xaxis display=(nolabel) thresholdmin=0; yaxis grid values=(0 to 6000 by 1000); keylegend / position=right sortorder=reverseauto; /* SAS 9.4M5: reverse legend order */ run; /* Optionally, you can directly label the bands with the names of the groups, rather than use a legend. I prefer the label towards the left side so that the reader can immediately see what each colored band represents. */ data Labels; set Energy; where Year = 1979; /* position labels at 1979 */ Label = Source; XPos = Year; YPos = (cumValue + Previous) / 2; keep Label XPos YPos; run; data EnergyLabels; set Energy Labels; run; title2 "Stacked Band Plot"; proc sgplot data=EnergyLabels noautolegend; band x=Year upper=cumValue lower=Previous / group=Source; refline 1000 to 6000 by 1000 / axis=y lineattrs=GraphGridLines transparency=0.75; text x=XPos y=Ypos text=Label; xaxis display=(nolabel) values=(1973, 1980 to 2010 by 10, 2016) offsetmin=0 offsetmax=0; yaxis grid values=(0 to 6000 by 1000) label="CO2 (mmt)"; run;