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

I generated spaghetti plots using the series statement in proc sgplot to show values over time for each patient (coming from 2 cohorts). I am repeating the same plots by cohort and would like the patients to have same colors as in the overall plot. How can I do that? Here's my code

**PLOT1;

proc sgplot;

series x= visit y=heartrate/group=patientID;

run;

* Displays values for patients 1, 2, 3, 4, 5, 6, 7 in colors red, blue, green, orange, cyan, black, brown.

* Repeating by treatment;

*PLOT2;

proc sgplot;

series x= visit y=heartrate/group=patientID;

by treatment;

run;

Patients 1, 2, 5, 7 are in treatment 1 and remaining in treatment 2. How can I make sure that the colors assigned to each patient is same as in the "PLOT1"?

Thanks so much!

 

1 ACCEPTED SOLUTION

Accepted Solutions
GraphGuy
Meteorite | Level 14

In general, sgplot assigns colors in the order the group= values are encountered in the dataset. Therefore you can usually sort the dataset by the group= values to get a consistent order.

 

But as reeza said - the best way to guarantee your color assignments are consistent is to use an attribute map.

 

View solution in original post

9 REPLIES 9
Reeza
Super User
Create a data attribute map and then use that in each SGPLOT.
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/grstatproc/n18szqcwir8q2nn10od9hhdh2ksj.htm

This is the simplest way to guarantee colours, especially if you may be missing categories or changing the order in different graphics.
AB2021
Calcite | Level 5

Thank you! I will try that

GraphGuy
Meteorite | Level 14

In general, sgplot assigns colors in the order the group= values are encountered in the dataset. Therefore you can usually sort the dataset by the group= values to get a consistent order.

 

But as reeza said - the best way to guarantee your color assignments are consistent is to use an attribute map.

 

AB2021
Calcite | Level 5

Thank you so much, this worked great for me! I found this easiest to try and implement

Rick_SAS
SAS Super FREQ

I think there is an easier way. Instead of using PROC SGPLOT with a BY statement, switch to PROC SGPANEL and use the PANELBY statement. An example of spaghetti plots using this method is shown halfway through the article, "Create spaghetti plots in SAS."

 

If you want to use PROC SGPLOT, you can either 

AB2021
Calcite | Level 5

Thank you, I tried this now and panelby does work nicely. Is it also possible to include the entire cohort as one of the panel plots (in addition to one panel plot for each by group)?

Rick_SAS
SAS Super FREQ

Sure. Just use PROC SGPLOT without using the BY statement.

Reeza
Super User
Or create a replicate of your data with one group being "ALL" and the others being each unique group.

data add_totals;
set orig orig (in=b);
if b then group = "Overall";
else group = origByVariable;
run;

Then use your new group variable as part of your BY statement.
AB2021
Calcite | Level 5

Thank you

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 9 replies
  • 4156 views
  • 4 likes
  • 4 in conversation