Published on :
Reporting INTERNAL_CREATION

100% Stacked Horizontal Bar Chart

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins by creating a 'my_data' dataset with information on categories, series, and amounts. Then, it uses PROC SQL to calculate the total sum of amounts for each category ('bartotal'). A second DATA STEP is used to calculate the percentage of each amount relative to its category's total ('catpct') and apply a percentage format. Finally, the ODS output is configured to generate an HTML file and a PNG image. PROC SGPLOT is used to create the 100% stacked horizontal bar chart, using 'category' on the Y-axis, 'catpct' as the response, and 'series' for grouping the bars. Titles and style attributes are applied to enhance the presentation.
Data Analysis

Type : INTERNAL_CREATION


Initial data is created directly within the script via a DATALINES block in a DATA STEP. It is then transformed and enriched by subsequent PROC SQL and DATA STEP steps.

1 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates a dataset named 'my_data'. It uses the 'input' statement to define three variables (CATEGORY, SERIES, AMOUNT) and the 'datalines' block to directly read data into the dataset.
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;
2 Code Block
PROC SQL Data
Explanation :
This SQL procedure modifies the 'my_data' dataset. It calculates the sum of 'amount' for each 'category', stores this total in a new 'bartotal' column, and recreates the 'my_data' dataset with this new column. This is an essential aggregation step for percentage calculations.
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;
3 Code Block
DATA STEP
Explanation :
This DATA STEP updates the existing 'my_data' dataset. It calculates the percentage ('catpct') of each 'amount' relative to the corresponding 'bartotal'. A 'percent6.0' format is applied to the new 'catpct' variable for percentage display.
Copied!
1DATA my_data; SET my_data;
2FORMAT catpct percent6.0;
3catpct=amount/bartotal;
4RUN;
4 Code Block
PROC SGPLOT
Explanation :
This block handles output and visualization. ODS (Output Delivery System) statements are used to generate HTML output and a PNG image of the graph. PROC SGPLOT is then called to create a 100% stacked horizontal bar chart. It uses 'category' on the Y-axis, 'catpct' as the response variable, and 'series' to group the bars, thus displaying the proportional contribution of each series within each category. Style options, titles, and axes are configured to customize the graph's appearance.
Copied!
1ODS LISTING CLOSE;
2ODS HTML path=odsout body="&name..htm"
3 (title="SGplot 100% Stacked Horizontal 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 Horizontal 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";
12 
13PROC SGPLOT DATA=my_data noautolegend pad=(left=10% right=15%);
14label catpct='PERCENT';
15styleattrs datacolors=(cx9999ff cx993366 cxffffcc);
16hbar category / response=catpct stat=sum
17 group=series /*barwidth=.6*/
18 outlineattrs=(color=black) nostatlabel;
19xaxis
20 values=(0 to 1 BY .2)
21 labelattrs=(size=16pt weight=bold color=gray33)
22 valueattrs=(size=16pt weight=bold color=gray33)
23 offsetmax=0 grid minor minorcount=1;
24yaxis
25 labelattrs=(size=16pt weight=bold color=gray33)
26 valueattrs=(size=16pt weight=bold color=gray33)
27 display=(noticks);
28RUN;
29 
30QUIT;
31ODS HTML CLOSE;
32ODS 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.