Hi all,
I'm trying to create a horizontal stacked bar chart to eventually export into Excel. I have my data but I can't get the chart working. Here's what I have so far :
To get the 100% stacked bar chart, follow the article at https://blogs.sas.com/content/iml/2014/04/08/construct-a-stacked-bar-chart-in-sas-where-each-bar-equ...
To get the special colors, you can use the STYLEATTRS statement or a discrete attribute map. See Consistent Group Colors by Value - Graphically Speaking
Here is the creation of the stacked plot. You can use names or Hex values for assigning colors:
data new; input lead $ north south east west; datalines; Joe 29 5 1 3 Suzie 4 6 8 19 Fred 109 8 2 36 Chris 235 48 29 54 ; data Long; set new; Region = 1; Sales = north; output; Region = 2; Sales = south; output; Region = 3; Sales = east; output; Region = 4; Sales = west; output; keep Lead Region Sales; run; /* make a stacked bar chart with 100% https://blogs.sas.com/content/iml/2014/04/08/construct-a-stacked-bar-chart-in-sas-where-each-bar-equals-100.html */ proc sort data=Long; by Lead Region; run; proc freq data=Long noprint order=data; by Lead; /* X categories on BY statement */ tables Region / out=FreqOut; /* Y (stacked groups) on TABLES statement */ weight Sales; run; proc print; run; /* assign names and colors to regions */ proc format; value RegionFmt 1 = "North" 2 = "South" 3 = "East" 4 = "West"; run; %let MyColors = Green Yellow Red Cyan; title "Sales Progress"; proc sgplot data=FreqOut; format Region RegionFmt.; styleattrs datacolors=(&MyColors);
hbar Lead / response=Percent group=Region groupdisplay=stack outlineattrs=(color=darkgray);yaxis discreteorder=data;
xaxis grid values=(0 to 100 by 10) label="Percentage of Total with Group"; run;
To get the 100% stacked bar chart, follow the article at https://blogs.sas.com/content/iml/2014/04/08/construct-a-stacked-bar-chart-in-sas-where-each-bar-equ...
To get the special colors, you can use the STYLEATTRS statement or a discrete attribute map. See Consistent Group Colors by Value - Graphically Speaking
Here is the creation of the stacked plot. You can use names or Hex values for assigning colors:
data new; input lead $ north south east west; datalines; Joe 29 5 1 3 Suzie 4 6 8 19 Fred 109 8 2 36 Chris 235 48 29 54 ; data Long; set new; Region = 1; Sales = north; output; Region = 2; Sales = south; output; Region = 3; Sales = east; output; Region = 4; Sales = west; output; keep Lead Region Sales; run; /* make a stacked bar chart with 100% https://blogs.sas.com/content/iml/2014/04/08/construct-a-stacked-bar-chart-in-sas-where-each-bar-equals-100.html */ proc sort data=Long; by Lead Region; run; proc freq data=Long noprint order=data; by Lead; /* X categories on BY statement */ tables Region / out=FreqOut; /* Y (stacked groups) on TABLES statement */ weight Sales; run; proc print; run; /* assign names and colors to regions */ proc format; value RegionFmt 1 = "North" 2 = "South" 3 = "East" 4 = "West"; run; %let MyColors = Green Yellow Red Cyan; title "Sales Progress"; proc sgplot data=FreqOut; format Region RegionFmt.; styleattrs datacolors=(&MyColors);
hbar Lead / response=Percent group=Region groupdisplay=stack outlineattrs=(color=darkgray);yaxis discreteorder=data;
xaxis grid values=(0 to 100 by 10) label="Percentage of Total with Group"; run;
That's PERFECT! THANK YOU. I truly appreciate it!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.