BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SASguyCO
Calcite | Level 5

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;


test.jpg
1 ACCEPTED SOLUTION

Accepted Solutions
Jay54
Meteorite | Level 14

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;

BarOverlay.png

View solution in original post

10 REPLIES 10
GraphGuy
Meteorite | Level 14

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:

http://robslink.com/SAS/democd29/robslink.htm

http://robslink.com/SAS/democd29/robslink_info.htm

SASguyCO
Calcite | Level 5

Is it simpler to have it done side by side?  Doesn't need to be one in front of the other, just not stacked. 

Jay54
Meteorite | Level 14

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;

BarOverlay.png

SASguyCO
Calcite | Level 5

You the man Sanjay! thanks so much.  that's very easy and helpful!

Jay54
Meteorite | Level 14

Note, you do need SAS 9.2 or later version to use SG procedures.

Jay54
Meteorite | Level 14

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.

SASguyCO
Calcite | Level 5

I'm using EG 4.3

Sanjay, can you provide example syntax for using the sgpanel and the panelby commands? 

GraphGuy
Meteorite | Level 14

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;

SASguyCO
Calcite | Level 5

This is good to Robert, but I'd rather have it all on one graph as opposed to separate graphs for each bucket.

Jay54
Meteorite | Level 14

BarOverlayPanel.png

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 2365 views
  • 0 likes
  • 3 in conversation