SAS.GRAPH 9.1 Reference, Volumes I and II

Example 7. Using BY- group Processing to Generate a Series of Charts

Features:

AXIS statement options:

BY statement

GOPTIONS statement options:

OPTIONS statement option:

PATTERN statement option:

TITLE statement:

Sample library member: GBYGMSC1

This example uses a BY statement with the GCHART procedure to produce a separate 3D vertical bar chart for each value of the BY variable TYPE. The three charts, which are shown in Display 7.1 on page 243, Display 7.2 on page 244, and Display 7.3 on page 245 following the code, show leading grain producers for 1995 and 1996.

Display 7.1: Output for BY Value Corn

Display 7.2: Output for BY Value Rice

Display 7.3: Output for BY Value Wheat

The program suppresses the default BY lines and instead uses #BYVAL in the TITLE statement text string to include the BY variable value in the title for each chart.

The AXIS1 statement that is assigned to the vertical (response) axis is automatically applied to all three graphs generated by the BY statement. This AXIS statement removes all the elements of the response axis except the label. The same AXIS statement also includes an ORDER= option. Because this option is applied to all the graphs, it ensures that they all use the same scale of response values.

Because no subgroups are specified and the PATTERNID= option is omitted, the color specified in the single PATTERN statement is used by all the bars.

Set the graphics environment. HSIZE= and VSIZE= set the horizontal and vertical size of the graphics output area.

goptions reset=global gunit=pct border cback=white colors=(black blue green red) ftitle=swissb ftext=swiss htitle=5 htext=4 hsize=5in vsize=5in;

Create the data set GRAINLDR. GRAINLDR contains data about grain production in five countries for 1995 and 1996. The quantities in AMOUNT are in thousands of metric tons. MEGTONS converts these quantities to millions of metric tons.

data grainldr; length country $ 3 type $ 5; input year country $ type $ amount; megtons=amount/1000; datalines; 1995 BRZ Wheat 1516 1995 BRZ Rice 11236 1995 BRZ Corn 36276 ... more data lines ... 1996 USA Wheat 62099 1996 USA Rice 7771 1996 USA Corn 236064 ;

Create a format for the values of COUNTRY.

proc format; value $country 'BRZ' = 'Brazil' 'CHN' = 'China' 'IND' = 'India' 'INS' = 'Indonesia' 'USA' = 'United States'; run;

Suppress the default BY-line and define a title that includes the BY-value. #BYVAL inserts the value of the BY variable COUNTRY into the title of each report.

options nobyline; title1 'Leading #byval(type) Producers' j=c '1995 and 1996'; footnote1 j=r h=3 'GBYGMSC1 ';

Specify a color for the bars.

pattern1 color=green;

Define the axis characteristics for the response axes. ORDER= specifies the range of values for the response axes. ANGLE=90 in the LABEL= option rotates the label 90 degrees. All the other options remove axis elements. MAJOR=, MINOR=, and VALUE= remove the tick marks and values. STYLE=0 removes the line. NOPLANE removes the 3D plane.

axis1 order=(0 to 550 by 100) label=(angle=90 'Millions of Metric Tons') major=none minor=none value=none style=0 noplane;

Define midpoint axis characteristics. SPLIT= defines the character that causes an automatic line break in the axis values.

axis2 label=none split=' ';

Sort data according to values of BY variable. The data must be sorted before running PROC GCHART with the BY statement.

proc sort data=grainldr out=temp; by type; run;

Generate the vertical bar charts using a BY statement. The BY statement produces a chart for each value of SITE. The FORMAT statement assigns the $COUNTRY. format to the chart variable. Assigning AXIS1 to RAXIS= causes all three charts to have thesame response axis.

proc gchart data=temp (where=(megtons gt 31)) by type; format country $country.; vbar3d country / sumvar=megtons outside=sum descending shape=hexagon width=8 coutline=black cframe=grayaa maxis=axis2 raxis=axis1 name='GBYGMSC1'; run; quit;

Категории