Obsidian | Level 7

## Boxplot Help

Hi SAS Coders!

I created a box plot that looks like this:

My quartiles are out of order and I have tried everything. Here is my code for reference:

``````ODS NOPROCTITLE;
PROC SGPLOT DATA=WORK.FIGURE;
GROUPORDER = DATA
CATEGORY=Quartiles
FILLATTRS=(COLOR=LIGHTBLUE) BOXWIDTH=0.8;
TITLE BOLD "Boxplot of Quartiles of Marijuana Use and Frequent Mental Distress";
XAXIS DISPLAY=(NOLABEL) LABELATTRS=(WEIGHT=BOLD);
YAXIS LABEL="Frequent Mental Distress" VALUES=(0 TO 100 BY 10) OFFSETMIN=0;
XAXIS LABEL="Quartiles";
REFLINE 70 / AXIS=Y LINEATTRS=(COLOR=BLUE);
REFLINE 90 / AXIS=Y LINEATTRS=(COLOR=BLUE);
YAXIS GRID;
INSET "Data Source: Colorado Study Data"/ POSITION=NW BORDER;
RUN;``````

How do I get it where it goes from "First Quartile, Second Quartile, Third Quartile and Fourth Quartile" ?

1 ACCEPTED SOLUTION

Accepted Solutions
SAS Employee

## Re: Boxplot Help

Formats will help here! You'll want to create a format and informat as follows:

``````proc format;
invalue invqrtl   'First Quartile'  = 1
'Second Quartile' = 2
'Third Quartile'  = 3
'Fourth Quartile' = 4;	value qrtl  1='First Quartile'
2='Second Quartile'
3='Third Quartile'
4='Fourth Quartile';
run;

``````

The informat will allow you to convert your character variable Quartiles into a numeric variable Quartiles_num which you'll use for the graph. Since it's numeric, the plot will automatically order these in the right order (1,2,3,4) and you'll apply format qrtl. to display these as "First Quartile", "Second Quartile",...

The code you'll want to include in a data step to prep the data is as follows:

``````data ....;
.....;
Quartiles_num=input(Quartiles,invqrtl.);
format Quartiles_num qrtl.;
....;
run;``````

The only change to your PROC SGPLOT is to use Quartiles_num instead of Quartiles.

2 REPLIES 2
SAS Employee

## Re: Boxplot Help

Formats will help here! You'll want to create a format and informat as follows:

``````proc format;
invalue invqrtl   'First Quartile'  = 1
'Second Quartile' = 2
'Third Quartile'  = 3
'Fourth Quartile' = 4;	value qrtl  1='First Quartile'
2='Second Quartile'
3='Third Quartile'
4='Fourth Quartile';
run;

``````

The informat will allow you to convert your character variable Quartiles into a numeric variable Quartiles_num which you'll use for the graph. Since it's numeric, the plot will automatically order these in the right order (1,2,3,4) and you'll apply format qrtl. to display these as "First Quartile", "Second Quartile",...

The code you'll want to include in a data step to prep the data is as follows:

``````data ....;
.....;
Quartiles_num=input(Quartiles,invqrtl.);
format Quartiles_num qrtl.;
....;
run;``````

The only change to your PROC SGPLOT is to use Quartiles_num instead of Quartiles.

Super User

## Re: Boxplot Help

Notice that the quartiles are in alphabetical order.

While I would recommend use of an actual numeric value with a format as @antonbcristina shows in the long run another approach would be to change the values to something like 1st Quartile, 2nd Quartile, 3rd Quartile and 4th Quartile. The order is still alphabetical but the order would be 1st 2nd 3rd 4th.

GROUPORDER doesn't have much affect without a GROUP= variable.

If your data is in quartile order for the Category variable you want the DISCRETEORDER option.

Or use a different format for the variable to change the display order.

From the documentation for VBOX

The default ordering of the tick values is ascending formatted order of the category-variable. To change this ordering, you can use the DISCRETEORDER=DATA option in the category axis statement

Discussion stats
• 2 replies
• 1033 views
• 2 likes
• 3 in conversation