I have the following example which produces the attached graph. How do I alter the code so that in the output the orange and green bars are not stacked but rather one in front of the other?
thanks a ton!
data test;
input bucket date:mmddyy10. pct group $;
datalines;
1 11/01/2011 2 Study
1 12/01/2011 3 Study
1 01/01/2012 2 Study
1 11/01/2011 3 Base
1 12/01/2011 5 Base
1 01/01/2012 3 Base
2 11/01/2011 6 Study
2 12/01/2011 3 Study
2 01/01/2012 4 Study
2 11/01/2011 3 Base
2 12/01/2011 7 Base
2 01/01/2012 2 Base
;
run;
PROC GCHART DATA=WORK.test;
format date mmyyd7.;
VBAR3d
date
/ discrete
SUMVAR=pct
group=bucket
subgroup=group
CLIPREF
FRAME TYPE=SUM
COUTLINE=BLACK
;
RUN; QUIT;
This is relatively straightforward with SGPLOT. Here is the code and output. You can modify it to suit.
data test;
input bucket date:mmddyy10. pct group $;
datalines;
1 11/01/2011 2 Study
1 12/01/2011 3 Study
1 01/01/2012 2 Study
1 11/01/2011 3 Base
1 12/01/2011 5 Base
1 01/01/2012 3 Base
2 11/01/2011 6 Study
2 12/01/2011 3 Study
2 01/01/2012 4 Study
2 11/01/2011 3 Base
2 12/01/2011 7 Base
2 01/01/2012 2 Base
;
run;
/*--Transpose grouped data into multi-variable--*/
data multiVar;
keep Study Base date;
format date mmddyy10.;
merge test(where=(group="Study") rename=(pct=Study))
test(where=(group="Base") rename=(pct=Base));
run;
proc print; run;
ods listing gpath='C:\Work\Blogs\Communities\images';
ods graphics / reset width=5in height=3in imagename='BarOverlay'
title 'Percent by Study';
proc sgplot data=multivar;
vbar date / response=base nostatlabel;
vbar date / response=study nostatlabel barwidth=0.7 transparency=0.1;
yaxis label='Pct';
xaxis display=(nolabel);
run;
Getting bars to appear as though they're in-front/behind each other (rather than stacked or side-by-side) is a bit tricky in SAS/Graph (not really one of the built-in features of Gchart).
Here is an example that you might like to look at, that uses annotate to draw the overlapping bars:
Is it simpler to have it done side by side? Doesn't need to be one in front of the other, just not stacked.
This is relatively straightforward with SGPLOT. Here is the code and output. You can modify it to suit.
data test;
input bucket date:mmddyy10. pct group $;
datalines;
1 11/01/2011 2 Study
1 12/01/2011 3 Study
1 01/01/2012 2 Study
1 11/01/2011 3 Base
1 12/01/2011 5 Base
1 01/01/2012 3 Base
2 11/01/2011 6 Study
2 12/01/2011 3 Study
2 01/01/2012 4 Study
2 11/01/2011 3 Base
2 12/01/2011 7 Base
2 01/01/2012 2 Base
;
run;
/*--Transpose grouped data into multi-variable--*/
data multiVar;
keep Study Base date;
format date mmddyy10.;
merge test(where=(group="Study") rename=(pct=Study))
test(where=(group="Base") rename=(pct=Base));
run;
proc print; run;
ods listing gpath='C:\Work\Blogs\Communities\images';
ods graphics / reset width=5in height=3in imagename='BarOverlay'
title 'Percent by Study';
proc sgplot data=multivar;
vbar date / response=base nostatlabel;
vbar date / response=study nostatlabel barwidth=0.7 transparency=0.1;
yaxis label='Pct';
xaxis display=(nolabel);
run;
You the man Sanjay! thanks so much. that's very easy and helpful!
Note, you do need SAS 9.2 or later version to use SG procedures.
Robert pointed out I did not use the 'Bucket' variable. You can retain it in the data, and either use BY statement in SGPLOT (for separate graphs), or use SGPANEL and PANELBY Bucket to get both buckets in the same graph.
With SAS 9.3, you can use a GROUP variable with GROUPDISPLAY=CLUSTER as another option. or you can use DISCRETEOFFSET to be more creative. With SAS 9.3, you also have the DataSkin option to spiffy up the appearance. See recent blog article.
I'm using EG 4.3
Sanjay, can you provide example syntax for using the sgpanel and the panelby commands?
Or, how to do it with Gchart (if you don't have 9.2).
data test;
input bucket date:mmddyy10. pct group $;
datalines;
1 11/01/2011 2 Study
1 12/01/2011 3 Study
1 01/01/2012 2 Study
1 11/01/2011 3 Base
1 12/01/2011 5 Base
1 01/01/2012 3 Base
2 11/01/2011 6 Study
2 12/01/2011 3 Study
2 01/01/2012 4 Study
2 11/01/2011 3 Base
2 12/01/2011 7 Base
2 01/01/2012 2 Base
;
run;
axis1 label=none value=none;
proc gchart data=test;
by bucket;
format date mmddyy10.;
vbar group / maxis=axis1
type=sum sumvar=pct subgroup=group
group=date gspace=5 space=0;
run;
This is good to Robert, but I'd rather have it all on one graph as opposed to separate graphs for each bucket.
data test;
input bucket date:mmddyy10. pct group $;
datalines;
1 11/01/2011 2 Study
1 12/01/2011 3 Study
1 01/01/2012 2 Study
1 11/01/2011 3 Base
1 12/01/2011 5 Base
1 01/01/2012 3 Base
2 11/01/2011 6 Study
2 12/01/2011 3 Study
2 01/01/2012 4 Study
2 11/01/2011 3 Base
2 12/01/2011 7 Base
2 01/01/2012 2 Base
;
run;
/*--Transpose grouped data into multi-variable--*/
data multiVar;
keep bucket Study Base date;
format date mmddyy10.;
merge test(where=(group="Study") rename=(pct=Study))
test(where=(group="Base") rename=(pct=Base));
run;
proc print; run;
ods listing gpath='C:\Work\Blogs\Communities\images';
ods graphics / reset width=5in height=3in imagename='BarOverlayPlot';
title 'Percent by Study';
proc sgplot data=multivar;
by bucket;
vbar date / response=base nostatlabel;
vbar date / response=study nostatlabel barwidth=0.7 transparency=0.1;
yaxis label='Pct';
xaxis display=(nolabel);
run;
ods graphics / reset width=5in height=3in imagename='BarOverlayPanel';
title 'Percent by Study';
proc sgpanel data=multivar;
panelby bucket / onepanel columns=2;
vbar date / response=base nostatlabel;
vbar date / response=study nostatlabel barwidth=0.7 transparency=0.1;
rowaxis label='Pct';
colaxis display=(nolabel);
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.