BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Abishekaa
Obsidian | Level 7

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:

Abishekaa_0-1650600237128.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

2 REPLIES 2
ballardw
Super User

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;
Ksharp
Super User
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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 850 views
  • 0 likes
  • 3 in conversation