BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
kcvaldez98
Obsidian | Level 7

Hi SAS Coders!

 

I created a box plot that looks like this:

Screen Shot 2023-12-07 at 9.43.09 AM.png

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

ODS NOPROCTITLE;
PROC SGPLOT DATA=WORK.FIGURE;
  VBOX outcomes_adults_mental_distr / 
    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" ?

Please advise

1 ACCEPTED SOLUTION

Accepted Solutions
antonbcristina
SAS Super FREQ

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.

View solution in original post

2 REPLIES 2
antonbcristina
SAS Super FREQ

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.

ballardw
Super User

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

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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