SAS - Box Plots

A Boxplot is graphical representation of groups of numerical data through their quartiles. Box plots may also have lines extending vertically from the boxes (whiskers) indicating variability outside the upper and lower quartiles. The bottom and top of the box are always the first and third quartiles, and the band inside the box is always the second quartile (the median). In SAS a simple Boxplot is created using PROC SGPLOT and paneled boxplot is created using PROC SGPANEL.
Please note that we create the data set named CARS1 in the first example and use the same data set for all the subsequent data sets. This data set remains in the work library till the end of the SAS session.
The basic syntax to create a boxplot in SAS is −
PROC SGPLOT  DATA = DATASET;
   VBOX VARIABLE / category = VARIABLE;
RUN; 

PROC SGPANEL  DATA = DATASET;;
PANELBY VARIABLE;
   VBOX VARIABLE> / category = VARIABLE;
RUN; 

DATASET − is the name of the dataset used.
DATASET − is the name of the dataset used.
VARIABLE − is the value used to plot the Boxplot.
VARIABLE − is the value used to plot the Boxplot.
In a simple Boxplot we choose one variable from the data set and another to form a category. The values of the first variable are categorized in as many number of groups as the number of distinct values in the second variable.
In the below example we choose the variable horsepower as the first variable and type as the category variable. So we get boxplots for the distribution of values of horsepower for each type of car.
PROC SQL;
create table CARS1 as
SELECT make, model, type, invoice, horsepower, length, weight
   FROM 
   SASHELP.CARS
   WHERE make in ('Audi','BMW')
;
RUN;

PROC SGPLOT  DATA = CARS1;
   VBOX horsepower 
   / category = type;

   title 'Horsepower of cars by types';
RUN; 
When we execute the above code, we get the following output −
We can divide the Boxplots of a variable into many vertical panels(columns). Each panel holds the boxplots for all the categorical variables. But the boxplots are further grouped using another third variable which divides the graph into multiple panels.
In the below example we have paneled the graph using the variable 'make'. As there are two distinct values of 'make' so we get two vertical panels.
PROC SGPANEL  DATA = CARS1;
PANELBY MAKE;
   VBOX horsepower   / category = type;

   title 'Horsepower of cars by types';
RUN; 

When we execute the above code, we get the following output −
We can divide the Boxplots of a variable into many horizontal panels(rows). Each panel holds the boxplots for all the categorical variables. But the boxplots are further grouped using another third variable which divides the graph into multiple panels. In the below example we have paneled the graph using the variable 'make'. As there are two distinct values of 'make' so we get two horizontal panels.
PROC SGPANEL  DATA = CARS1;
PANELBY MAKE / columns = 1 novarname;

   VBOX horsepower   / category = type;

   title 'Horsepower of cars by types';
RUN; 

When we execute the above code, we get the following output −

    50 Lectures 
     5.5 hours 

 Code And Create

    124 Lectures 
     30 hours 

 Juan Galvan

    162 Lectures 
     31.5 hours 

 Yossef Ayman Zedan

    35 Lectures 
     2.5 hours 

 Ermin Dedic

    167 Lectures 
     45.5 hours 

 Muslim Helalee
 Print
 Add Notes
 Bookmark this page