Data visualization with SAS programming

how to subset data for a graph so that lines are not stacked?

Accepted Solution Solved
Reply
Contributor
Posts: 42
Accepted Solution

how to subset data for a graph so that lines are not stacked?

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;

Attachment

Accepted Solutions
Solution
‎03-27-2012 01:43 PM
SAS Super FREQ
Posts: 1,081

how to subset data for a graph so that lines are 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;

BarOverlay.png

View solution in original post


All Replies
SAS Employee
Posts: 967

how to subset data for a graph so that lines are not stacked?

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

Contributor
Posts: 42

how to subset data for a graph so that lines are not stacked?

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

Solution
‎03-27-2012 01:43 PM
SAS Super FREQ
Posts: 1,081

how to subset data for a graph so that lines are 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;

BarOverlay.png

Contributor
Posts: 42

how to subset data for a graph so that lines are not stacked?

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

SAS Super FREQ
Posts: 1,081

how to subset data for a graph so that lines are not stacked?

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

SAS Super FREQ
Posts: 1,081

how to subset data for a graph so that lines are not stacked?

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.

Contributor
Posts: 42

how to subset data for a graph so that lines are not stacked?

I'm using EG 4.3

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

SAS Employee
Posts: 967

how to subset data for a graph so that lines are not stacked?

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;

Contributor
Posts: 42

how to subset data for a graph so that lines are not stacked?

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

SAS Super FREQ
Posts: 1,081

how to subset data for a graph so that lines are not stacked?

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;

☑ This topic is SOLVED.

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

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