Data visualization with SAS programming

How to group vbar using sgplot?

Reply
SAS Employee
Posts: 2

How to group vbar using sgplot?

Hi! I need you help! I'm trying to use the proc sgplot in order to plot 3 variables: 1 with a line, and the other 2 variable with grouped vertical bars (no stacked). I've tried several ways but i can´t get what I need... this is my code:

proc sgplot data=work.subset;
title "Budget and sales";
vbar month/ response=sales_A ;
vbar month/ response=sales_B;
vline month/ response=sales_acum y2axis;

run;
SAS Super FREQ
Posts: 1,080

Re: How to group vbar using sgplot?

Can you post the code for your data set?
SAS Super FREQ
Posts: 1,080

Re: How to group vbar using sgplot?

You seem to have 3 separate columns. With this data structure, you have a couple of options. With SAS (TS2M3), you can get the bars side by side using GTL. Else, you can get overlaid bars. See code below. The DISCRETEOFFSET option in GTL will not work for a SAS release prior to TS2M3.

Side-by-side grouped bar charts are coming with SAS 9.3.


data subset;
input month $ sales_a sales_b sales_acum;
datalines;
Jan 200 300 500
Feb 300 400 700
Mar 200 400 600
;
run;
proc print;run;

/*--SGPLOT--*/
ods graphics / width=4in height=3in imagename='BarsSG';
proc sgplot data=work.subset;
title "Budget and Sales";
vbar month/ response=sales_A nostatlabel;
vbar month/ response=sales_B barwidth=0.6 nostatlabel;
vline month/ response=sales_acum y2axis lineattrs=(pattern=solid thickness=4) nostatlabel;
yaxis offsetmin=0;
xaxis discreteorder=data;
run;

/*--GTL--*/
proc template;
define statgraph bars;
begingraph;
entrytitle "Budget and Sales";
layout overlay / yaxisopts=(offsetmin=0);
barchart x=month y=sales_a / discreteoffset=-0.2 barwidth=0.4 fillattrs=graphdata1 name='a';
barchart x=month y=sales_b / discreteoffset= 0.2 barwidth=0.4 fillattrs=graphdata2 name='b';
seriesplot x=month y=sales_acum / lineattrs=graphdata3(pattern=solid thickness=4) yaxis=y2 name='c';
discretelegend 'a' 'b' 'c';
endlayout;
endgraph;
end;
run;

ods graphics / width=4in height=3in imagename='BarsGTL';
proc sgrender data=work.subset template=bars;
run;
SAS Employee
Posts: 2

Re: How to group vbar using sgplot?

Thanks for your example! it was quite helpful. I've tried both options and the second is the best. Regards!
Ask a Question
Discussion stats
  • 3 replies
  • 482 views
  • 0 likes
  • 2 in conversation