Published on :
Chart CREATION_INTERNE

3D Horizontal Stacked Bar Chart

This code is also available in: Deutsch Español Français
Awaiting validation
The script initializes a 'my_data' dataset from inline data (datalines). It then configures the ODS destination system to generate HTML output and a PNG graphic. A PROC SGPLOT is used to create a horizontal stacked bar chart. The bars are styled with a 3D effect ('dataskin=sheen') and axes are customized for better readability. The chart is saved to an HTML file and a PNG image, with specific titles.
Data Analysis

Type : CREATION_INTERNE


The 'my_data' dataset is created directly within the script using a DATA step and the DATALINES statement. It contains variables for category, series, and amount.

1 Code Block
Macro Variable / FILENAME
Explanation :
Defines a macro variable `name` to name the output files (HTML, PNG) and assigns an `odsout` fileref to the current working directory, allowing output files to be written there.
Copied!
1%let name=bar4;
2 
3filename odsout '.';
2 Code Block
DATA STEP Data
Explanation :
Creates the SAS dataset named 'my_data' using a DATA step and the DATALINES statement. The inline data is read into three variables: 'CATEGORY' (numeric), 'SERIES' (character), 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
Explanation :
Configures the ODS (Output Delivery System) destination system to generate HTML output and graphics. The ODS LISTING destination is closed, and ODS HTML is activated to create an HTML file ('bar4.htm') with a title and 'htmlblue' style. ODS GRAPHICS is configured to produce a PNG image ('bar4.png') of 800x600 pixels without a border. Two titles are defined for the chart with specific styles and colors.
Copied!
1ODS LISTING CLOSE;
2ODS HTML path=odsout body="&name..htm"
3 (title="SGplot Horizontal 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 "Horizontal Stacked Bar";
10title2 color=gray33 ls=0.5 h=17pt "With 3D Shading";
4 Code Block
PROC SGPLOT
Explanation :
Executes PROC SGPLOT to create a horizontal stacked bar chart from the 'my_data' dataset. Style attributes define data colors. The `hbar` statement creates the bars, using 'CATEGORY' for the Y-axis, 'AMOUNT' for the X-axis (with sum as statistic), and 'SERIES' to group the bars. The 'dataskin=sheen' option applies a 3D effect. X and Y axes are customized in terms of values, labels, text attributes, and display.
Copied!
1PROC SGPLOT DATA=my_data noautolegend;
2styleattrs datacolors=(cx9999ff cx993366);
3hbar category / response=amount stat=sum
4 group=series /*groupdisplay=cluster grouporder=descending*/
5 dataskin=sheen /* <--- basically, added this line! */
6 outlineattrs=(color=black) nostatlabel;
7xaxis
8 values=(0 to 16 BY 4)
9 labelattrs=(size=16pt weight=bold color=gray33)
10 valueattrs=(size=16pt weight=bold color=gray33)
11 offsetmax=0 grid minor minorcount=1;
12yaxis
13 labelattrs=(size=16pt weight=bold color=gray33)
14 valueattrs=(size=16pt weight=bold color=gray33)
15 display=(noticks);
16RUN;
5 Code Block
ODS Cleanup
Explanation :
Ends the current SGPLOT procedure, then closes the ODS HTML destination, and finally reactivates the ODS LISTING destination to return to the default SAS output state.
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.