Sample 24874: Maintain patterns across by groups
/* Set graphics options */
goptions reset=all border colors=(black);
/* Create input data set, ICECREAM */
data icecream;
input @1 flavor $10. @12 rank 1. @14 grp $1.;
datalines;
Strawberry 2 B
Chocolate 1 B
Vanilla 3 B
Strawberry 2 A
Vanilla 1 A
Coffee 1 C
Vanilla 3 C
Chocolate 2 C
;
/* After creating the ICECREAM data set, sort the data by GRP */
/* and FLAVOR using the SORT procedure. */
proc sort data=icecream;
by grp flavor;
run;
/* The NEW data set is needed to create the macro variables */
/* GRP, PAT, TOTNBY, and TOTAL. First, specify the */
/* ICECREAM data set in a SET statement and also include a */
/* BY statement with the variables GRP and FLAVOR. The BY */
/* statement provides FIRST.GRP and LAST.GRP variables, which */
/* are used to determine the beginning and end of the BY groups. */
/* The END= option on the SET statement creates the variable */
/* EOF that signals the end-of-file. */
data new;
set icecream end=eof;
by grp flavor;
if first.grp then do; /* Specify an IF-THEN DO to conditionally */
bycount=0; /* check for the beginning of a BY group. */
total+1; /* The purpose of the DO loop is to initialize */
end; /* the variable BYCOUNT to 0 and increment the */
bycount+1; /* variable TOTAL by 1 for each time the BY */
/* group value changes. */
/* The CALL SYMPUT routine is used to create macro variables,*/
/* GRPn, that will resolve to the different BY group values. */
call symput('grp'||left(total),grp);
/* The IF-THEN DO loops are used to conditionally assign the */
/* PATTERN statements based on the value of the midpoint variable,*/
/* FLAVOR. The CALL SYMPUT routines create macro variables, PATn, */
/* that will resolve to the appropriate PATTERN statements across */
/* BY groups. */
if flavor='Strawberry' then do;
call symput('pat'||trim(left(total))||trim(left(bycount)),
'pattern'||trim(left(total))||trim(left(bycount))
|| ' '|| 'c=pink'|| ' '||'v=solid'||' '||';');
end;
if flavor='Chocolate' then do;
call symput('pat'||trim(left(total))||trim(left(bycount)),
'pattern'||trim(left(total))||trim(left(bycount))
|| ' '|| 'c=brown'|| ' '||'v=solid'||' '||';');
end;
if flavor='Vanilla' then do;
call symput('pat'||trim(left(total))||trim(left(bycount)),
'pattern'||trim(left(total))||trim(left(bycount))
|| ' '|| 'c=yellow'|| ' '||'v=solid'||' '||';');
end;
if flavor='Coffee' then do;
call symput('pat'||trim(left(total))||trim(left(bycount)),
'pattern'||trim(left(total))||trim(left(bycount))
|| ' '|| 'c=tan'|| ' '||'v=solid'||' '||';');
end;
/* The LAST.GRP variable is used in an IF condition in order */
/* to create the macro variables, TOTNBYn, which will resolve */
/* to the number of patterns needed for each BY group. The */
/* TOTNBYn macro variables will be used in the PATTERN */
/* macro as the ending value in the %DO loop. */
if last.grp then call symput('totnby'||trim(left(total)),bycount);
/* The end-of-file is determined in order to create the macro */
/* variable, TOTAL. The TOTAL macro variable will be used in the */
/* GCHART macro as the ending value in the %DO loop. */
if eof then call symput('total',total);
run;
/* The PATTERN macro is creating all the pattern */
/* statements to be used across the BY groups. */
%macro pattern;
%do j=1 %to &&totnby&i;
&&pat&i&j
%end;
%mend pattern;
/* The GCHART macro calls the PATTERN macro and calls the */
/* GCHART procedure to create multiple graphs. The WHERE */
/* statement is performing the function that the BY statement */
/* would have provided. The BY statement is not used, because */
/* we need to increment the macro variable GRP using the */
/* counter variable (i) in a %DO loop. */
%macro gchart;
%do i=1 %to &total;
goptions reset=pattern;
%pattern
proc gchart data=icecream;
where grp="&&grp&i";
vbar flavor / sumvar=rank patternid=midpoint raxis=axis1;
title1 f=swiss "Ice Cream Survey for Group &&grp&i";
axis1 minor=none;
run;
quit;
%end;
%mend gchart;
%gchart

This program uses macro coding to conditionally produce PATTERN statements based on the value of the midpoint variable, FLAVOR.
| Type: | Sample |
| Topic: | SAS Reference ==> Procedures ==> GCHART Query and Reporting ==> Creating Reports ==> Graphical ==> Graph Elements ==> Patterns
|
| Date Modified: | 2005-08-24 16:06:27 |
| Date Created: | 2004-11-11 11:07:53 |
Operating System and Release Information
| SAS System | SAS/GRAPH | All | n/a | n/a |