Published on :
Graph CREATION_INTERNE

100% Stacked Area Plot

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins by defining numerical data 'x', 'y1', and 'y2' via datalines. It then calculates the sum 'x_total' and percentages 'y1_pct' and 'y2_pct' for each row, which will serve as the lower and upper bounds for the plot's bands. The output is directed to an HTML file, and a PNG image is generated. PROC SGPLOT is used with two BAND statements to create the stacked area plot, where the X and Y axes are configured to display specific values and labels, ensuring a clear visualization of proportions.
Data Analysis

Type : CREATION_INTERNE


The 'my_data' dataset is created directly within the script using the DATALINES clause. It does not originate from an external source or a standard SAS library like SASHELP.

1 Code Block
MACRO VAR
Explanation :
Defines a macro variable `name` used to name the output HTML file and the image.
Copied!
1%let name=area3;
2 Code Block
FILEREF
Explanation :
Assigns the fileref `odsout` to the current working directory, where ODS files will be saved.
Copied!
1filename odsout '.';
3 Code Block
DATA STEP Data
Explanation :
Creates the `my_data` dataset with variables `x`, `y1`, `y2`. A new variable `x_total` is calculated as the sum of `y1` and `y2`. Data is provided directly in the script via `datalines`.
Copied!
1DATA my_data;
2INPUT x y1 y2;
3x_total=y1+y2;
4DATALINES;
50 2.0 1.0
61 1.0 1.2
73 2.0 1.7
84 1.0 2.0
95 0.5 2.5
10;
11RUN;
4 Code Block
DATA STEP Data
Explanation :
Modifies the existing `my_data` dataset by calculating percentage variables necessary for the stacked area plot. `y1_pct` and `y2_pct` represent the relative proportions of `y1` and `y2`, and `base1_pct` and `base2_pct` define the starting points for the plot bands.
Copied!
1DATA my_data; SET my_data;
2base1_pct=0; y1_pct=y1/x_total;
3base2_pct=y1_pct; y2_pct=y1_pct+(y2/x_total);
4RUN;
5 Code Block
ODS
Explanation :
Closes the default ODS LISTING destination and opens an ODS HTML destination. The output HTML file is dynamically named using the macro variable `&name` and includes a specified title and `htmlblue` style.
Copied!
1ODS LISTING CLOSE;
2ODS HTML path=odsout body="&name..htm"
3 (title="SGplot 100% Stacked Area Plot")
4 style=htmlblue;
6 Code Block
ODS GRAPHICS
Explanation :
Configures options for ODS graphical output. Specifies PNG format for the image, names the image file, and sets the width, height, and absence of border.
Copied!
1ods graphics / imagefmt=png imagename="&name"
2width=800px height=600px noborder;
3 
7 Code Block
TITLE
Explanation :
Sets the main title of the plot, specifying color, line spacing, and font height.
Copied!
1title1 color=gray33 ls=0.0 h=23pt "100% Stacked Area Plot";
2 
8 Code Block
PROC SGPLOT
Explanation :
Generates the stacked area plot using PROC SGPLOT. It uses `my_data` and disables the automatic legend. Two `BAND` statements are used to create the stacked areas, based on the calculated percentage variables. The Y and X axes are customized with value ranges, labels, font attributes, and grids.
Copied!
1PROC SGPLOT DATA=my_data noautolegend;
2styleattrs datacolors=(cx993366 cx9999ff);
3band x=x lower=base1_pct upper=y1_pct;
4band x=x lower=base2_pct upper=y2_pct;
5yaxis
6 values=(0 to 1 BY .2) label='Y Axis'
7 labelattrs=(size=16pt weight=bold color=gray33)
8 valueattrs=(size=16pt weight=bold color=gray33)
9 offsetmin=0 offsetmax=0 grid;
10xaxis
11 values=(0 to 5 BY 1) label='X Axis'
12 labelattrs=(size=16pt weight=bold color=gray33)
13 valueattrs=(size=16pt weight=bold color=gray33)
14 offsetmin=0 offsetmax=0 grid;
15RUN;
16 
17QUIT;
9 Code Block
ODS
Explanation :
Closes the previously opened ODS HTML destination and reactivates the default ODS LISTING destination.
Copied!
1ODS HTML CLOSE;
2ODS 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.