Boxplot in SAS using proc gchart

2019-07-26 09:06发布

问题:

First question, is it possible to produce a boxplot using proc gchart in SAS?

If it is possible, please give me a brief idea.

Or else, on the topic of using proc boxplot. Suppose I have a dataset that has three variables ID score year; something like,

data aaa;
input id score year;
datalines;
1 50 2008
1 40 2007
2 30 2008
2 20 2007
;
run;

I want to produce a boxplot showing for each ID in each year. (So in this case, 4 boxplots in a single plot) How can i achieve this?

I have tried using

proc boxplot data=aaa;
plot score*ID;
by year;
run;

However, this is not working as we can see year is not sorted by order. Is there a way to get other this?

回答1:

You need to sort your input dataset first. Run this

proc sort data = aaa;
    by year;
run;

and then your proc boxplot should work as written.



回答2:

This is quite easy to do with sgplot, which is part of the newer ODS Graphics suite which is available in base SAS.

proc sgplot data=sashelp.cars;
  vbox mpg_city/category=type group=origin grouporder=ascending;
run;

You would use category=id and group=year in your example data - you get one separate tick on the x axis for each category and then you get a separate bar clustered together for each group.



标签: sas