title: Boxplot by groups & blocks, with axis-aligned statistics
body: <p>I've been building up a GTL template for a boxplot by group and timepoint blocks. I suspect that GTL in SAS 9.4 (TS Level 1M2) does not yet fully support such a multi-dimensional, axis-aligned BOXPLOT plus BLOCKPLOT combination.</p>
  
  <ul>
  <li>If I'm wrong, I'd appreciate suggestions on the right approach.</li>
  <li>If I'm right, I welcome suggestions for a more-or-less straightforward GTL work-around.</li>
  </ul>
  
  <p>The code below produces the first plot, but I really want the 2nd plot. In other words, I want the BLOCKPLOT to support the same two x-axis levels as the BOXPLOT (timepoint and treatment group). However, the BLOCKPLOT will only stack CLASS (treatment group) within X-axis (timepoint). But the BOXPLOT will separate the GROUPs along the X-axis within the blocks.</p>
  
  <p>I don't find any way to switch from BLOCKPLOT's "stack" approach to a "cluster" approach like BOXPLOT. (I've tried AXISTABLEs as well, but they behave like BLOCKPLOTs.)</p>
  
  <p>Thanks for pointing out the right approach or suggesting a work-around.</p>
  
  <p>Representative discussions I've already seen:</p>
  
  <ul>
  <li><p>SOF: <a href="https://stackoverflow.com/questions/33096595/sas-graph-align-axis-for-2-plots-with-lattice-rows">SAS GRAPH: align axis for 2 plots with lattice rows</a></p></li>
  <li><p>SOD: <a href="http://support.sas.com/documentation/cdl/en/grstatug/67914/HTML/default/viewer.htm#n07sfa0fbx6yh5n1dyutt7boc3kp.htm#p12eenhz12jkcon1pjvhy4wzioe1" rel="nofollow noreferrer">Displaying Axis-Aligned Values with a Block Plot</a></p></li>
  <li><p>GS: <a href="http://blogs.sas.com/content/graphicallyspeaking/2011/11/12/graphs-with-axis-aligned-statistics-tables/" rel="nofollow noreferrer">Graphs with axis-aligned statistics tables</a></p></li>
  </ul>
  
  <p>Plot from 9.4 (TS1M2) code, below:
  <a href="https://i.stack.imgur.com/4H3Pa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4H3Pa.png" alt="Plot from 9.4 (TS1M2) code, below"></a></p>
  
  <p>Obviously-manipulated plot that I would like to code:
  <a href="https://i.stack.imgur.com/glgLp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/glgLp.png" alt="Obviously manipulated plot that I want from GTL"></a></p>
  
  <p>Code for first plot:</p>
  
  <pre><code>/* Set up test data structure, with few seed observations */
    proc sql;
      create table labstruct
        (  mygroup         char(3) label='Treatment Group'
         , myvisitnum      num     label='Visit number'
         , myvisitname     char(8) label='Visit name'
         , labtestname     char(8) label='Name of lab test'
         , labseed         num     label='Lab measurement seed'
         , lablow          num     label='Low end of normal range'
         , labhigh         num     label='High end of normal range'
        )
      ;
      insert into labstruct
        values('A', 1,  'Day 1',  'Test XYZ', 48, 40, 60)
        values('B', 1,  'Day 1',  'Test XYZ', 52, 40, 60)
        values('A', 5,  'Week 1', 'Test XYZ', 50, 40, 60)
        values('B', 5,  'Week 1', 'Test XYZ', 50, 40, 60)
        values('A', 10, 'Week 2', 'Test XYZ', 52, 40, 60)
        values('B', 10, 'Week 2', 'Test XYZ', 48, 40, 60)
      ;
    quit;
  
    /* Make more obs from the seeds above */
      data labdata;
        set labstruct;
  
        do repeat = 1 to 20;
          labvalue = labseed + 6*rannor(3297);
          if ranuni(59843)&gt;0.1 then output;
        end;
  
        label labvalue = 'Lab measurement';
        drop repeat labseed;
      run;
  
  
  /* Calculate summary stats, and merge onto lab data for use as "block" labels */
    proc summary data=labdata noprint;
      by myvisitnum mygroup;
      var labvalue;
      output out=labstats (drop=_type_) 
             n=n mean=mean std=std;
    run;
  
    /* Merge summary stats onto lab data, used in GTL template as "block" labels */
    data labdata;
      merge labdata (in=in_data)
            labstats (in=in_stats);
      by myvisitnum mygroup;
      label n      = 'n'
            mean   = 'Mean'
            std    = 'Std Dev';
    run;
  
  
  proc template;
    define statgraph xalignedstats;
      dynamic _TRT _AVISIT _AVISITN _AVAL _STAT1 _STAT2 _STAT3;
  
      begingraph / border=false dataskin=none attrpriority=none;
  
        layout overlay / walldisplay=none ;
  
          innermargin / align=top separator=false pad=(top=0);
            blockplot x=_AVISITN block=_AVISIT /
                      display=(outline values) valuefitpolicy=split ;
          endinnermargin;
  
          boxplot x=_AVISITN y=_AVAL /
                  group=_TRT groupdisplay=cluster clusterwidth=0.8 ;
  
          innermargin / align=bottom separator=false pad=(bottom=0);
            blockplot x=_AVISITN block=_TRT / class=_TRT label='Treatment' filltype=alternate;
            blockplot x=_AVISITN block=_STAT1 / class=_TRT label='n' filltype=alternate;
            blockplot x=_AVISITN block=_STAT2 / class=_TRT label='Mean' filltype=alternate;
            blockplot x=_AVISITN block=_STAT3 / class=_TRT label='Std Dev' filltype=alternate;
          endinnermargin;
  
        endlayout;
  
      endgraph;
    end;
  run;
  
  proc sgrender data=labdata template=xalignedstats;
    dynamic 
            _TRT     ='mygroup'
            _AVISITN ='myvisitnum' 
            _AVISIT  ='myvisitname' 
            _AVAL    ='labvalue'
            _STAT1   ='n'
            _STAT2   ='mean'
            _STAT3   ='std'
            ;
  run;
  </code></pre>