BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
BruceBrad
Lapis Lazuli | Level 10

Anyone have suggestions for simple ways to present this sort of data: multiple series with groups. The following codes doesn't work, the legend only has information for the groups in the first series. If I can't get both the series and groups in the legend, I'd like to have the series in the legend with a consistent formatting for the groups (eg group B always dotted lines).

 

data temp;
input x grp $ s1 s2 s3;
cards;
1 A 1 1 1
2 A 2 3 4
3 A 1 2 3
1 B 1.2 1.2 1.2
2 B 2.2 3.2 4.2
3 B 1.2 2.2 3.2
;
run;

proc sgplot data=temp;
series x=x y=s1 /markers group=grp ;
series x=x y=s2 /markers group=grp ;
series x=x y=s3 /markers group=grp ;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Then you need to change your data structure by proc transpose.

 

data temp;
input x grp $ s1 s2 s3;
cards;
1 A 1 1 1
2 A 2 3 4
3 A 1 2 3
1 B 1.2 1.2 1.2
2 B 2.2 3.2 4.2
3 B 1.2 2.2 3.2
;
run;

proc sort data=temp;by grp x;run;
proc transpose data=temp out=temp2;
by grp x;
var s:;
run;
proc sort data=temp2 out=temp3;by _name_ grp x;run;
data temp4;
 set temp3;
 group=cats(_name_,grp);
run;
proc sgplot data=temp4;
series x=x y=col1/markers group=group grouplc=_name_ grouplp=grp groupms=_name_ groupmc=_name_;
run;

Ksharp_0-1713951726601.png

 

 

View solution in original post

8 REPLIES 8
Ksharp
Super User

You want this ?

data temp;
input x grp $ s1 s2 s3;
cards;
1 A 1 1 1
2 A 2 3 4
3 A 1 2 3
1 B 1.2 1.2 1.2
2 B 2.2 3.2 4.2
3 B 1.2 2.2 3.2
;
run;

proc sgplot data=temp;
series x=x y=s1 /markers group=grp name='a' ;
series x=x y=s2 /markers group=grp name='b';
series x=x y=s3 /markers group=grp name='c';
keylegend 'a' 'b' 'c';
run;

Ksharp_0-1713938930909.png

 

 

Or this one ?

ods graphics/attrpriority=none;
proc sgplot data=temp;
series x=x y=s1 /markers group=grp name='a' ;
series x=x y=s2 /markers group=grp name='b';
series x=x y=s3 /markers group=grp name='c';
keylegend 'a' 'b' 'c';
run;

Ksharp_1-1713939001872.png

 

BruceBrad
Lapis Lazuli | Level 10
Thanks. I see now how to get all lines in the legend. But I need some way to indicate both the series and the group. Eg three different colours (or markers) for the series, and two different patterns for the groups (eg A=solid, B=dotted).
Ksharp
Super User

Then you need to change your data structure by proc transpose.

 

data temp;
input x grp $ s1 s2 s3;
cards;
1 A 1 1 1
2 A 2 3 4
3 A 1 2 3
1 B 1.2 1.2 1.2
2 B 2.2 3.2 4.2
3 B 1.2 2.2 3.2
;
run;

proc sort data=temp;by grp x;run;
proc transpose data=temp out=temp2;
by grp x;
var s:;
run;
proc sort data=temp2 out=temp3;by _name_ grp x;run;
data temp4;
 set temp3;
 group=cats(_name_,grp);
run;
proc sgplot data=temp4;
series x=x y=col1/markers group=group grouplc=_name_ grouplp=grp groupms=_name_ groupmc=_name_;
run;

Ksharp_0-1713951726601.png

 

 

BruceBrad
Lapis Lazuli | Level 10
Great. So, summarising:
- Reshape the data to use one series with groups (and use the 9.4m2+ options to control the group colours and patterns).
- I guess reshaping the data and using multiple series without groups would also work (but not as elegant).
Thanks.
Ksharp
Super User

Yes. You are right. The following code is more simple than the post of mine above (GROUPLC= option make me to complcate the code).

 

data temp;
input x grp $ s1 s2 s3;
cards;
1 A 1 1 1
2 A 2 3 4
3 A 1 2 3
1 B 1.2 1.2 1.2
2 B 2.2 3.2 4.2
3 B 1.2 2.2 3.2
;
run;

ods graphics/attrpriority=none;
proc sgplot data=temp nocycleattrs;
series x=x y=s1 /markers group=grp lineattrs=(color=green) markerattrs=(symbol=plus);
series x=x y=s2 /markers group=grp lineattrs=(color=red) markerattrs=(symbol=circle);
series x=x y=s3 /markers group=grp lineattrs=(color=blue) markerattrs=(symbol=X);
run;

Ksharp_0-1714014742723.png

 

BruceBrad
Lapis Lazuli | Level 10
I'm not sure this is simple! It means I have to understand the interaction between attrpriority and cycleattrs. Even after reading https://blogs.sas.com/content/iml/2018/06/13/attrpriority-cycleattrs-styleattrs-ods-graphics.html this seems very messy.
Ksharp
Super User
Yes. Try read its DOC in support.sas.com
ballardw
Super User

Another option might be to move to SGPANEL and display each series in a different graph:

Using the data set Temp3 as created by @Ksharp 

 

proc sgpanel data=temp3;
   panelby _name_;
   series x=x y=col1/markers group=grp ;
   label _name_ ='Series';
run;

If each series were to have more than 2 values for Grp, or if values are very similar or more complex it might be hard to tell things apart based just on the line style attributes. Using options in Sgpanel to produce on column of graphs would place them one over another for possibly easier interpretation.

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!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 981 views
  • 0 likes
  • 3 in conversation