College of Natural Sciences
 
FAQs
This is for IE7 to hold div open

SAS FAQ #28: Plotting a regression line using SAS/GRAPH

Question:

Is there a way I can generate a scatterplot with a regression line and 95% confidence intervals superimposed using SAS/GRAPH?

Answer:

Yes. The following SAS code demonstrates how to do this. It consists of three steps. Step 1 is a SAS DATA step which creates a set of demonstration data we use to illustrate the SAS/GRAPH technique for fitting a regression line. The data consist of a single X predictor and a single Y outcome (dependent) variable.

Step 2 is a PROC REG which we use to check the accuracy of the sample data we generated in Step 1. The PROC REG is not actually necessary for the plot itself, but it is a useful error-catching mechanism.

Step 3 begins with the GOPTIONS line. Here we define a SAS/GRAPH symbol which provides both the regression line, as well setting the color markers for the points of the scatterplot.


* Begin sample SAS/GRAPH program ;



* Step 1 ; 

DATA demo ;

DO i=1 TO 1000 ;

   x=RANNOR(0) ;

   y=(.36 * x) + ((1 - .36**2)**.5)*RANNOR(0) ;

 * This generates two variables with y as a function of x as described above;

   OUTPUT ;

END ;



* Step 2 ; 

PROC REG DATA = demo ;

   MODEL  y = x / STB ;

* PROC REG is actually not necessary for the plot, but it is nice to see
that the data creation process worked correctly;



* Step 3 ; 

GOPTIONS RESET=GLOBAL ;

SYMBOL INTERPOL=RLCLM95 VALUE=star CI=blue CO=green CV=red;

*This symbol definition will allow us to produce a scatter-plot of observed

values as red stars, with a blue regression line of best fit with green 95%

confidence intervals around the line;



PROC GPLOT DATA=demo;

   PLOT y*x ;

RUN ; 



*End sample SAS/GRAPH program; 

If you have further questions, send E-mail to stats@ssc.utexas.edu.