Data visualization with SAS programming

Create barchart with data table for each group value

Reply
Contributor
Posts: 73

Create barchart with data table for each group value

I want to add a data table to the bottom of each chart generated using the by statement in proc sgplot.

 

I have found code to add a table to the bottom of a single chart at: http://support.sas.com/kb/49/697.html

(Sample 49697: Add a table beneath a graph with the Graph Template Language (GTL)).

 

Capture.PNG

I would like to replicate this to add a table to the bottom of each chart generated using, for example: 

proc sgplot data=sashelp.shoes;
by region;
vbar subsidiary /response=sales ;
run;

Note I like the formatting in the following code better - not that in sample 49697

 

Any help will be appreciated ... I haven't yet used the Graph Template Language.

 

Thanks,

Marie

Esteemed Advisor
Esteemed Advisor
Posts: 6,697

Re: Create barchart with data table for each group value

Well, if I need to loop over a by group and do various then I do this.  I create a dataset with all the unique values.  Then I use that dataset to generate the statements.  I update the code given on that SAS page to show what I mean (the sql and data _null_ at the end).  The generate a title and sgrender for each unique region:

proc template;
   define statgraph barblock;
   begingraph;
      entrytitle 'Total Sales Across Regions';
      layout lattice / rowweights=(.65 .35);
         layout overlay;
	    barchart x=region y=sales / group=region barlabel=true dataskin=gloss;
	 endlayout;
	 layout overlay / xaxisopts=(type=discrete label='Number of Stores per Region' 
                          display=(label)) walldisplay=none;
	    blockplot x=region block=stores / class=product display=(outline values label) 
                                              repeatedvalues=true;
	 endlayout;
      endlayout;
   endgraph;
   end;
run;

proc sort data=sashelp.shoes out=new;
   by region product;
run;

proc means data=new noprint;
   where region in('Africa' 'Asia' 'Canada' 'Pacific' 'United States');
   id sales;
   by region product;
   var stores;
   output out=shoes sum=;
run; 

proc sql;
  create table LOOP as
  select  distinct REGION 
  from    NEW;
quit;

data _null_;
  set loop;
  call execute('title "By group is '||strip(region)||'";');
  call execute('proc sgrender data=shoes template=barblock; where region="'||strip(region)||'"; run;');
run;
SAS Super FREQ
Posts: 1,044

Re: Create barchart with data table for each group value

[ Edited ]

With SAS 9,4, you can use the XAXISTABLE statement to display the table of data.  See code below:

 

proc sgplot data=sashelp.shoes;
  by region;
  vbar subsidiary /response=sales ;
  xaxistable sales;
run;

Shoes.png

 

If you want the bar chart summarized for each subsidiary, but want the data table to show the sales by product, you will need to use the new VBARBASIC with SAS 9.40M3 as shown below..  This is due to the mismatch between VBAR (no group) and XAXISTABLE options.

ods graphics / reset width=6in height=3in imagename='Shoes';
proc sgplot data=sashelp.shoes;
  by region;
  vbarbasic subsidiary /response=sales ;
  xaxistable sales / x=subsidiary class=product;
run;

 

ShoesClass.png

XAXISTABLE has many options.  See examples in Graphically Speaking Blog, or SAS documentation.

 

 

 

 

 

 

SAS Super FREQ
Posts: 1,044

Re: Create barchart with data table for each group value

Post a Question
Discussion Stats
  • 3 replies
  • 248 views
  • 2 likes
  • 3 in conversation