The script begins by defining a macro variable 'name' to name the output files. It then creates a 'my_data' dataset using internal 'datalines'. ODS (Output Delivery System) statements are configured to generate HTML output containing the chart. PROC SGPLOT is used to create a stacked bar chart, visualizing the 'AMOUNT' variable grouped by 'SERIES' on the category axis. Custom titles and style attributes are applied to enhance presentation. Finally, ODS destinations are closed.
Data Analysis
Type : CREATION_INTERNE
The 'my_data' dataset is created directly within the SAS script using the 'datalines' method, meaning the data is embedded in the code and does not come from an external source or standard SAS libraries like SASHELP.
1 Code Block
Initial Configuration
Explanation : This block initializes a macro variable `name` used to name the HTML output file and the image. The `filename odsout '.'` statement redirects ODS output to the current working directory.
Copied!
%let name=col3;
filename odsout '.';
1
%let name=col3;
2
3
filename odsout '.';
2 Code Block
DATA STEP Data
Explanation : This DATA STEP creates an in-memory dataset named 'my_data'. It reads the data provided in the 'datalines' block, defining three variables: 'CATEGORY' (numeric), 'SERIES' (character, from position 3 to 11), and 'AMOUNT' (numeric).
Copied!
data my_data;
input CATEGORY SERIES $ 3-11 AMOUNT;
datalines;
1 Series A 5
2 Series A 6.8
3 Series A 9.2
1 Series B 6.5
2 Series B 6.9
3 Series B 5.6
;
run;
1
DATA my_data;
2
INPUT CATEGORY SERIES $ 3-11 AMOUNT;
3
DATALINES;
4
1 Series A 5
5
2 Series A 6.8
6
3 Series A 9.2
7
1 Series B 6.5
8
2 Series B 6.9
9
3 Series B 5.6
10
;
11
RUN;
3 Code Block
ODS Configuration and Titles
Explanation : This block configures the ODS output system. It first closes the default LISTING destination, then opens an HTML destination, naming the file 'col3.htm' and applying an 'htmlblue' style. The `ods graphics` options define the image format (PNG), the image file name ('col3'), as well as its dimensions and attributes. `title` statements are used to add custom titles to the chart with specific styles.
Copied!
ODS LISTING CLOSE;
ODS HTML path=odsout body="&name..htm"
(title="SGplot Stacked Bar")
style=htmlblue;
ods graphics / imagefmt=png imagename="&name"
width=800px height=600px noborder imagemap;
title1 color=gray33 ls=0.5 h=23pt "Stacked Bar";
title2 color=gray33 ls=0.5 h=17pt "Compares the contribution of each value";
title3 color=gray33 ls=0.5 h=17pt "to a total across categories";
1
ODS LISTING CLOSE;
2
ODS HTML path=odsout body="&name..htm"
3
(title="SGplot Stacked Bar")
4
style=htmlblue;
5
6
ods graphics / imagefmt=png imagename="&name"
7
width=800px height=600px noborder imagemap;
8
9
title1 color=gray33 ls=0.5 h=23pt "Stacked Bar";
10
title2 color=gray33 ls=0.5 h=17pt "Compares the contribution of each value";
11
title3 color=gray33 ls=0.5 h=17pt "to a total across categories";
4 Code Block
PROC SGPLOT
Explanation : This `SGPLOT` procedure generates a stacked bar chart from the 'my_data' dataset. It disables the automatic legend and adds padding. Bar colors are defined via `styleattrs`. The `vbar` statement creates vertical bars, stacking the 'AMOUNT' ('response') variable by 'SERIES' ('group') and calculating the sum ('stat=sum'). Y and X axes are customized with specific labels, values, and styles, including a grid for the Y-axis.
Explanation : This final block closes the current procedure (`quit`), then explicitly closes the ODS HTML destination and reactivates the LISTING destination, thereby restoring the default ODS environment.
Copied!
quit;
ODS HTML CLOSE;
ODS LISTING;
1
QUIT;
2
ODS HTML CLOSE;
3
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.