The script begins by defining a macro variable and a fileref. It then creates a dataset named 'my_data' from DATALINES statements. A PROC SQL step is used to enrich 'my_data' by calculating the total amounts per category ('bartotal'). A second DATA STEP calculates the percentage of each amount relative to its 'bartotal' ('catpct') and formats this new variable. Finally, the script configures ODS destinations (HTML and PNG graphic) and uses PROC SGPLOT to generate a 100% stacked bar chart, illustrating these percentages with aesthetic customizations for axes and colors.
Data Analysis
Type : CREATION_INTERNE
The main dataset 'my_data' is created directly in the script via a DATALINES statement, making it an internal data source to the code.
1 Code Block
Macro Variable / Fileref
Explanation : Defines the macro variable 'name' used for the output filename and the 'odsout' fileref pointing to the current directory for ODS output.
Copied!
%let name=col5;
filename odsout '.';
1
%let name=col5;
2
filename odsout '.';
2 Code Block
DATA STEP Data
Explanation : Creates the SAS dataset 'my_data' by reading raw data provided directly in the script ('datalines') with the variables CATEGORY, SERIES, and AMOUNT.
Copied!
data my_data;
input CATEGORY SERIES $ 3-11 AMOUNT;
datalines;
1 Series A 5.0
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
1 Series C 2.3
2 Series C 3.1
3 Series C 2.3
;
run;
1
DATA my_data;
2
INPUT CATEGORY SERIES $ 3-11 AMOUNT;
3
DATALINES;
4
1 Series A 5.0
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
1 Series C 2.3
11
2 Series C 3.1
12
3 Series C 2.3
13
;
14
RUN;
3 Code Block
PROC SQL
Explanation : Modifies the 'my_data' dataset by adding a new column 'bartotal', which represents the sum of 'AMOUNT' for each 'CATEGORY' using an SQL query.
Copied!
proc sql;
create table my_data as
select *, sum(amount) as bartotal
from my_data
group by category;
quit; run;
1
PROC SQL;
2
create TABLE my_data as
3
select *, sum(amount) as bartotal
4
from my_data
5
group BY category;
6
QUIT; RUN;
4 Code Block
DATA STEP
Explanation : Modifies the 'my_data' dataset by calculating 'catpct', the percentage of 'AMOUNT' relative to 'bartotal', and applies a percentage format to this new variable.
Copied!
data my_data; set my_data;
format catpct percent6.0;
catpct=amount/bartotal;
run;
1
DATA my_data; SET my_data;
2
FORMAT catpct percent6.0;
3
catpct=amount/bartotal;
4
RUN;
5 Code Block
ODS
Explanation : Closes the default ODS LISTING destination. Opens the ODS HTML destination, specifying the output path, HTML filename, and style. Configures ODS GRAPHICS options for the generated PNG image. Defines the main chart titles with style and color options.
Copied!
ODS LISTING CLOSE;
ODS HTML path=odsout body="&name..htm"
(title="SGplot 100% Stacked Bar")
style=htmlblue;
ods graphics / imagefmt=png imagename="&name"
width=800px height=600px noborder imagemap;
title1 color=gray33 ls=0.5 h=23pt "100% Stacked Bar";
title2 color=gray33 ls=0.5 h=17pt "Compares the percent each value";
title3 color=gray33 ls=0.5 h=17pt "to a total across categories";
title2 color=gray33 ls=0.5 h=17pt "Compares the percent each value";
11
title3 color=gray33 ls=0.5 h=17pt "to a total across categories";
6 Code Block
PROC SGPLOT
Explanation : Generates a 100% stacked bar chart using the 'my_data' dataset. Bars represent 'CATEGORY', height is based on 'catpct', and stacks are defined by 'SERIES'. The code customizes legends, data colors, bar attributes (width, outline), and the appearance of the X and Y axes (labels, values, grid).
Explanation : Ends active procedures. Closes the ODS HTML destination, saving the associated HTML file and images. Then reactivates the ODS LISTING destination.
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.