BookmarkSubscribeRSS Feed
sun014
Calcite | Level 5

Hi I'm a bit of a newbie to SAS trying to create a scatter plot that would include the following:

  • a line of equality (i.e. a diagonal line showing points where X=Y)
  • data labels for individual observations

An early version of the chart is shown below? I found some example code showing how to do the diagonal line using proc gplot but was not able to figure out how to do the data labels. What would be the most effective way of doing this? Would I use proc sgcatter, proc sgplot or proc gplot? Would anyone be able to provide some example code showing how to do both using the same proc?

Thanks

Chart.bmp

10 REPLIES 10
Reeza
Super User

Using sgplot you can use the datalabel option to specify a variable that contains a label. This all depends on how your data is set up of course:

data temp;

do i=1 to 150;

x_ref=i; y_ref=i;

output;

end;

run;

data class;

set sashelp.class temp;

run;

proc sgplot data=class;

scatter x=height y=weight/datalabel=name;

series x=x_ref y=y_ref;

xaxis min=0 max=150;

yaxis min=0 max=150;

run;quit;


Jay54
Meteorite | Level 14

And with SAS 9.3 or later, you can use the LINEPARM statement to draw the 45 degree reference line:

  LINEPARM x=0 y=0 slope=1;

The line is drawn for the full data range determined by the other plots in the proc.  See doc for the details.

Reeza
Super User

Something new Smiley Happy

That makes it much simpler:

proc sgplot data=sashelp.class;

scatter x=height y=weight/datalabel=name;

lineparm x=0 y=0 slope=1;

xaxis min=0 max=150;

yaxis min=0 max=150;

run;quit;

sun014
Calcite | Level 5

Thanks Reeza, Sanjay

For some reason lineparm statement doesn't work for me in sgplot (Error message: Statement is not valid or it is used out of proper order). Is there some general declaration that I need to do before using it? I'm using SAS EG 4.3

Reeza
Super User

EG 4.3 corresponds to SAS 9.1, LINEPARM is a feature of SAS 9.2 as indicated by Sanjay.

djrisks
Barite | Level 11

Try this Refline Statement:

data temp;
do i=1 to 150;
x_ref=i; y_ref=i;
output;
end;
run;


data class;
set sashelp.class temp;
run;


proc sgplot data=class;
scatter x=height y=weight/datalabel=name;
refline 0 / axis = y;
xaxis min=0 max=150;
yaxis min=0 max=150;
run;

quit;

djrisks
Barite | Level 11

Actually, for some reason I thought you wanted a horizontal line. If you want a 45 degree line, then you can also you the VECTOR statement in SAS 9.2 onwards.


data temp;
do i=1 to 150;
x_ref=i; y_ref=i;
output;
end;
run;


data class;
set sashelp.class temp;
y = 150;
x = 150;
run;


proc sgplot data=class;
scatter x=height y=weight/datalabel=name;
vector x = x y = y / noarrowhead yorigin = 0 xorigin = 0;
xaxis min=0 max=150;
yaxis min=0 max=150;
run;

sun014
Calcite | Level 5

Thanks, that works.

I guess you meant to write vector x = x_ref y = y_ref / noarrowhead yorigin = 0 xorigin = 0;

djrisks
Barite | Level 11

Cool, glad to hear that it worked!

The reason why I wrote "vector x = x y = y" is because I wanted the diagonal line to end at 150 on the x and y axis. As you've noticed though you can also use x_ref and y_ref and the advantange of that is you do not have to create additional variables as I previously did. That's actually good for me to know, because I didn't know that before! Thanks.

GraphGuy
Meteorite | Level 14

One way to add labels to the plot markers would be to use the pointlabel= option of the symbol statement.

This should work in SAS 9.1.  Here is an example:

symbol1 value=circle height=2 interpol=none pointlabel=('#name');

proc gplot data=sashelp.class;

plot height*weight=1;

run;

pointlabel.png

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 4936 views
  • 2 likes
  • 5 in conversation