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!
%let name=area3;
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!
filename odsout '.';
1
filename 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`.
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!
data my_data; set my_data;
base1_pct=0; y1_pct=y1/x_total;
base2_pct=y1_pct; y2_pct=y1_pct+(y2/x_total);
run;
1
DATA my_data; SET my_data;
2
base1_pct=0; y1_pct=y1/x_total;
3
base2_pct=y1_pct; y2_pct=y1_pct+(y2/x_total);
4
RUN;
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!
ODS LISTING CLOSE;
ODS HTML path=odsout body="&name..htm"
(title="SGplot 100% Stacked Area Plot")
style=htmlblue;
1
ODS LISTING CLOSE;
2
ODS 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.
Explanation : Sets the main title of the plot, specifying color, line spacing, and font height.
Copied!
title1 color=gray33 ls=0.0 h=23pt "100% Stacked Area Plot";
1
title1 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!
proc sgplot data=my_data noautolegend;
styleattrs datacolors=(cx993366 cx9999ff);
band x=x lower=base1_pct upper=y1_pct;
band x=x lower=base2_pct upper=y2_pct;
yaxis
values=(0 to 1 by .2) label='Y Axis'
labelattrs=(size=16pt weight=bold color=gray33)
valueattrs=(size=16pt weight=bold color=gray33)
offsetmin=0 offsetmax=0 grid;
xaxis
values=(0 to 5 by 1) label='X Axis'
labelattrs=(size=16pt weight=bold color=gray33)
valueattrs=(size=16pt weight=bold color=gray33)
offsetmin=0 offsetmax=0 grid;
run;
quit;
1
PROC SGPLOTDATA=my_data noautolegend;
2
styleattrs datacolors=(cx993366 cx9999ff);
3
band x=x lower=base1_pct upper=y1_pct;
4
band x=x lower=base2_pct upper=y2_pct;
5
yaxis
6
values=(0 to 1BY .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;
10
xaxis
11
values=(0 to 5BY1) 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;
15
RUN;
16
17
QUIT;
9 Code Block
ODS
Explanation : Closes the previously opened ODS HTML destination and reactivates the default ODS LISTING destination.
Copied!
ODS HTML CLOSE;
ODS LISTING;
1
ODS HTML CLOSE;
2
ODS 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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.