Published on :
Reporting CREATION_INTERNE

3D Stacked Bar Chart with PROC SGPLOT

This code is also available in: Deutsch Español Français
Awaiting validation
The script starts by defining a macro variable for the output file name and an ODS fileref. It then creates an internal dataset, `my_data`, using `datalines`. The main part of the script configures the ODS environment for generating an HTML file containing the chart. `PROC SGPLOT` is used to create a stacked bar chart, with specific styling and formatting options for axes and bar colors, including a sheen effect (dataskin=sheen) for a 3D rendering. The chart is saved in PNG format within the HTML file.
Data Analysis

Type : CREATION_INTERNE


The 'my_data' dataset is created directly within the script using a DATALINES statement.

1 Code Block
GLOBAL DECLARATION
Explanation :
Defines a macro variable `name` to name the ODS output file and assigns the current working directory to the `odsout` fileref for ODS output.
Copied!
1%let name=col4;
2filename odsout '.';
2 Code Block
DATA STEP Data
Explanation :
Creates the `my_data` dataset using the `datalines` statement to provide data directly within the script. This dataset will be the source for the chart.
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
PROC SGPLOT
Explanation :
This section configures the ODS system to generate HTML output. It closes the default LISTING output and opens the HTML output, specifying the path and file name. `ODS GRAPHICS` options are used to define the image format (PNG), the name, and the dimensions of the graph. Two titles are defined for the graph. `PROC SGPLOT` is then called to create a stacked bar chart from the `my_data` dataset. Options include suppressing the automatic legend, padding, defining data colors, and most importantly `dataskin=sheen` for a 3D effect. The Y and X axes are configured with specific style labels and values. Finally, ODS HTML is closed, and ODS LISTING is reopened.
Copied!
1ODS LISTING CLOSE;
2ODS HTML path=odsout body="&name..htm"
3 (title="SGplot 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 "Stacked Bar";
10title2 color=gray33 ls=0.5 h=17pt "With 3D Shading";
11 
12PROC SGPLOT DATA=my_data noautolegend pad=(left=10% right=15%);
13styleattrs datacolors=(cx9999ff cx993366);
14vbar category / response=amount stat=sum
15 group=series barwidth=.6
16 dataskin=sheen /* <--- basically, added this line! */
17 outlineattrs=(color=black) nostatlabel;
18yaxis
19 values=(0 to 16 BY 4)
20 labelattrs=(size=16pt weight=bold color=gray33)
21 valueattrs=(size=16pt weight=bold color=gray33)
22 offsetmax=0 grid minor minorcount=3;
23xaxis
24 labelattrs=(size=16pt weight=bold color=gray33)
25 valueattrs=(size=16pt weight=bold color=gray33)
26 display=(noticks);
27RUN;
28 
29QUIT;
30ODS HTML CLOSE;
31ODS 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.