Published on :
Graphical Reporting CREATION_INTERNE

100% Stacked Bar Chart (3D) with SGPLOT

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins by defining a macro variable 'name' and an 'odsout' fileref. It then creates a dataset named 'my_data' from inline data (datalines). A PROC SQL is used to add a 'bartotal' column to the dataset, representing the sum of 'amount' by 'category'. A second DATA STEP calculates 'catpct' (percentage of each 'amount' relative to 'bartotal') and formats this new variable as a percentage. Finally, it configures ODS options for HTML output, sets the chart titles, and uses PROC SGPLOT to create a stacked vertical bar chart. Key SGPLOT options include 'response=catpct', 'group=series', and 'dataskin=sheen' for the 3D effect.
Data Analysis

Type : CREATION_INTERNE


The 'my_data' dataset is entirely created at the beginning of the script using the DATALINES method. No external data or SASHELP library is used as an initial source.

1 Code Block
MACRO/GLOBAL
Explanation :
Defines the macro variable 'name' as 'col6', which will be used to name the HTML output file and the image. 'filename odsout '.' ' directs the ODS output to the current working directory.
Copied!
1%let name=col6;
2filename odsout '.';
2 Code Block
DATA STEP Data
Explanation :
Creates the 'my_data' dataset with variables CATEGORY, SERIES (character), and AMOUNT (numeric) by reading data provided via the DATALINES statement.
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
Explanation :
Modifies the 'my_data' dataset by adding a new 'bartotal' column. This column contains the sum of 'amount' for each 'category', calculated via SQL aggregation.
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
Explanation :
A second DATA STEP that reads the 'my_data' dataset, calculates the 'catpct' percentage of 'amount' relative to 'bartotal' for each observation, and applies a percentage format to this new variable.
Copied!
1DATA my_data; SET my_data;
2FORMAT catpct percent6.0;
3catpct=amount/bartotal;
4RUN;
5 Code Block
ODS/OPTIONS
Explanation :
Closes the default LISTING output. Opens an ODS HTML destination, specifying the output path, the file name ('col6.htm'), a title for the HTML page, and an 'htmlblue' style. It then configures ODS graphics options to generate PNG images with a specific size, no border, and an image map. Finally, it defines two titles for the graph with color, size, and style properties.
Copied!
1ODS LISTING CLOSE;
2ODS HTML path=odsout body="&name..htm"
3 (title="SGplot 100% Stacked 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 Bar";
10title2 color=gray33 ls=0.5 h=17pt "With 3D Shading";
6 Code Block
PROC SGPLOT
Explanation :
Uses PROC SGPLOT to create a stacked bar chart. Bars are defined by 'category' and stacked by 'series', with 'catpct' as the response variable. The 'dataskin=sheen' option adds a 3D effect. The style, colors, labels, and axis attributes are configured to enhance the chart's appearance. After execution, the ODS HTML destination is closed, and LISTING output is reopened.
Copied!
1PROC SGPLOT DATA=my_data noautolegend pad=(left=10% right=15%);
2label catpct='PERCENT';
3styleattrs datacolors=(cx9999ff cx993366 cxffffcc);
4vbar category / response=catpct stat=sum
5 group=series barwidth=.6
6 dataskin=sheen /* <--- basically, added this line! */
7 outlineattrs=(color=black) nostatlabel;
8yaxis
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;
13xaxis
14 labelattrs=(size=16pt weight=bold color=gray33)
15 valueattrs=(size=16pt weight=bold color=gray33)
16 display=(noticks);
17RUN;
18 
19QUIT;
20ODS HTML CLOSE;
21ODS 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.