Hello, I am trying to use GTL to make a template for a series of pie charts (I know, I know) that are needed for my org. I am trying to use the DISCRETEATTRMAP statement to ensure that certain levels retain the same colors in the pie chart. (People will be viewing lots of these, so I need consistency across charts). The variables in my data set are numeric, and I am using a format to bin them into categories. The desired output is a pie chart with wedges, but this code is making concentric circles and is not applying the colors.
I have read the documentation that I can find but am still stumped about where I'm going wrong. If I remove the GROUP statement, I get a pie chart with wedges, but not with the correct colors. Any tips would be appreciated!
data test;
input x @@;
datalines;
1 2 2 3 3 3 4 4 4 4 6 6 6 6 6 6 6 7 7 7 5 5 4 4 3 3
;
run;
** Load the binning format ;
proc format;
value bin
1 = 'Never'
2 = 'Once or Twice'
3 - 7 = 'Multiple Times'
;
proc template;
define statgraph pie2;
begingraph;
entrytitle "Overall";
discreteattrmap name='colors' /ignorecase=true; ** This is where things are going wrong. ;
value 'Never' / fillattrs= (color=green) ;
value 'Once or Twice' / fillattrs= (color=yellow) ;
value 'Multiple Times'/ fillattrs= (color=red);
enddiscreteattrmap ;
discreteattrvar attrvar=classfill var=x attrmap='colors';
layout region;
piechart category = x /
stat = pct
datalabellocation = inside
categorydirection = clockwise
start = 180
name = 'pie'
group = classfill;
discretelegend 'pie'
;
endlayout;
endgraph;
end;
run;
proc sgrender data = test
template = pie2;
format x bin.;
run;
Removing group was the first step. I also needed to change the attrvar= to attrvar=x. Thanks!
proc template;
define statgraph pie4;
begingraph;
entrytitle "Overall";
discreteattrmap name='colors' /ignorecase=true; ** This is where things are going wrong. ;
value 'Never' / fillattrs= (color=green) ;
value 'Once or Twice' / fillattrs= (color=yellow) ;
value 'Multiple Times'/ fillattrs= (color=red);
enddiscreteattrmap ;
discreteattrvar attrvar=x var=x attrmap='colors';
layout region;
piechart category = x /
stat = pct
datalabellocation = inside
categorydirection = clockwise
start = 180
name = 'pie'
;
discretelegend 'pie'
;
endlayout;
endgraph;
end;
run;
proc sgrender data = test
template = pie4;
format x bin.;
run;
The
group = classfill
is what makes the concentric rings.
Try removing it and see if the result is as expected.
When I remove it, I get the wedges, but not the desired colors. It's as if I can't get the format to be picked up by the attribute values.
Removing group was the first step. I also needed to change the attrvar= to attrvar=x. Thanks!
proc template;
define statgraph pie4;
begingraph;
entrytitle "Overall";
discreteattrmap name='colors' /ignorecase=true; ** This is where things are going wrong. ;
value 'Never' / fillattrs= (color=green) ;
value 'Once or Twice' / fillattrs= (color=yellow) ;
value 'Multiple Times'/ fillattrs= (color=red);
enddiscreteattrmap ;
discreteattrvar attrvar=x var=x attrmap='colors';
layout region;
piechart category = x /
stat = pct
datalabellocation = inside
categorydirection = clockwise
start = 180
name = 'pie'
;
discretelegend 'pie'
;
endlayout;
endgraph;
end;
run;
proc sgrender data = test
template = pie4;
format x bin.;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.