How to Create a Scatterplot with Regression Line in SAS

Creating a scatterplot with a regression line in SAS is a straightforward process. Begin by using the scatter statement within a proc sgplot procedure to generate the scatterplot of the response and predictor variables. Then use the add option to specify the regression line associated with the scatterplot. Finally, use the legend statement to label the regression line, and the title statement to label the scatterplot. This will allow you to visualize the relationship between the variables as well as the linear regression line associated with the data.


You can use proc sgplot to quickly create a scatterplot with a regression line in SAS.

The following examples show how to use this procedure in practice.

Example 1: Create Basic Scatterplot with Regression Line

The following code shows how to create a basic scatterplot with a regression line using the built-in SAS :

/*create scatterplot with regression line*/
proc sgplot data=sashelp.class;
   reg y=height x=weight;
run;

scatterplot with regression line in SAS

The points in the plot display the individual from the dataset and the blue line displays the fitted regression line.

Example 2: Create Custom Scatterplot with Regression Line

Note that proc sgplot can create highly customizable scatterplots.

For example, you can:

  • Add a title to the chart
  • Modify the axis labels
  • Remove the legend
  • Customize the color and thickness of the regression line
  • Customize the appearance of the points in the plot

The following code shows how to customize each of these aspects of the plot:

/*create custom scatterplot with regression line*/
proc sgplot data=sashelp.class noautolegend;
   title 'Regression Model';
   xaxis label='Weight (pounds)';
   yaxis label='Height (inches)';
   reg y=height x=weight /
   lineattrs=(color=red thickness=2)
   markerattrs=(color=green size=12px symbol=circlefilled);
run;

Notice that the title, axis labels, individual points, and the regression line have all been modified.

The following tutorials explain how to perform other common tasks in SAS:

x