BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
fengyuwuzu
Pyrite | Level 9

I want to remove outliers using median +/- 1.5 IQR (Qrange in SAS).

The proc univariate can generate median and Qrange, but how do I use these values in another proc or data step?

 

Another way is to use proc sql, but it seems proc sql summary function does not have qrange

 

or proc boxplot can also generate median and qrange, but I still need to use these values in another step.

 

1 ACCEPTED SOLUTION

Accepted Solutions
rayIII
SAS Employee

You could save the median and iqr as macro variables, apply them in a Data step: 

 

proc univariate data = sashelp.iris;
var petallength;
output out=boxStats median=median qrange = iqr;
run; 

data _null_;
	set boxStats;
	call symput ('median',median);
	call symput ('iqr', iqr);
run; 

%put &median;
%put &iqr;

data trimmed;
set sashelp.iris;
    if (petallength le &median + 1.5 * &iqr) and (petallength ge &median - 1.5 * &iqr); 
run; 

proc print data = trimmed;
run; 

View solution in original post

6 REPLIES 6
Reeza
Super User

Use proc means, capture the data in a dataset and merge that into the main data set. 

 

If you have SAS 9.4 proc SQL supports the median function otherwise it doesn't. 

fengyuwuzu
Pyrite | Level 9
Thank you, Reeza. I do have SAS 9.4; I will check if it supports qrange.
rayIII
SAS Employee

You could save the median and iqr as macro variables, apply them in a Data step: 

 

proc univariate data = sashelp.iris;
var petallength;
output out=boxStats median=median qrange = iqr;
run; 

data _null_;
	set boxStats;
	call symput ('median',median);
	call symput ('iqr', iqr);
run; 

%put &median;
%put &iqr;

data trimmed;
set sashelp.iris;
    if (petallength le &median + 1.5 * &iqr) and (petallength ge &median - 1.5 * &iqr); 
run; 

proc print data = trimmed;
run; 
fengyuwuzu
Pyrite | Level 9
Thanks. This works.
What I did is: I used proc means to output median and qrange to a new data set, and use proc sql to query my data and this new dataset with median and qrange, and do comparisons with b.median+1.5*b.qrange ...
stat_sas
Ammonite | Level 13

Hi,

 

Please try this:

 

proc univariate data=sashelp.class;
var age;
output out=stats median=median Qrange=Qrange;
run;

 

data want(drop=median Qrange);
if _n_=1 then set stats;
set sashelp.class;
if age<median - 1.5*Qrange or age>median + 1.5*Qrange then delete;
run;

fengyuwuzu
Pyrite | Level 9
Thank you, Stat_SAS. I thought I need to merge them, but your solution gave me new idea. I will test. Thank you.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 13746 views
  • 5 likes
  • 4 in conversation