Hi. I am trying to make a simple line series with multiple lines where each line represents a different group. I would like each group line to be a specific color based on a binary indicator variable. In other words, if group A and B are indicator=0 I want them blue (for example), and if groups C, D, and E are indicator=1 I want them red.
How can I do that?
proc sgplot data=mydata;
series y=outcome x=time / group=type;
run;
Along with the code you have, add the LCGROUP option to the SERIES to specify the variable that contains the binary values. LC stands for "LineColor". The SERIES plot has the ability to assign multiple "group" variable to the plot to control different attributes, including line color, line patterns, marker color, and marker shape.
If you want specific colors for each of the binary values, I would use a discrete attributes map to assign the color to the value. In the end, you code will look something like the following:
data my_attrmap;
retain ID "ind_colors";
input value linecolor $;
cards;
0 blue
1 red
;
run;
proc sgplot data=mydata dattrmap=my_attrmap;
series y=outcome x=time / group=type grouplc=indicator lcattrid=ind_colors;
run;
Hope this helps!
@sasgorilla wrote:
Hi. I am trying to make a simple line series with multiple lines where each line represents a different group. I would like each group line to be a specific color based on a binary indicator variable. In other words, if group A and B are indicator=0 I want them blue (for example), and if groups C, D, and E are indicator=1 I want them red.
How can I do that?
proc sgplot data=mydata;
series y=outcome x=time / group=type;
run;
I assume that your indicator variable is named TYPE, but you didn't specifically say that. You should always link the text that describes the problem with the actual name of the variable. Does TYPE have values A B C D E? (That's what I am assuming for this code) Or is TYPE binary?
The simplest method is to use the styleattrs command
proc sgplot data=mydata;
styleattrs datacontrastcolors=(blue blue red red red);
series y=outcome x=time / group=type;
run;
I am always uncomfortable providing code for this obviously contrived example, where C D and E all show up in identical colors on the plot, so you cannot distinguish them visually. I get the feeling that the answer above will not work on your real problem, and I think it would be much better if you explain the real problem.
Thanks, @PaigeMiller . Manually doing this could be a challenge.
More context: There are multiple different group types, each with anywhere from 5 to 10 + groups therein.
Each of these groups has experienced an intervention (indicator=1) or not (indicator=0).
While I have plotted one consolidated line for indicator 1 vs 0 by group type, I also want to be able to see the trend of each individual group based on their their intervention status. This is primarily a visual check to see if there are outliers within groups who aren't following the same pattern as the rest. In other words, do subgroups within the different group types follow similar trends based on the intervention status.
If I can see that at a glance by these groups, then I can further tease out the outliers.
@sasgorilla wrote:
Thanks, @PaigeMiller . Manually doing this could be a challenge.
More context: There are multiple different group types, each with anywhere from 5 to 10 + groups therein.
Each of these groups has experienced an intervention (indicator=1) or not (indicator=0).
While I have plotted one consolidated line for indicator 1 vs 0 by group type, I also want to be able to see the trend of each individual group based on their their intervention status. This is primarily a visual check to see if there are outliers within groups who aren't following the same pattern as the rest. In other words, do subgroups within the different group types follow similar trends based on the intervention status.
If I can see that at a glance by these groups, then I can further tease out the outliers.
Thank you for explaining in more detail. There's no way I could provide a useful answer to the "real" problem based upon your original problem statement.
I'm still not sure I understand what you want. I don't understand this part: "There are multiple different group types, each with anywhere from 5 to 10 + groups therein." Groups within groups?
Nor am I sure I understand what plot you want. Is there any way you could DRAW the plot you want for, let's say, 4 groups, and scan it into your computer and then include it in your reply? (Or have Excel create the plot and then include it in your reply?) To include plots in your reply please use the "Insert Photos" icon (and do NOT attach files).
Yes, essentially you could think of group as company types.
There are self-owned companies (A, B, C ...), there are small multi-owner companies (A1, B1, C1...) and there are larger interstate companies (A2, B2, C2....).
Then, imagine the intervention is a policy. The policy is turned on in certain states and not others, so you now have companies of each type that did and did not experience that policy. So, I want to visually be able to look at the self-owned companies that did have the policy versus those that did not, and do the same for the small multi-owner, larger interstate companies. I want to see in each group type whether the individual companies appear to generally follow the same trends.
Does that make sense?
While this is what you would hope to see if you assumed the policy had an impact, I also want to see if in any of the different types there is more variance.
@sasgorilla wrote:
Yes, essentially you could think of group as company types.
There are self-owned companies (A, B, C ...), there are small multi-owner companies (A1, B1, C1...) and there are larger interstate companies (A2, B2, C2....).
Then, imagine the intervention is a policy. The policy is turned on in certain states and not others, so you now have companies of each type that did and did not experience that policy. So, I want to visually be able to look at the self-owned companies that did have the policy versus those that did not, and do the same for the small multi-owner, larger interstate companies. I want to see in each group type whether the individual companies appear to generally follow the same trends.
Does that make sense?
So, I am out of time today, and I do not have a YES or NO answer to your question "Does that make sense?" Probably tomorrow I can get back to this.
Thanks for your time. I'll keep tinkering.
The plot GROUP= option uses one variable and generally creates one set of characteristics, color, marker, line type, pattern fill, depending on the values of the group variable and the type of plot.
ONE variable.
If you have multiple "group" variables you might be able to accomplish what you want with multiple PLOT statements. You can have more than one Series or Vline or what have in a single call to SGPLOT/SGPANEL . However the data organization may get messy. Same x,y value pair with different group variables will overlay each other and depending on settings like TRANSPARENCY may mean only the top most value is visible or hard to interpret.
Or create a single variable for the actual to plot group variable.
data have(index=(weight)); set sashelp.class; if age in (10 11 12) then group=1; else if age in (13 14) then group=2; else if age in (15 16 17) then group=3; run; proc sgplot data=have; series x=weight y=height/group=age grouplc=group lineattrs=(thickness=4); run;
Along with the code you have, add the LCGROUP option to the SERIES to specify the variable that contains the binary values. LC stands for "LineColor". The SERIES plot has the ability to assign multiple "group" variable to the plot to control different attributes, including line color, line patterns, marker color, and marker shape.
If you want specific colors for each of the binary values, I would use a discrete attributes map to assign the color to the value. In the end, you code will look something like the following:
data my_attrmap;
retain ID "ind_colors";
input value linecolor $;
cards;
0 blue
1 red
;
run;
proc sgplot data=mydata dattrmap=my_attrmap;
series y=outcome x=time / group=type grouplc=indicator lcattrid=ind_colors;
run;
Hope this helps!
@DanH_sas thanks! This was exactly what I was looking for, grouplc=indicator did the trick!
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.
Ready to level-up your skills? Choose your own adventure.