The script initializes a macro-variable and a fileref for ODS output. It then creates the `my_data` dataset from inline data (`datalines`). A `PROC SQL` step is used to calculate the total sum (`bartotal`) of `AMOUNT` for each `CATEGORY`. A second `DATA STEP` calculates the percentage (`catpct`) of each `AMOUNT` relative to its corresponding `bartotal` and applies a format. ODS HTML and ODS Graphics destinations are configured to generate an HTML file with the chart. Finally, `PROC SGPLOT` is used to create a 100% stacked horizontal bar chart, styled with custom colors, a 3D effect ('sheen'), and specific axis attributes. The output is generated in an HTML file and ODS destinations are closed.
Data Analysis
Type : CREATION_INTERNE
The source data `my_data` is created directly within the SAS script using the `datalines` statement, allowing autonomous execution without external dependencies.
1 Code Block
Global Declarations
Explanation : Defines a macro variable `name` for the filename and a fileref `odsout` pointing to the current directory for ODS outputs.
Copied!
%let name=bar6;
/*
Set your current-working-directory (to read/write files), if you need to ...
%let rc=%sysfunc(dlgcdir('c:\someplace\public_html'));
*/
filename odsout '.';
1
%let name=bar6;
2
3
/*
4
Set your current-working-directory (to read/write files), if you need to ...
Explanation : Creates a SAS dataset named `my_data` using inline data provided via the `datalines` statement, with variables `CATEGORY`, `SERIES` (character), 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 Data
Explanation : Uses `PROC SQL` to modify the `my_data` dataset. It recalculates `my_data` by adding a new column `bartotal`, which is the sum of `AMOUNT` for each `CATEGORY`. The `GROUP BY` clause ensures `bartotal` is calculated per category.
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 Data
Explanation : A second `DATA STEP` reads the `my_data` dataset (modified by `PROC SQL`) and calculates a new variable `catpct` representing the percentage of `AMOUNT` relative to `bartotal`. A `percent6.0` format is applied to `catpct`.
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 HTML
Explanation : Closes the ODS LISTING destination. Opens the ODS HTML destination, specifies the HTML output file (`bar6.htm`), the page title, and the `htmlblue` style. Configures `ODS GRAPHICS` for PNG image export with specific dimensions, and sets two titles for the chart.
title2 color=gray33 ls=0.5 h=17pt "With 3D Shading";
6 Code Block
PROC SGPLOT
Explanation : Generates a 100% stacked horizontal bar chart using `PROC SGPLOT`. The `my_data` dataset is used. The `hbar` option creates the bars, `response=catpct` indicates the variable to represent, `stat=sum` aggregates the values, and `group=series` stacks the bars by `SERIES`. `dataskin=sheen` applies a 3D visual effect. The X and Y axes are configured with custom style and display attributes.
dataskin=sheen /* <--- basically, added this line! */
7
outlineattrs=(color=black) nostatlabel;
8
xaxis
9
values=(0 to 1BY .2)
10
labelattrs=(size=16pt weight=bold color=gray33)
11
valueattrs=(size=16pt weight=bold color=gray33)
12
offsetmax=0 grid minor minorcount=1;
13
yaxis
14
labelattrs=(size=16pt weight=bold color=gray33)
15
valueattrs=(size=16pt weight=bold color=gray33)
16
display=(noticks);
17
RUN;
7 Code Block
ODS HTML
Explanation : Terminates procedure execution (`quit`). Closes the ODS HTML destination and 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.