Control Chart with Data Table

 /****************************************************************/
 /*          S A S   S A M P L E   L I B R A R Y                 */
 /*                                                              */
 /*    NAME: SHWBLK2                                             */
 /*   TITLE: Control Chart with Data Table                       */
 /* PRODUCT: QC                                                  */
 /*  SYSTEM: ALL                                                 */
 /*    KEYS: Shewhart Charts,                                    */
 /*   PROCS: SHEWHART                                            */
 /*    DATA:                                                     */
 /*                                                              */
 /*     REF: SAS/QC Software:  Examples                          */
 /*    MISC:                                                     */
 /*                                                              */
 /****************************************************************/
options ps=60 ls=80 nodate;

title 'Control Chart with Data Table';

data one;
   drop i;
   retain subgrp;
   input  subgrp @;
   do i=1 to 4;
      input  x @@;
      output;
      end;
   cards;
    1  9.0  9.6 10.2  8.4
    2 10.5  9.5  9.9 10.9
    3 10.7 12.8  6.8 12.8
    4  9.5 10.5 10.3  9.7
    5  9.6 10.2  9.4 10.0
    6 11.8 12.2 12.5 13.5
    7 12.0 10.4 11.8  9.6
    8 11.0 10.8 10.3 11.5
    9 11.0 10.0 11.3  9.7
   10 12.0 11.8  9.3 12.5
   ;

 * Create OUTTABLE= dataset containing subgroup information ;

proc shewhart data=one;
   xrchart x*subgrp / nochart
                      outtable = table;
   run;

 * Create block variables containing observations ;

data tab1;
   set one;
   keep block1 block2 block3 block4;
   reps+1;
   if      reps=1 then block1 = x;
   else if reps=2 then block2 = x;
   else if reps=3 then block3 = x;
   else do;
      block4 = x;
      output;
      reps=0;
      end;
   block1 = put(block1,5.2);
   block2 = put(block2,5.2);
   block3 = put(block3,5.2);
   block4 = put(block4,5.2);
   retain block1-block4;
   run;

data tab2;
   merge table tab1;
run;

symbol1 v=dot h=.75;
proc shewhart table=tab2;
   xrchart x*subgrp (block4 block3 block2 block1 ) /
                   blocklabtype = scaled
                   blockpos     = 4
                   hoffset      = 4
                   cframe       = gray
                   cinfill      = yellow
                   blockrep
                   novangle;
   run;

 * Add labels X1-X4 to first subgroup ;

data tab2;
   set tab1;
   keep blck1 blck2 blck3 blck4;
   blck1 = left(block1);
   blck2 = left(block2);
   blck3 = left(block3);
   blck4 = left(block4);
   if _n_ = 1 then do;
      blck1 = 'X1   '||blck1;
      blck2 = 'X2   '||blck2;
      blck3 = 'X3   '||blck3;
      blck4 = 'X4   '||blck4;
   end;
run;

 * Use Block Variables To Hightlight Out-of-Control Points ;

data table;
   set table;
   if (_subx_ >_uclx_) or (_subx_ < _lclx_ ) then do;
      c1 = 'red     ';
      c2 = 'red     ';
      c3 = 'red     ';
      c4 = 'red     ';
      end;
   if (_subr_ >_uclr_) or (_subr_ < _lclr_ ) then do;
      c1 = 'red     ';
      c2 = 'red     ';
      c3 = 'red     ';
      c4 = 'red     ';
      end;
run;

data temp;
   merge table tab2;
run;

title 'Out-of-Control Points Highlighted';

symbol1 v=dot h=.75;
proc shewhart table=temp;
   xrchart x*subgrp (blck4 blck3 blck2 blck1 ) /
                   blocklabtype = scaled
                   blockpos     = 4
                   hoffset      = 4
                   cblockvar    = (c1 c2 c3 c4)
                   cframe       = gray
                   cinfill      = yellow
                   blockrep
                   novangle
                   nolegend;
   run;

goptions reset=all;