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

How can I control the color of the band when I use GROUP option? For example, I have 4 groups and would like to have specific colour for each group, or at least something related to the line color for that group.

My code is:

proc sgplot data=sp_in_out_stats;
	styleattrs datalinepatterns=(solid dash) datacontrastcolors=(orange black);
	band x=t lower=cum_r_excess_lclm upper=cum_r_excess_uclm/group=in_out ;
	series x=t y=cum_r_excess_mean/group=in_out curvelabel curvelabelloc=outside ;	
run;

Note that I have removed other function/option within the PROC SGPLOT for ease of following. 

So can you see that in the following graph, each band has a different color. One outcome I would like to achieve is to have 1 color for the top 2 groups (IN_0 IN_1) and another color for the bottom two. Each group would have different levels of transparency.

SGPlot115.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
unison
Lapis Lazuli | Level 10

For the band colors, sorting the data will accomplish what you want. To get the line pattern you desire I think you have to use a data attribute mapping. See below:

 

data have;
	set sashelp.citimon(obs=20 keep=date c:);
run;

proc transpose data=have out=step1(rename=(_NAME_=GROUP COL1=VALUE) 
		drop=_LABEL_);
	by date;
run;

data step2;
	set step1;
	lower=VALUE-5000;
	upper=VALUE+5000;
run;

proc sort data=step2(keep=group) 
	nodupkey out=groups;
	by group;
run;

data attrmap(rename=group=value);
	retain id "attrmapping";
	set groups;
	format linepattern $5.;
	if mod(_n_,2) = 0 then linepattern = "solid";
	else linepattern = "dash";
run;

proc sgplot data=step2 dattrmap=attrmap;
	band x=date lower=lower upper=upper/group=group;
	styleattrs   
		datacolors=(VIYG BIYG PAP VPAP);	
	series x=date y=value/group=group lineattrs=(thickness=3) attrid=attrmapping;
	styleattrs 
		datacontrastcolors=(white white yellow yellow);
run;

proc sort data=step2;
	by descending group;
run;

proc sgplot data=step2 dattrmap=attrmap;
	band x=date lower=lower upper=upper/group=group;
	styleattrs   
		datacolors=(VIYG BIYG PAP VPAP);	
	series x=date y=value/group=group lineattrs=(thickness=3) attrid=attrmapping;
	styleattrs 
		datacontrastcolors=(white white yellow yellow);
run;

sg1.pngsg2.png

 

This website is helpful for color-picking: https://www.devenezia.com/docs/SAS/sas-colors.html

-unison

 

-unison

View solution in original post

8 REPLIES 8
unison
Lapis Lazuli | Level 10

For the band colors, sorting the data will accomplish what you want. To get the line pattern you desire I think you have to use a data attribute mapping. See below:

 

data have;
	set sashelp.citimon(obs=20 keep=date c:);
run;

proc transpose data=have out=step1(rename=(_NAME_=GROUP COL1=VALUE) 
		drop=_LABEL_);
	by date;
run;

data step2;
	set step1;
	lower=VALUE-5000;
	upper=VALUE+5000;
run;

proc sort data=step2(keep=group) 
	nodupkey out=groups;
	by group;
run;

data attrmap(rename=group=value);
	retain id "attrmapping";
	set groups;
	format linepattern $5.;
	if mod(_n_,2) = 0 then linepattern = "solid";
	else linepattern = "dash";
run;

proc sgplot data=step2 dattrmap=attrmap;
	band x=date lower=lower upper=upper/group=group;
	styleattrs   
		datacolors=(VIYG BIYG PAP VPAP);	
	series x=date y=value/group=group lineattrs=(thickness=3) attrid=attrmapping;
	styleattrs 
		datacontrastcolors=(white white yellow yellow);
run;

proc sort data=step2;
	by descending group;
run;

proc sgplot data=step2 dattrmap=attrmap;
	band x=date lower=lower upper=upper/group=group;
	styleattrs   
		datacolors=(VIYG BIYG PAP VPAP);	
	series x=date y=value/group=group lineattrs=(thickness=3) attrid=attrmapping;
	styleattrs 
		datacontrastcolors=(white white yellow yellow);
run;

sg1.pngsg2.png

 

This website is helpful for color-picking: https://www.devenezia.com/docs/SAS/sas-colors.html

-unison

 

-unison
somebody
Lapis Lazuli | Level 10

Thanks. This solves half of my problem. Is there a way to make the 2 lines with the same color to have a solid and a dash pattern?

I tried 

datalinepatterns=(solid dash) 
datacontrastcolors=(black red) 

but SAS cycles the colour first and then the pattern which is quite frustrating

ChrisNZ
Tourmaline | Level 20

This is exactly why I think table and variable names should be uppercased.

proc sgplot data=STEP2 dattrmap=ATTRMAP;
  band x=DATE lower=LOWER upper=UPPER/group=GROUP;
  styleattrs datacolors=(viyg biygpap vpap);	
  series x=DATE y=VALUE/group=GROUP lineattrs=(thickness=3) attrid=ATTRMAPPING;
  styleattrs datacontrastcolors=(white white yellow yellow);
run;

This a lot more legible imho.

 

ChrisNZ
Tourmaline | Level 20

Have you seen the transparency= option?

somebody
Lapis Lazuli | Level 10

I can do transparency. However, choosing colour with patterns is troublesome as (I think) SAS cycles colours first and then the patterns

unison
Lapis Lazuli | Level 10

Hey @somebody, see my edited post above. I think an attribute map will accomplish what you want.

-unison
somebody
Lapis Lazuli | Level 10

Thanks. That does it. just one last question, can't we do it within PROC SGPLOT? Do we have to use a DATA step to create the attrmap?

ballardw
Super User

The Dattrmap would be a dataset so can't be done in Sgplot.

You may be able to get close to what you want by using the STYLEATTRS statement to provide lists of items such as:

 

  styleattrs  
     datacontrastcolors=(red green blue) 
     datalinepatterns=(dot solid);

would assign the colors red, green and blue as the first three datacontrast colors used and the two line types.

The fun part may be getting needed repeats and orders to align with your data.

If there is much complexity the Dattrmap option is more robust at least in my opinion and if you make a permanent data set is always available for making consistent combinations of colors, markers and line properties. The Styleattrs would likely need to be adjusted as if your data has groups that only appear in some of your analysis subsets.

SAS Innovate 2025: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 4761 views
  • 4 likes
  • 4 in conversation