Published on :
Reporting CREATION_INTERNE

Horizontal Grouped Bar Chart

This code is also available in: Deutsch Español Français
Awaiting validation
The script uses a DATA step to create a `my_data` dataset with categories, series, and amounts. It then configures ODS to produce HTML output and a PNG image file. The SGPLOT procedure is used to create a horizontal grouped bar chart (hbar) that visualizes the sum of amounts for different series within each category. Style options and custom titles are applied to the chart, and axes are formatted to improve readability. Finally, the ODS destinations are closed.
Data Analysis

Type : CREATION_INTERNE


The `my_data` dataset is created directly within the script using a DATA step and a DATALINES statement, providing data for categories A and B with their respective amounts.

1 Code Block
MACRO VAR
Explanation :
Defines a macro variable `name` used to name the output HTML file and the PNG image.
Copied!
1%let name=bar1;
2 Code Block
FILENAME
Explanation :
Assigns the `odsout` fileref to the current working directory, indicating where ODS output files will be saved.
Copied!
1filename odsout '.';
3 Code Block
DATA STEP Data
Explanation :
Creates the `my_data` dataset containing information on `CATEGORY`, `SERIES` (character), and `AMOUNT` (numeric). Data is provided via an embedded `datalines` statement.
Copied!
1DATA my_data;
2INPUT CATEGORY SERIES $ 3-11 AMOUNT;
3DATALINES;
41 Series A 5
52 Series A 7.8
61 Series B 9.5
72 Series B 5.9
8;
9RUN;
4 Code Block
ODS
Explanation :
Closes the default ODS LISTING destination and opens the ODS HTML destination, specifying the output path, the HTML file name (`bar1.htm`), a title for the HTML document, and the `htmlblue` style. `ods graphics` options are set to generate an 800x600px PNG image without a border and with an imagemap.
Copied!
1ODS LISTING CLOSE;
2ODS HTML path=odsout body="&name..htm"
3 (title="SGplot Horizontal Grouped Bar")
4 style=htmlblue;
5 
6ods graphics / imagefmt=png imagename="&name"
7 width=800px height=600px noborder imagemap;
5 Code Block
PROC SGPLOT
Explanation :
Sets the main and secondary titles of the chart with color, spacing, and height options. `PROC SGPLOT` is used to create a horizontal grouped bar chart (`hbar`). Data comes from `my_data`. Bars are grouped by `series` and display the sum of `amount` for each `category`. Style attributes are applied to the bars and axes for improved visual rendering.
Copied!
1title1 color=gray33 ls=0.5 h=23pt "Horizontal Grouped Bar";
2title2 color=gray33 ls=0.5 h=17pt "Compares values across categories";
3 
4PROC SGPLOT DATA=my_data noautolegend;
5styleattrs datacolors=(cx9999ff cx993366);
6hbar category / response=amount stat=sum
7 group=series groupdisplay=cluster grouporder=descending
8 outlineattrs=(color=black) nostatlabel;
9xaxis
10 values=(0 to 10 BY 2)
11 labelattrs=(size=16pt weight=bold color=gray33)
12 valueattrs=(size=16pt weight=bold color=gray33)
13 offsetmax=0 grid minor minorcount=1;
14yaxis
15 labelattrs=(size=16pt weight=bold color=gray33)
16 valueattrs=(size=16pt weight=bold color=gray33)
17 display=(noticks);
18RUN;
19 
20QUIT;
6 Code Block
ODS
Explanation :
Closes the ODS HTML destination and reactivates the ODS LISTING destination, thus completing the report generation process.
Copied!
1ODS HTML CLOSE;
2ODS 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.