Published on :
Reporting CREATION_INTERNE

SGPLOT Stacked Bar Chart Generation

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins by defining a macro variable 'name' to name the output files. It then creates a 'my_data' dataset using internal 'datalines'. ODS (Output Delivery System) statements are configured to generate HTML output containing the chart. PROC SGPLOT is used to create a stacked bar chart, visualizing the 'AMOUNT' variable grouped by 'SERIES' on the category axis. Custom titles and style attributes are applied to enhance presentation. Finally, ODS destinations are closed.
Data Analysis

Type : CREATION_INTERNE


The 'my_data' dataset is created directly within the SAS script using the 'datalines' method, meaning the data is embedded in the code and does not come from an external source or standard SAS libraries like SASHELP.

1 Code Block
Initial Configuration
Explanation :
This block initializes a macro variable `name` used to name the HTML output file and the image. The `filename odsout '.'` statement redirects ODS output to the current working directory.
Copied!
1%let name=col3;
2 
3filename odsout '.';
2 Code Block
DATA STEP Data
Explanation :
This DATA STEP creates an in-memory dataset named 'my_data'. It reads the data provided in the 'datalines' block, defining three variables: 'CATEGORY' (numeric), 'SERIES' (character, from position 3 to 11), and 'AMOUNT' (numeric).
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 Configuration and Titles
Explanation :
This block configures the ODS output system. It first closes the default LISTING destination, then opens an HTML destination, naming the file 'col3.htm' and applying an 'htmlblue' style. The `ods graphics` options define the image format (PNG), the image file name ('col3'), as well as its dimensions and attributes. `title` statements are used to add custom titles to the chart with specific styles.
Copied!
1ODS LISTING CLOSE;
2ODS HTML path=odsout body="&name..htm"
3 (title="SGplot Stacked 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 "Stacked Bar";
10title2 color=gray33 ls=0.5 h=17pt "Compares the contribution of each value";
11title3 color=gray33 ls=0.5 h=17pt "to a total across categories";
4 Code Block
PROC SGPLOT
Explanation :
This `SGPLOT` procedure generates a stacked bar chart from the 'my_data' dataset. It disables the automatic legend and adds padding. Bar colors are defined via `styleattrs`. The `vbar` statement creates vertical bars, stacking the 'AMOUNT' ('response') variable by 'SERIES' ('group') and calculating the sum ('stat=sum'). Y and X axes are customized with specific labels, values, and styles, including a grid for the Y-axis.
Copied!
1PROC SGPLOT DATA=my_data noautolegend pad=(left=10% right=15%);
2styleattrs datacolors=(cx9999ff cx993366);
3vbar category / response=amount stat=sum
4 group=series barwidth=.6
5 outlineattrs=(color=black) nostatlabel;
6yaxis
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=3;
11xaxis
12 labelattrs=(size=16pt weight=bold color=gray33)
13 valueattrs=(size=16pt weight=bold color=gray33)
14 display=(noticks);
15RUN;
5 Code Block
ODS Closure
Explanation :
This final block closes the current procedure (`quit`), then explicitly closes the ODS HTML destination and reactivates the LISTING destination, thereby restoring the default ODS environment.
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.