data icecream;
   input @1 flavor $10. @12 rank 1. @14 group $1.;
cards;
Strawberry 2 B
Chocolate  1 B
Vanilla    3 B
Strawberry 2 A
Vanilla    1 A
Coffee     1 C
Vanilla    3 C
Chocolate  2 C
;
run;

proc sort data=icecream;
   by group flavor;
run;

data _NULL_;
   set icecream end=eof;
   by group flavor;

   if first.group then do;
   bycount=0;
   total+1;

   call symput('grp'||left(total),group);
   end;

   if first.flavor then
   bycount+1;

   if flavor='Strawberry' then
   call symput('pat'||trim(left(total))||trim(left(bycount)),
               'pattern'||trim(left(total))
               ||trim(left(bycount))
               ||' c=gray1a'||' v=x3'||';');

   if flavor='Chocolate' then
   call symput('pat'||trim(left(total))||trim(left(bycount)),
               'pattern'||trim(left(total))
               ||trim(left(bycount))
               ||' c=black '||' v=solid'||';');

   if flavor='Vanilla' then
   call symput('pat'||trim(left(total))||trim(left(bycount)),
               'pattern'||trim(left(total))
               ||trim(left(bycount))
               ||' c=gray7f '||' v=l3'||';');

   if flavor='Coffee' then
   call symput('pat'||trim(left(total))||trim(left(bycount)),
               'pattern'||trim(left(total))
               ||trim(left(bycount))
               ||' c=gray4c '||' v=r3'||';');

   if last.group then call symput('totnby'||trim(left(total)),bycount);

   if eof then call symput('total',total);
run;

%macro pattern;
   %do j=1 %to &&totnby&i;
      &&pat&i&j
   %end;
%mend pattern;

pattern11 c=gray1a v=x3;     /* Patterns for first BY group  */
pattern12 c=gray7f v=l3;

pattern21 c=black v=solid;   /* Patterns for second BY group */
pattern22 c=gray1a v=x3;
pattern23 c=gray7f v=l3;

pattern31 c=black v=solid;   /* Patterns for third BY group  */
pattern32 c=gray4c v=r3;
pattern33 c=gray7f v=l3;

%macro gchart;
   %do i=1 %to &total;
goptions reset=pattern;
%pattern

proc gchart data=icecream;
   where group="&&grp&i";
   vbar flavor / patternid=midpoint sumvar=rank;
   title1 f=swiss "Ice Cream Flavor Rankings,  Group &&grp&i";
run;
quit;
%end;
%mend gchart;

%gchart

goptions reset=pattern;
pattern11 c=gray1a v=x3;
pattern12 c=gray7f v=l3;
proc gchart data=icecream;
   where group="A";
   vbar flavor / sumvar=rank patternid=midpoint;
   title1 f=swiss "Ice Cream Flavor Rankings,  Group A";
run;
quit;

goptions reset=pattern;
pattern21 c=black v=solid;
pattern22 c=gray1a v=x3;
pattern23 c=gray7f v=l3;
proc gchart data=icecream;
   where group="B";
   vbar flavor / sumvar=rank patternid=midpoint;
   title1 f=swiss "Ice Cream Flavor Rankings,  Group B";
run;
quit;

goptions reset=pattern;
pattern31 c=black v=solid;
pattern32 c=gray4c v=r3;
pattern33 c=gray7f v=l3;
proc gchart data=icecream;
   where group="C";
   vbar flavor / sumvar=rank patternid=midpoint;
   title1 f=swiss "Ice Cream Flavor Rankings,  Group C";
run;
quit;