immaprogrammingnoob
immaprogrammingnoob

Reputation: 167

Paired bar charts in SAS

I need help creating a single bar chart, that pairs bars together (by two levels of Group), for four time periods. Here's what my table of data look like, sorted by 'Group':

enter image description here

I've figured out how to plot the means for both groups, but only for one time period at a time:

proc sgplot data=Testdata;
  vbar Group /
    response=Baseline
    stat=mean
    GROUPDISPLAY = CLUSTER;
run;

Which gets me this:

enter image description here

However, I'd like to "smoosh" these two bars together, so that they're touching, and then add the means, for each level of group, for the other three time periods, all in one plot. I've tried just adding the other time periods to the 'response=' line (both with, and without commas) but that doesn't work.

Please help!

(And I know this is kind of greedy, but it would be great if anyone could tell me how to change the bar color based on Group level)

TIA for any help.

Upvotes: 0

Views: 731

Answers (1)

Richard
Richard

Reputation: 27508

You will want to transpose the data so you have columns id, group, period, result.

The VBAR satement would change from

  • VBAR GROUP to
  • VBAR PERIOD

and you can use the VBAR features

  • group = GROUP
  • datalabel = result
  • statlabel

Example:

data have;
  call streaminit(123);
  do group = 'A' , 'B';
    do _n_ = 1 to ifn(group='A',6,11);
      id + 1;
      baseline = ifn(group='A', 2125, 4400) + rand('integer', 400) - 200;
      period1 =  ifn(group='A', 2425, 4100) + rand('integer', 600) - 300;
      period2 =  ifn(group='A', 1800, 3600) + rand('integer', 500) - 250;
      period3 =  ifn(group='A', 1600, 2800) + rand('integer', 500) - 250;
      output;
    end;
  end;

  label 
    baseline = 'Basline'
    period1  = '14 Day Average'
    period2  = '30 Day Average'
    period3  = '60 Day Average'
  ;
run;

proc transpose data=have 
  out=plotdata ( 
    rename=(
      _name_  = period
      _label_ = period_label
      col1    = result
    ))
;
  by id group notsorted;
  var baseline period1-period3;
  label period = ' ';
  label period_label = ' ';
run; 

ods html file='plot.html' style=plateau;

proc sgplot data=plotdata;
  vbar period_label /
    response = result
    stat = mean
    groupdisplay = cluster
    group = group
    datalabel = result statlabel
  ;

  xaxis display=(nolabel);
run;

ods html close;

Image

enter image description here

Upvotes: 0

Related Questions