Published on :
Reporting INTERNAL_CREATION

Grouped Bar Chart with SGPLOT

This code is also available in: Deutsch Español Français
Awaiting validation
The script initializes a macro variable `name` to 'col1'. It then defines an `odsout` `fileref` to point to the current directory. A `DATA STEP` is used to create a `my_data` dataset with categories, series, and amounts. The output is configured to generate an HTML file (`col1.htm`) and a PNG image via ODS HTML and ODS Graphics. `PROC SGPLOT` is then called to create a grouped vertical bar chart, visualizing the sum of amounts by category, grouped by series, with custom titles and axis attributes.
Data Analysis

Type : INTERNAL_CREATION


The data is directly integrated into the SAS script via a `datalines` block within a `DATA STEP` named `my_data`. No external data source or SASHELP library is used for the main input of the chart.

1 Code Block
DATA STEP Data
Explanation :
This `DATA STEP` block creates the `my_data` dataset that will be used for the chart. It reads four observations with the variables `CATEGORY`, `SERIES` (character, length 3-11), and `AMOUNT` (numeric) from the `datalines`.
Copied!
1DATA my_data;
2 INPUT CATEGORY SERIES $ 3-11 AMOUNT;
3 DATALINES;
41 Series A 5
52 Series A 7.8
61 Series B 9.5
72 Series B 5.9
8 ;
9RUN;
2 Code Block
PROC SGPLOT
Explanation :
This block initializes the `name` macro variable, then configures ODS output to generate an HTML file (`col1.htm`) and PNG graphics in the current directory. `PROC SGPLOT` is then invoked to create a grouped vertical bar chart from the `my_data` dataset. The bars represent the sum of `AMOUNT` for each `CATEGORY`, grouped by `SERIES`. Style, color, and axis options are applied to enhance the visualization. The chart is exported as a PNG image and embedded in the HTML file.
Copied!
1%let name=col1;
2 
3filename odsout '.';
4 
5ODS LISTING CLOSE;
6ODS HTML path=odsout body="&name..htm"
7 (title="SGplot Grouped Bar")
8 style=htmlblue;
9 
10ods graphics / imagefmt=png imagename="&name"
11 width=800px height=600px noborder imagemap;
12 
13title1 color=gray33 ls=0.5 h=23pt "Grouped Bar";
14title2 color=gray33 ls=0.5 h=17pt "Compares values across categories";
15 
16PROC SGPLOT DATA=my_data noautolegend;
17 styleattrs datacolors=(cx9999ff cx993366);
18 vbar category / response=amount stat=sum
19 group=series groupdisplay=cluster
20 outlineattrs=(color=black) nostatlabel;
21 yaxis
22 values=(0 to 10 BY 2)
23 labelattrs=(size=16pt weight=bold color=gray33)
24 valueattrs=(size=16pt weight=bold color=gray33)
25 offsetmax=0 grid minor minorcount=1;
26 xaxis
27 labelattrs=(size=16pt weight=bold color=gray33)
28 valueattrs=(size=16pt weight=bold color=gray33)
29 labelpos=right;
30RUN;
31 
32QUIT;
33ODS HTML CLOSE;
34ODS 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.