FOCUS AREAS

Graphics

grouped histogram
Base SAS: Graph Template Language

/* This program requires SAS 9.4 TS Level 1M2 or later */
%let gpath='.';
%let dpi=100;

/**
 * This sample uses the Graph Template Language (GTL) to produce a grouped
 * distribution plot. Group processing for computed histogram and density
 * is new for SAS 9.4 Maintainence 2.
 * It uses a two-row Lattice Layout, with the upper cell for the grouped
 * histogram with a grouped normal density curve and legend.
 * The lower cell has a (grouped) horizontal Box Plot.
 */

/* Merge systolic and diastolic into BP with a group of Type */
data heart;
  keep Type Sex BP;
  label BP="Blood Pressure";
  length Type $10;
  set sashelp.heart;
  Type='Systolic'; BP=systolic; output;
  Type='Diastolic'; BP=diastolic; output;
run;

proc template;
  define statgraph grpHistogram;
    dynamic var "Variable of interest"
       groupVar "Variable to group by";
    begingraph;	 
      entrytitle "Distribution of Blood Pressure by Type";
                  
      legendItem name="norm" type=line / label="Normal"
        lineAttrs=GraphFit(color=black);
      layout lattice / rowweights=(.85 .15) columndatarange=union rowgutter=2px;
        columnaxes;
          columnaxis / display=(ticks tickvalues label) gridDisplay=on;
        endcolumnaxes;
        layout overlay / yaxisopts=(offsetmin=.03 griddisplay=auto_on);
          
          entry  halign=left "N: " eval(strip(put(n(sex),12.0))) / vAlign=top;

          histogram var / group=groupVar scale=percent name="hist"
            nbins=30 binaxis=true datatransparency=0.5;
          densityplot var / group=groupVar normal() legendlabel="Normal";

          discretelegend "hist" "norm"  / location=inside across=1
            autoalign=(topright) opaque=true itemSize=(lineLength=3%); 
        endlayout;
 
        layout overlay;		    
          boxplot y=var / group=groupVar groupDisplay=cluster
            meanAttrs=GraphBoxMean(symbol=diamond)
            orient=horizontal datatransparency=0.5;
        endlayout; 
      endlayout;
    endgraph;
  end; 
run;

ods html close;
ods listing gpath=&gpath image_dpi=&dpi style=default;
ods graphics / reset attrpriority=color width=6.93in height=3.15in
  imagename='GroupedHistogram';
proc sgrender data=heart template=grpHistogram ;
  dynamic var="BP" groupVar="Type";
run;

ods listing close;