DATA Step, Macro, Functions and more

How to sort boxplots by the median

Reply
Regular Learner
Posts: 1

How to sort boxplots by the median

Dear all,

 

I am new to SAS (coming from R) and i would like to sort boxplot by the median of the variable being plotted. 

The code i have generated using SAS Enterprise is the following:

 

 

Spoiler
PROC SQL;
CREATE VIEW WORK.SORTTempTableSorted AS
SELECT T.Country, T.BUFF
FROM WORK.QUERY_FOR_PROFITABILITY_MI as T
WHERE BUFF BETWEEN -5 AND 5
;
QUIT;
SYMBOL1 INTERPOL=BOXTJF VALUE=CIRCLE
HEIGHT=1
MODE=EXCLUDE
;
Axis1
STYLE=1
WIDTH=1
MINOR=NONE

;
Axis2
STYLE=1
WIDTH=1
MINOR=NONE

VALUE=(ANGLE=90)
;
TITLE;
TITLE1 "Box Plot";
FOOTNOTE;
FOOTNOTE1 "Generated by the SAS System (&_SASSERVERNAME, &SYSSCPL) on %TRIM(%QSYSFUNC(DATE(), NLDATE20.)) at %TRIM(%SYSFUNC(TIME(), TIMEAMPM12.))";
PROC GPLOT DATA=WORK.SORTTempTableSorted
;
PLOT BUFF * Country/
VAXIS=AXIS1

HAXIS=AXIS2

GRID
;
/* -------------------------------------------------------------------
End of task code.
------------------------------------------------------------------- */
RUN; QUIT;
%_eg_conditional_dropds(WORK.SORTTempTableSorted);
TITLE; FOOTNOTE;
GOPTIONS RESET = SYMBOL;%SYMDEL _CLIENTTASKFILTER;

 

 

Is there a way to modify such code in order to get the desired order of the boxplots? 

 

Thanks for any help!

 

Occasional Contributor
Posts: 11

Re: How to sort boxplots by the median

Hello,
Can i know whether did you find out the median of the variable which you want to plot? You can use Proc univariate or Proc means in order to get a median of variable.
Super User
Posts: 10,044

Re: How to sort boxplots by the median

Super User
Posts: 10,044

Re: How to sort boxplots by the median

[ Edited ]

Give you an example. If you have SAS9.4 ,that would be more easy by using SQL.

 

 

 

data class;
 set sashelp.heart;
 array x{*} _numeric_;
 do i=1 to dim(x);
  vname=vname(x{i});
  value=x{i};
  output;
 end;
 keep vname value;
run;
proc sort data=class;by vname;run;
proc summary data=class;
 by vname;
 var value;
 output out=median(drop=_:) median=median;
run;
data want;
 merge class median;
 by vname;
run;
proc sort data=want;by median;run;

proc sgplot data=want ;
 vbox value/category=vname;
 xaxis discreteorder=data;
run;

 

 

x.png

Ask a Question
Discussion stats
  • 3 replies
  • 410 views
  • 0 likes
  • 3 in conversation