Published on :
Chart INTERNAL_CREATION

Horizontal Stacked Bar Chart

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins by defining a macro variable `name`. It then creates a dataset named `my_data` using a DATA step with `datalines` to include the data directly within the script. This data contains information on categories, series, and amounts. The script then uses ODS (Output Delivery System) to generate HTML output containing an SGPLOT graph. The graph is a horizontal stacked bar chart that compares amounts by `CATEGORY`, grouped by `SERIES`. Style attributes and titles are applied to the chart. The image is exported in PNG format within the HTML file.
Data Analysis

Type : INTERNAL_CREATION


Data is created directly within the script via a DATA step and the DATALINES statement.

1 Code Block
Macro/Global
Explanation :
Defines a macro variable `name` used for the output filename and assigns the `odsout` fileref to the current directory.
Copied!
1%let name=bar3;
2filename odsout '.';
2 Code Block
DATA STEP Data
Explanation :
Creates the `my_data` dataset by reading embedded raw data (datalines) with CATEGORY, SERIES, and AMOUNT variables.
Copied!
1DATA my_data;
2INPUT CATEGORY SERIES $ 3-11 AMOUNT;
3DATALINES;
41 Series A 5
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
10;
11RUN;
3 Code Block
ODS
Explanation :
Closes the default LISTING output and opens an ODS HTML environment to generate the report. Specifies the output path, HTML filename, and style.
Copied!
1ODS LISTING CLOSE;
2ODS HTML path=odsout body="&name..htm"
3 (title="SGplot Horizontal Stacked Bar")
4 style=htmlblue;
4 Code Block
ODS GRAPHICS
Explanation :
Configures graphic options for ODS, defining the image format (PNG), image name, size, and other attributes.
Copied!
1ods graphics / imagefmt=png imagename="&name"
2width=800px height=600px noborder imagemap;
3 
5 Code Block
Global Statements
Explanation :
Defines the main and secondary chart titles with specific color, size, and font options.
Copied!
1title1 color=gray33 ls=0.5 h=23pt "Horizontal Stacked Bar";
2title2 color=gray33 ls=0.5 h=17pt "Compares the contribution of each value";
3title3 color=gray33 ls=0.5 h=17pt "to a total across categories";
4 
6 Code Block
PROC SGPLOT
Explanation :
Executes the SGPLOT procedure to create a horizontal stacked bar chart. Uses `my_data`, specifies bar colors, X-axis (response and statistic), and Y-axis (category) with formatting and style options.
Copied!
1PROC SGPLOT DATA=my_data noautolegend;
2styleattrs datacolors=(cx9999ff cx993366);
3hbar category / response=amount stat=sum
4 group=series /*groupdisplay=cluster grouporder=descending*/
5 outlineattrs=(color=black) nostatlabel;
6xaxis
7 values=(0 to 16 BY 4)
8 labelattrs=(size=16pt weight=bold color=gray33)
9 valueattrs=(size=16pt weight=bold color=gray33)
10 offsetmax=0 grid minor minorcount=1;
11yaxis
12 labelattrs=(size=16pt weight=bold color=gray33)
13 valueattrs=(size=16pt weight=bold color=gray33)
14 display=(noticks);
15RUN;
7 Code Block
ODS
Explanation :
Ends the SGPLOT procedure (`quit`), closes the ODS HTML file, and reactivates the default LISTING output.
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.