Data visualization with SAS programming

Create barchart with data table for each group value

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 84
Accepted Solution

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


Accepted Solutions
Solution
‎10-03-2017 08:34 PM
SAS Super FREQ
Posts: 1,171

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.

 

 

 

 

 

 

View solution in original post


All Replies
Super User
Super User
Posts: 7,942

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;
Solution
‎10-03-2017 08:34 PM
SAS Super FREQ
Posts: 1,171

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,171

Re: Create barchart with data table for each group value

Posted in reply to Sanjay_SAS
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 537 views
  • 3 likes
  • 3 in conversation