SAS Box-Plot/Tukey Method of Capping Outliers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*Calculate IQR and first/third quartiles; | |
proc means data=sashelp.class stackods n qrange p25 p75; | |
var weight height; | |
ods output summary=ranges; | |
run; | |
*create data with outliers to check; | |
data capped; | |
set sashelp.class; | |
if name='Alfred' then weight=220; | |
if name='Jane' then height=-30; | |
run; | |
*macro to cap outliers; | |
%macro cap(dset=,var=, lower=, upper=); | |
data &dset; | |
set &dset; | |
if &var>&upper then &var=&upper; | |
if &var<&lower then &var=&lower; | |
run; | |
%mend; | |
*create cutoffs and execute macro for each variable; | |
data cutoffs; | |
set ranges; | |
lower=p25-3*qrange; | |
upper=p75+3*qrange; | |
string = catt('%cap(dset=capped, var=', variable, ", lower=", lower, ", upper=", upper ,");"); | |
call execute(string); | |
run; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment