The script begins by creating a 'my_data' dataset with information on categories, series, and amounts. Then, it uses PROC SQL to calculate the total sum of amounts for each category ('bartotal'). A second DATA STEP is used to calculate the percentage of each amount relative to its category's total ('catpct') and apply a percentage format. Finally, the ODS output is configured to generate an HTML file and a PNG image. PROC SGPLOT is used to create the 100% stacked horizontal bar chart, using 'category' on the Y-axis, 'catpct' as the response, and 'series' for grouping the bars. Titles and style attributes are applied to enhance the presentation.
Data Analysis
Type : INTERNAL_CREATION
Initial data is created directly within the script via a DATALINES block in a DATA STEP. It is then transformed and enriched by subsequent PROC SQL and DATA STEP steps.
1 Code Block
DATA STEP Data
Explanation : This DATA STEP block creates a dataset named 'my_data'. It uses the 'input' statement to define three variables (CATEGORY, SERIES, AMOUNT) and the 'datalines' block to directly read data into the dataset.
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;
2 Code Block
PROC SQL Data
Explanation : This SQL procedure modifies the 'my_data' dataset. It calculates the sum of 'amount' for each 'category', stores this total in a new 'bartotal' column, and recreates the 'my_data' dataset with this new column. This is an essential aggregation step for percentage calculations.
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;
3 Code Block
DATA STEP
Explanation : This DATA STEP updates the existing 'my_data' dataset. It calculates the percentage ('catpct') of each 'amount' relative to the corresponding 'bartotal'. A 'percent6.0' format is applied to the new 'catpct' variable for percentage display.
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;
4 Code Block
PROC SGPLOT
Explanation : This block handles output and visualization. ODS (Output Delivery System) statements are used to generate HTML output and a PNG image of the graph. PROC SGPLOT is then called to create a 100% stacked horizontal bar chart. It uses 'category' on the Y-axis, 'catpct' as the response variable, and 'series' to group the bars, thus displaying the proportional contribution of each series within each category. Style options, titles, and axes are configured to customize the graph's appearance.
Copied!
ODS LISTING CLOSE;
ODS HTML path=odsout body="&name..htm"
(title="SGplot 100% Stacked Horizontal 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 Horizontal 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";
proc sgplot data=my_data noautolegend pad=(left=10% right=15%);
label catpct='PERCENT';
styleattrs datacolors=(cx9999ff cx993366 cxffffcc);
hbar category / response=catpct stat=sum
group=series /*barwidth=.6*/
outlineattrs=(color=black) nostatlabel;
xaxis
values=(0 to 1 by .2)
labelattrs=(size=16pt weight=bold color=gray33)
valueattrs=(size=16pt weight=bold color=gray33)
offsetmax=0 grid minor minorcount=1;
yaxis
labelattrs=(size=16pt weight=bold color=gray33)
valueattrs=(size=16pt weight=bold color=gray33)
display=(noticks);
run;
quit;
ODS HTML CLOSE;
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.