BookmarkSubscribeRSS Feed
SASGeek
Obsidian | Level 7

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 :

 

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 
    ;
run;
 
proc sgplot data=new pctlevel=group;
hbar lead /  stat=percent;
run;
 
Here's the output I want with the colors to always be set to those colors.
SASGeek_0-1762551432163.png

 

 
 
Thank you
1 REPLY 1
Rick_SAS
SAS Super FREQ

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;

 

SGPlot19.png

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1 reply
  • 93 views
  • 0 likes
  • 2 in conversation