This is a sample of my data - Basically, I have multiple measures for each patient, and a calculated mean of those measures. How do I create a scatterplot of the measured values (y) vs the mean value (x) of each patient in one graph? It seems pretty simple, but I have used sgplot and gplot and am stumped!
ID m1 m2 m3 m4 mean
1 5 6 2 1 4
2 2 1 1 0 1
3 9 . 8 7 8
4 2 . . 2 2
This is the type of plot I would like to create:
It really helps when you claim something doesn't work if you 1) show the code you tried and 2) describe why it didn't meet your needs.
Sgplot, one way
proc sgplot data=have noautolegend; scatter y=m1 x=mean/markerattrs=(symbol=circlefilled color=blue);
scatter y=m2 x=mean/markerattrs=(symbol=circlefilled color=blue);
scatter y=m3 x=mean/markerattrs=(symbol=circlefilled color=blue);
scatter y=m4 x=mean/markerattrs=(symbol=circlefilled color=blue);
xaxis label='Horizontal Label'; yaxis label='Vertical Label'; run;
If you do not specify a symbol then each individual scatter will use a different symbol by default (until it runs out of default settings anyway) and if you don't specify a color each plot will have a different color. Without the Noautolegend to suppress the legend each of the marker symbol/colors would have legend entry.
Or possibly (which is likely more flexible in term of multiple measures)
data have; input ID m1 m2 m3 m4 mean; datalines; 1 5 6 2 1 4 2 2 1 1 0 1 3 9 . 8 7 8 4 2 . . 2 2 ; proc transpose data=have out=trans; by id mean; var m1-m4; run; proc sgplot data=trans; scatter x=mean y=col1/group=_name_; label _name_='Measure'; xaxis label='Horizontal Label'; yaxis label='Vertical Label'; run;
It really helps when you claim something doesn't work if you 1) show the code you tried and 2) describe why it didn't meet your needs.
Sgplot, one way
proc sgplot data=have noautolegend; scatter y=m1 x=mean/markerattrs=(symbol=circlefilled color=blue);
scatter y=m2 x=mean/markerattrs=(symbol=circlefilled color=blue);
scatter y=m3 x=mean/markerattrs=(symbol=circlefilled color=blue);
scatter y=m4 x=mean/markerattrs=(symbol=circlefilled color=blue);
xaxis label='Horizontal Label'; yaxis label='Vertical Label'; run;
If you do not specify a symbol then each individual scatter will use a different symbol by default (until it runs out of default settings anyway) and if you don't specify a color each plot will have a different color. Without the Noautolegend to suppress the legend each of the marker symbol/colors would have legend entry.
Or possibly (which is likely more flexible in term of multiple measures)
data have; input ID m1 m2 m3 m4 mean; datalines; 1 5 6 2 1 4 2 2 1 1 0 1 3 9 . 8 7 8 4 2 . . 2 2 ; proc transpose data=have out=trans; by id mean; var m1-m4; run; proc sgplot data=trans; scatter x=mean y=col1/group=_name_; label _name_='Measure'; xaxis label='Horizontal Label'; yaxis label='Vertical Label'; run;
data have;
input ID m1 m2 m3 m4 mean;
datalines;
1 5 6 2 1 4
2 2 1 1 0 1
3 9 . 8 7 8
4 2 . . 2 2
;
proc transpose data=have out=trans;
by id ;
var m1-m4 mean;
run;
data trans;
set trans;
if _name_='mean' then group=1;
else group=2;
run;
ods graphics /attrpriority=none;
proc sgplot data=trans noautolegend;
styleattrs datasymbols=( circle star);
scatter x=id y=col1/group=group datalabel=_name_ markerattrs=(size=20);
label _name_='Measure';
xaxis label='Horizontal Label' type=discrete integer;
yaxis label='Vertical Label';
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: