0

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.

  • You will get more useful help if you post data as text instead of a picture. What do you want to see after *add the means* ? Do you want to see 3 more pairs of bars, the pairs being for period1, period2 and period3 ? As for group level you might want a gradient axis. A picture is worth a thousand words, so don't be afraid to 'hand draw' the image and add that to the question – Richard Jun 05 '20 at 09:33
  • You're right: I should have included the data as a text. The answer below works. Thanks – immaprogrammingnoob Jun 05 '20 at 15:31

1 Answers1

0

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

Richard
  • 25,390
  • 3
  • 25
  • 38