DATA Step, Macro, Functions and more

SAS program to detect and display only outliers

Reply
Occasional Contributor
Posts: 10

SAS program to detect and display only outliers

Hello,

I want to know simple code how to detect and display only outliers

I do not know cutoff values.

Thanks

Best Regards

Agate

Re: SAS program to detect and display only outliers

This may be simplest to learn extreme values of your data.

Example:

title 'Extreme Blood Pressure Observations';

ods select ExtremeObs;

proc univariate data=BPressure;

  var Systolic Diastolic;

  id PatientID;

run;

Base SAS(R) 9.2 Procedures Guide: Statistical Procedures, Third Edition

Contributor
Posts: 57

Re: SAS program to detect and display only outliers

There are an infinite number of methods you could use.  Another simple method might be to classify any observation as an outlier if it falls above or below the outer fences of a boxplot:

Lower outer fence: Q1 - 3*IQ

Upper outer fence: Q3 + 3*IQ

Where IQ is the interquartile range, Q1 is the first quartile, and Q3 is the third quartile.

Occasional Contributor
Posts: 10

Re: SAS program to detect and display only outliers

Posted in reply to statistician13

Thanks for your reply.

What would be code for displaying only values above or below the outer fences of a boxplot.

I have tried to create the program but unsuccessfully.

Contributor
Posts: 57

Re: SAS program to detect and display only outliers

You could try something like this.  I'm sure there's more elegant code, but this is what I whipped up in a minute.  All you'd need to do is replace the values of dsn with your dataset name and VariableOfInterest with the variable name in the dsn dataset that you want to examine for outliers.

%let VariableOfInterest=height;

%let dsn=sashelp.class;

data work.temp;

set &dsn;

run;

proc univariate data=work.temp noprint;

var &VariableOfInterest;

output out=work.IQRData Q1=Q1 Q3=Q3 QRANGE=IQR;

run;

proc sql noprint;

select Q1-3*IQR, Q3+3*IQR into :lowerfence, :upperfence

from

work.IQRData;

quit;

%put Outliers are those observations less than &lowerfence and greater than &upperfence;

data work.outliers;

set work.temp;

if &VariableOfInterest gt &upperfence or height lt &lowerfence then output;

run;

Occasional Contributor
Posts: 10

Re: SAS program to detect and display only outliers

Posted in reply to statistician13

Thank you very much! That works perfect! Smiley Happy

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