How to Create Scatter Plots in SAS (With Examples)

Creating scatter plots in SAS is a simple process that can be done in a few steps. First, you need to enter the data into SAS and then create a scatter plot using the SGPLOT procedure. You can customize your scatter plot by changing the color, size, symbol, and other aspects of the plot. Additionally, you can add labels and a title for clarity. Finally, you can export the plot as an image file or save it as a SAS dataset for further analysis.


You can use the following methods to create scatter plots in SAS:

Method 1: Create One Scatter Plot

proc sgplot data=my_data;
    scatter x=var1 y=var2;
run;

Method 2: Create Scatter Plots by Group

proc sgplot data=my_data;
    scatter x=var1 y=var2 / group=var3;
run;

The following examples show how to use each method with the following dataset in SAS:

/*create dataset*/
data my_data;
    input team $ points rebounds;
    datalines;
A 29 8
A 23 6
A 20 6
A 21 9
A 33 14
A 35 11
A 31 10
B 21 9
B 14 5
B 15 7
B 11 10
B 12 6
B 10 8
B 15 10
;
run;

/*view dataset*/
proc print data=my_data;

Example 1: Create One Scatter Plot

The following code shows how to create a scatterplot for the points and rebounds variables:

proc sgplot data=my_data;
    scatter x=points y=rebounds;
run;

The x-axis displays the values for the points variable and the y-axis displays the values for the rebounds variable.

Note that we can also add a title to the plot and modify the appearance of the markers within the plot to make it more aesthetically pleasing:

title "Points vs. Rebounds";
proc sgplot data=my_data;
    scatter x=points y=rebounds /
    markerattrs=(symbol=CircleFilled size=12 color=purple);
run;

Example 2: Create Scatter Plots by Group

title "Points vs. Rebounds by Team";
proc sgplot data=my_data;
    scatter x=points y=rebounds /
    markerattrs=(symbol=CircleFilled size=12)
    group=team;
run;

This plot allows us to quickly visualize the relationship between points and rebounds for both team A and team B.

The following tutorials explain how to create other charts in SAS:

How to Create Histograms in SAS

x