By group controlling line colors/where clause

1.6k Views Asked by At

I want to plot Y by X plot where I group by year, but color code year based on different variable (dry). So each year shows as separate line but dry=1 years plot one color and dry=0 years plot different color. I actually figured one option (yeah!) which is below. But this doesn't give me much control.

Is there a way to put a where clause in the series statement to select specific categories so that I can specifically assign a color (or other format)? Or is there another way? This would be analogous to R where one can use multiple line statements for different subsets of data.

Thanks!!

This code works.

proc sgplot data = tmp;  
   where microsite_id = "&msit";
   by microsite_id ;
   yaxis label= "Pct. Stakes" values = (0 to 100 by 20);
   xaxis label= 'Date'  values = (121 to 288 by 15);
   series y=tpctwett x=jday / markers markerattrs=(symbol=plus) group = year grouplc=dry groupmc=dry;
   format jday tadjday metajday jdyfmt.;
   label tpctwett='%surface water' tadval1='breed' metaval1='meta';
run; 
1

There are 1 best solutions below

0
On

Use an Attribute map, see the documentation

You can use the DRY variable to set the specific colours. For each year, assign the colour using the DRY variable in a data step.

 proc sort data=tmp out=attr_data; by year; run;

 data attrs;
 set attr_data;
 id='year';
 if dry=0 then linecolor='green';
 if dry=1 then  linecolor='red';
 keep id linecolor;
 run;

Then add the dattrmap=attrs in the PROC SGPLOT statement and the attrid=year in the SGPLOT options.

ods graphics / attrpriority=none;

proc sgplot data = tmp dattrmap=attrs;  
where microsite_id = "&msit";
by microsite_id ;
yaxis label= "Pct. Stakes" values = (0 to 100 by 20);
xaxis label= 'Date'  values = (121 to 288 by 15);
series y=tpctwett x=jday / markers markerattrs=(symbol=plus) group = year grouplc=dry groupmc=dry attrid=year;
format jday tadjday metajday jdyfmt.;
label tpctwett='%surface water' tadval1='breed' metaval1='meta';
run; 

Note that I tested and edited this post so it should work now.