Published on :
Reporting INTERNAL_CREATION

Generating Stacked Area Plot with SGPLOT

This code is also available in: Deutsch Español Français
Awaiting validation
This script illustrates the creation of a 'Stacked Area Plot' using the SGPLOT procedure. It first creates an internal dataset containing coordinates and calculated bases for stacking. Then, it configures the ODS output to generate an HTML file and a PNG image. The plot uses the BAND statement to draw the areas between the calculated variables.
Data Analysis

Type : INTERNAL_CREATION


Data is explicitly defined in the 'my_data' Data Step using the DATALINES statement.

1 Code Block
DATA STEP Data
Explanation :
Definition of the output name and creation of the 'my_data' dataset with calculation of 'base1', 'base2', and 'y2_stacked' variables required for graphically stacking the areas.
Copied!
1%let name=area2;
2 
3filename odsout '.';
4 
5DATA my_data;
6INPUT x y1 y2;
7base1=0;
8base2=y1;
9y2_stacked=y2+y1;
10DATALINES;
110 2.0 1.0
121 1.0 1.2
133 2.0 1.7
144 1.0 2.0
155 0.5 2.5
16;
17RUN;
2 Code Block
PROC SGPLOT
Explanation :
Configuration of ODS output (HTML and PNG), title definition, and execution of the SGPLOT procedure. The BAND statement is used twice to draw the stacked layers, and axes are customized.
Copied!
1ODS LISTING CLOSE;
2ODS HTML path=odsout body="&name..htm"
3 (title="SGplot Stacked Area Plot")
4 style=htmlblue;
5 
6ods graphics / imagefmt=png imagename="&name"
7 width=800px height=600px noborder;
8 
9title1 color=gray33 ls=0.0 h=23pt "Stacked Area Plot";
10 
11PROC SGPLOT DATA=my_data noautolegend;
12styleattrs datacolors=(cx993366 cx9999ff);
13band x=x lower=base1 upper=y1;
14band x=x lower=base2 upper=y2_stacked;
15yaxis
16 values=(0 to 4 BY 1) label='Y Axis'
17 labelattrs=(size=16pt weight=bold color=gray33)
18 valueattrs=(size=16pt weight=bold color=gray33)
19 offsetmin=0 offsetmax=0 grid;
20xaxis
21 values=(0 to 5 BY 1) label='X Axis'
22 labelattrs=(size=16pt weight=bold color=gray33)
23 valueattrs=(size=16pt weight=bold color=gray33)
24 offsetmin=0 offsetmax=0 grid;
25RUN;
26 
27QUIT;
28ODS HTML CLOSE;
29ODS 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.