Published on :
Chart CREATION_INTERNE

100% Stacked Bar Chart with SGPLOT

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins by defining a macro variable and a fileref. It then creates a dataset named 'my_data' from DATALINES statements. A PROC SQL step is used to enrich 'my_data' by calculating the total amounts per category ('bartotal'). A second DATA STEP calculates the percentage of each amount relative to its 'bartotal' ('catpct') and formats this new variable. Finally, the script configures ODS destinations (HTML and PNG graphic) and uses PROC SGPLOT to generate a 100% stacked bar chart, illustrating these percentages with aesthetic customizations for axes and colors.
Data Analysis

Type : CREATION_INTERNE


The main dataset 'my_data' is created directly in the script via a DATALINES statement, making it an internal data source to the code.

1 Code Block
Macro Variable / Fileref
Explanation :
Defines the macro variable 'name' used for the output filename and the 'odsout' fileref pointing to the current directory for ODS output.
Copied!
1%let name=col5;
2filename odsout '.';
2 Code Block
DATA STEP Data
Explanation :
Creates the SAS dataset 'my_data' by reading raw data provided directly in the script ('datalines') with the variables CATEGORY, SERIES, and AMOUNT.
Copied!
1DATA my_data;
2INPUT CATEGORY SERIES $ 3-11 AMOUNT;
3DATALINES;
41 Series A 5.0
52 Series A 6.8
63 Series A 9.2
71 Series B 6.5
82 Series B 6.9
93 Series B 5.6
101 Series C 2.3
112 Series C 3.1
123 Series C 2.3
13;
14RUN;
3 Code Block
PROC SQL
Explanation :
Modifies the 'my_data' dataset by adding a new column 'bartotal', which represents the sum of 'AMOUNT' for each 'CATEGORY' using an SQL query.
Copied!
1PROC SQL;
2 create TABLE my_data as
3 select *, sum(amount) as bartotal
4 from my_data
5 group BY category;
6QUIT; RUN;
4 Code Block
DATA STEP
Explanation :
Modifies the 'my_data' dataset by calculating 'catpct', the percentage of 'AMOUNT' relative to 'bartotal', and applies a percentage format to this new variable.
Copied!
1DATA my_data; SET my_data;
2FORMAT catpct percent6.0;
3catpct=amount/bartotal;
4RUN;
5 Code Block
ODS
Explanation :
Closes the default ODS LISTING destination. Opens the ODS HTML destination, specifying the output path, HTML filename, and style. Configures ODS GRAPHICS options for the generated PNG image. Defines the main chart titles with style and color options.
Copied!
1ODS LISTING CLOSE;
2ODS HTML path=odsout body="&name..htm"
3 (title="SGplot 100% Stacked Bar")
4 style=htmlblue;
5 
6ods graphics / imagefmt=png imagename="&name"
7 width=800px height=600px noborder imagemap;
8 
9title1 color=gray33 ls=0.5 h=23pt "100% Stacked Bar";
10title2 color=gray33 ls=0.5 h=17pt "Compares the percent each value";
11title3 color=gray33 ls=0.5 h=17pt "to a total across categories";
6 Code Block
PROC SGPLOT
Explanation :
Generates a 100% stacked bar chart using the 'my_data' dataset. Bars represent 'CATEGORY', height is based on 'catpct', and stacks are defined by 'SERIES'. The code customizes legends, data colors, bar attributes (width, outline), and the appearance of the X and Y axes (labels, values, grid).
Copied!
1PROC SGPLOT DATA=my_data noautolegend pad=(left=10% right=15%);
2label catpct='PERCENT';
3styleattrs datacolors=(cx9999ff cx993366 cxffffcc);
4vbar category / response=catpct stat=sum
5 group=series barwidth=.6
6 outlineattrs=(color=black) nostatlabel;
7yaxis
8 values=(0 to 1 BY .2)
9 labelattrs=(size=16pt weight=bold color=gray33)
10 valueattrs=(size=16pt weight=bold color=gray33)
11 offsetmax=0 grid minor minorcount=1;
12xaxis
13 labelattrs=(size=16pt weight=bold color=gray33)
14 valueattrs=(size=16pt weight=bold color=gray33)
15 display=(noticks);
16RUN;
7 Code Block
ODS
Explanation :
Ends active procedures. Closes the ODS HTML destination, saving the associated HTML file and images. Then reactivates the ODS LISTING destination.
Copied!
1QUIT;
2ODS HTML CLOSE;
3ODS LISTING;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.