Published on :
Reporting CREATION_INTERNE

SGplot Horizontal Grouped Bar Chart (3D)

This code is also available in: Deutsch Español Français
Awaiting validation
The main objective of this script is to visualize categorical data using a grouped bar chart. It first prepares a 'my_data' dataset using a DATALINES statement. Then, it configures the ODS destination system to generate HTML output, including an SGPLOT graphic in PNG format. The chart displays the sum of an 'AMOUNT' variable grouped by 'SERIES' and categorized by 'CATEGORY'. Style options are applied for better visual presentation, including a 3D shading on the bars ('dataskin=sheen'). Chart titles and axis attributes are also customized.
Data Analysis

Type : CREATION_INTERNE


The 'my_data' dataset is created internally within the script via a DATA step and the DATALINES statement. It contains three variables: 'CATEGORY', 'SERIES', and 'AMOUNT'.

1 Code Block
DATA STEP Data
Explanation :
This block creates the 'my_data' dataset that will be used for the chart. Data is integrated directly into the script using the DATALINES statement, simulating data from various sources.
Copied!
1DATA my_data;
2INPUT CATEGORY SERIES $ 3-11 AMOUNT;
3DATALINES;
41 Series A 5
52 Series A 7.8
61 Series B 9.5
72 Series B 5.9
8;
9RUN;
2 Code Block
ODS Configuration
Explanation :
This block initializes the ODS (Output Delivery System) environment. It defines a macro variable 'name' to name the output files, closes the default ODS LISTING destination, and opens an ODS HTML destination to generate an HTML file. ODS GRAPHICS options are configured to produce a PNG image of the chart with specific dimensions, and titles are defined for the chart.
Copied!
1%let name=bar2;
2filename odsout '.';
3ODS LISTING CLOSE;
4ODS HTML path=odsout body="&name..htm"
5 (title="SGplot Horizontal Grouped Bar (3D)")
6 style=htmlblue;
7 
8ods graphics / imagefmt=png imagename="&name"
9 width=800px height=600px noborder imagemap;
10 
11title1 color=gray33 ls=0.5 h=23pt "Horizontal Grouped Bar";
12title2 color=gray33 ls=0.5 h=17pt "With 3D Shading";
3 Code Block
PROC SGPLOT
Explanation :
This procedure generates the horizontal grouped bar chart. It uses the 'my_data' dataset. The HBAR statement creates the bars, summing 'AMOUNT' and grouping them by 'SERIES' within each 'CATEGORY'. The 'dataskin=sheen' option applies a 3D effect to the bars. Style and axis attributes are customized to improve readability and aesthetics of the chart.
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 10 BY 2)
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;
4 Code Block
ODS Cleanup
Explanation :
This block properly closes the open ODS destinations, particularly the ODS HTML destination, and reactivates the ODS LISTING destination. The 'quit' statement terminates any currently running SAS procedure.
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.