The sample code on the Full Code tab uses PROC GCHART with SAS macro code to conditionally use PATTERN statements based on the value of the midpoint variable. This enables you to maintain the patterns across BY groups.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
The graphics output in the Results tab was produced using SAS® 9.2. Submitting the sample code with releases of SAS prior to SAS 9.2 might produce different results.
/* Set the graphics environment */
goptions reset=all cback=white border htitle=12pt htext=10pt;
/* Specify macro debugging options */
options mprint mlogic symbolgen;
/* Create the 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
;
run;
/* Sort the data set by GRP and FLAVOR */
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=vibg'|| ' '||'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=mob'|| ' '||'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=depk'|| ' '||'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=grp'|| ' '||'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 cframe=beige;
title1 "Ice Cream Survey for Group &&grp&i";
axis1 minor=none;
run;
quit;
%end;
%mend gchart;
/* Call the macro to produce the graphs */
%gchart
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Type: | Sample |
Topic: | SAS Reference ==> Procedures ==> GCHART Query and Reporting ==> Creating Reports ==> Graphical ==> Graph Types ==> Charts ==> Bar |
Date Modified: | 2005-08-24 16:06:27 |
Date Created: | 2004-11-11 11:07:53 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | SAS/GRAPH | All | n/a | n/a |