Dan
Dan

Reputation: 143

Treatment x time boxplots in sas

I am trying to make a boxplot in SAS that displays 2 treatments over 5 periods in the same chart. I can make it do one or the other but not both. Code is below.

proc boxplot data=data;
id treatment;
plot pred1*sample;
run;

also tried

proc boxplot data=data;
plot (pred1 sample)*treatment;
run;

no luck either way. Any ideas? Seems like this should be straightforward.

Thanks

Upvotes: 1

Views: 1338

Answers (1)

Joe
Joe

Reputation: 63424

proc sgplot can likely do this, if I understand you properly and if you have the right version of SAS (9.3+ or maybe 9.4+, I'm not sure which).

I'm assuming you want two boxes, one for pred and one for sample. Here's a simple example:

data treatments;
  call streaminit(7);
  do treatment=1 to 2;
    do period=1 to 5;
      pred1 = treatment+period+rand('normal')*4;
      sample = treatment+period+rand('normal')*5;
      output;
    end;
  end;
run;


proc sgplot data=treatments;
  hbox pred1/ category=treatment legendlabel='Prediction' discreteoffset=.2 boxwidth=0.2;
  hbox sample/category=treatment legendlabel='Sample' boxwidth=0.2;
run;

There I set them next to each other using the discreteoffset (moving the bar a bit) and shrink the bar to match the offset.

First Example

You could also use group to do this, using a vertical dataset (ie, one 'value' variable with an indicator that identifies which result group they're in).

data treat_vert;
  set treatments;
  group='Prediction';
  value=pred1;
  output;
  group='Sample';
  value=sample;
  output;
run;
 proc sgplot data=treat_vert;
   hbox value/category=treatment group=group;
 run;

Second Example using GROUP

Both options have a lot of flexibility in terms of labelling/etc. if you want to go that route.

Upvotes: 3

Related Questions