Published on :
Visualization CREATION_INTERNE

100% Stacked Horizontal Bar Chart with SGPLOT

This code is also available in: Deutsch Español Français
Awaiting validation
The script initializes a macro-variable and a fileref for ODS output. It then creates the `my_data` dataset from inline data (`datalines`). A `PROC SQL` step is used to calculate the total sum (`bartotal`) of `AMOUNT` for each `CATEGORY`. A second `DATA STEP` calculates the percentage (`catpct`) of each `AMOUNT` relative to its corresponding `bartotal` and applies a format. ODS HTML and ODS Graphics destinations are configured to generate an HTML file with the chart. Finally, `PROC SGPLOT` is used to create a 100% stacked horizontal bar chart, styled with custom colors, a 3D effect ('sheen'), and specific axis attributes. The output is generated in an HTML file and ODS destinations are closed.
Data Analysis

Type : CREATION_INTERNE


The source data `my_data` is created directly within the SAS script using the `datalines` statement, allowing autonomous execution without external dependencies.

1 Code Block
Global Declarations
Explanation :
Defines a macro variable `name` for the filename and a fileref `odsout` pointing to the current directory for ODS outputs.
Copied!
1%let name=bar6;
2 
3/*
4Set your current-working-directory (to read/write files), if you need to ...
5%let rc=%sysfunc(dlgcdir('c:\someplace\public_html'));
6*/
7filename odsout '.';
2 Code Block
DATA STEP Data
Explanation :
Creates a SAS dataset named `my_data` using inline data provided via the `datalines` statement, with variables `CATEGORY`, `SERIES` (character), 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 Data
Explanation :
Uses `PROC SQL` to modify the `my_data` dataset. It recalculates `my_data` by adding a new column `bartotal`, which is the sum of `AMOUNT` for each `CATEGORY`. The `GROUP BY` clause ensures `bartotal` is calculated per category.
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 Data
Explanation :
A second `DATA STEP` reads the `my_data` dataset (modified by `PROC SQL`) and calculates a new variable `catpct` representing the percentage of `AMOUNT` relative to `bartotal`. A `percent6.0` format is applied to `catpct`.
Copied!
1DATA my_data; SET my_data;
2FORMAT catpct percent6.0;
3catpct=amount/bartotal;
4RUN;
5 Code Block
ODS HTML
Explanation :
Closes the ODS LISTING destination. Opens the ODS HTML destination, specifies the HTML output file (`bar6.htm`), the page title, and the `htmlblue` style. Configures `ODS GRAPHICS` for PNG image export with specific dimensions, and sets two titles for the chart.
Copied!
1ODS LISTING CLOSE;
2ODS HTML path=odsout body="&name..htm"
3 (title="SGplot 100% Stacked Horizontal Bar (3D)")
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 "With 3D Shading";
6 Code Block
PROC SGPLOT
Explanation :
Generates a 100% stacked horizontal bar chart using `PROC SGPLOT`. The `my_data` dataset is used. The `hbar` option creates the bars, `response=catpct` indicates the variable to represent, `stat=sum` aggregates the values, and `group=series` stacks the bars by `SERIES`. `dataskin=sheen` applies a 3D visual effect. The X and Y axes are configured with custom style and display attributes.
Copied!
1PROC SGPLOT DATA=my_data noautolegend pad=(left=10% right=15%);
2label catpct='PERCENT';
3styleattrs datacolors=(cx9999ff cx993366 cxffffcc);
4hbar category / response=catpct stat=sum
5 group=series /*barwidth=.6*/
6 dataskin=sheen /* <--- basically, added this line! */
7 outlineattrs=(color=black) nostatlabel;
8xaxis
9 values=(0 to 1 BY .2)
10 labelattrs=(size=16pt weight=bold color=gray33)
11 valueattrs=(size=16pt weight=bold color=gray33)
12 offsetmax=0 grid minor minorcount=1;
13yaxis
14 labelattrs=(size=16pt weight=bold color=gray33)
15 valueattrs=(size=16pt weight=bold color=gray33)
16 display=(noticks);
17RUN;
7 Code Block
ODS HTML
Explanation :
Terminates procedure execution (`quit`). Closes the ODS HTML destination and 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.