Published on :
Reporting INTERNAL_CREATION

Bubble Plot Generation with SGPLOT

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins by defining a macro variable 'name' for naming output files. An 'odsout' fileref is then assigned to the current directory for ODS output management. A SAS© data table named 'my_data' is created using a DATA step and datalines, containing categorical and numerical variables necessary for the plot. The ODS LISTING output is temporarily closed, and the ODS HTML destination is activated, configuring the creation of an HTML file and a PNG image from the generated plot. A custom title is applied to the plot. The PROC SGPLOT procedure is invoked to create the bubble plot, specifying variables for the X and Y axes, bubble size (based on 'value'), and bubble grouping (based on 'series'). Color and line styles, as well as axis attributes, are defined to improve readability. Finally, the procedure is terminated by 'quit;', the ODS HTML destination is closed, and the ODS LISTING output is reactivated.
Data Analysis

Type : INTERNAL_CREATION


The data used for the plot is entirely generated within the script via a DATA step with 'datalines'. The resulting table is 'my_data'.

1 Code Block
Macro
Explanation :
Defines a macro variable 'name' with the value 'bub1'. This variable will be used later to name the output files (HTML, PNG).
Copied!
1%let name=bub1;
2 Code Block
Global Statement
Explanation :
Assignment of a fileref 'odsout' to the current working directory. This tells SAS where to save ODS-generated output files.
Copied!
1filename odsout '.';
3 Code Block
DATA STEP Data
Explanation :
Creates the SAS data table 'my_data'. The 'color' variable is defined with a length of 8 characters. The 'series', 'x', 'y', and 'value' variables are read from the data lines ('datalines') provided directly in the script.
Copied!
1DATA my_data;
2LENGTH color $ 8;
3INPUT series $ 1-1 x y value;
4DATALINES;
5A 1.0 1.0 .65
6A 2.0 0.9 0.3
7B 1.4 2.3 .65
8B 2.2 1.4 0.3
9;
10RUN;
4 Code Block
ODS Statements
Explanation :
Closes the default ODS LISTING destination. Then opens the ODS HTML destination, specifying the output path via the 'odsout' fileref, the HTML file name ('bub1.htm' thanks to the macro variable '&name'), a title for the HTML document, and an 'htmlblue' style. The 'ods graphics' options are configured to generate a PNG image ('bub1.png') of 800x600 pixels without a border and with an image map.
Copied!
1ODS LISTING CLOSE;
2ODS HTML path=odsout body="&name..htm"
3 (title="SGplot Bubble Plot")
4 style=htmlblue;
5 
6ods graphics / imagefmt=png imagename="&name"
7 width=800px height=600px noborder imagemap;
5 Code Block
Global Statement
Explanation :
Defines the first title of the plot. It is styled with 'gray33' color, a line spacing of 0.0, and a font height of 23 points, displaying the text 'Bubble Plot'.
Copied!
1title1 color=gray33 ls=0.0 h=23pt "Bubble Plot";
6 Code Block
PROC SGPLOT
Explanation :
Executes the SGPLOT procedure to create the bubble plot. It uses the 'my_data' table. Bubble colors are defined by 'styleattrs datacolors'. The 'bubble' statement specifies 'x' and 'y' for positions, 'value' for size, and 'series' for grouping, with additional options for maximum radius and line attributes. The X and Y axes are fully customized with value ranges, stylized labels, display values, and minor grids.
Copied!
1PROC SGPLOT DATA=my_data aspect=1 noautolegend;
2styleattrs datacolors=(cx9999ff cx993366);
3bubble x=x y=y size=value / group=series proportional
4 bradiusmax=70px lineattrs=(color=gray33);
5yaxis
6 values=(0 to 3 BY 1) 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 minor minorcount=1;
10xaxis
11 values=(0 to 3 BY 1) 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 minor minorcount=1;
15RUN;
7 Code Block
ODS Statements
Explanation :
Terminates the execution of the SGPLOT procedure with 'quit;'. Then closes the previously opened ODS HTML destination and reactivates the ODS LISTING destination, restoring SAS's default output behavior.
Copied!
1QUIT;
2ODS HTML CLOSE;
3ODS 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.