Created
September 24, 2019 10:57
-
-
Save metaodi/a64eae63b8b4938347c87abcc81ba224 to your computer and use it in GitHub Desktop.
A SAS program to detect outliers using IQR (interquartile range)
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
/* Caluculate quartiles to get limits */ | |
proc univariate data=work.ZaehlstelleZeit_&num. noprint; | |
var AnzFahrzeuge; | |
output out=work.ZaehlstelleStat_&num. p25=q1 p75=q3; | |
run; | |
data work.ZaehlstelleStat_&num.; | |
set work.ZaehlstelleStat_# | |
ZSID = "&name"; | |
iqr = q3 - q1; | |
iqr_factor = 3; /* iqr_factor 1.5 is used for "mild" outliers, 3 for extreme outlier */ | |
q3_limit = q3 + (iqr * iqr_factor); | |
q1_limit = q1 - (iqr * iqr_factor); | |
drop iqr iqr_factor q1 q3; | |
run; | |
/* Merge dataset with statistics */ | |
data work.ZaehlstelleZeit_&num.; | |
merge work.ZaehlstelleZeit_&num. work.ZaehlstelleStat_&num.; | |
by ZSID; | |
MessungStatus='Messwert'; | |
format MessungStatus $20.; | |
if missing(AnzFahrzeuge) then do; | |
MessungStatus='Fehlend'; | |
end; | |
else if AnzFahrzeuge > q3_limit OR AnzFahrzeuge < q1_limit then do; | |
MessungStatus='Suspekt'; | |
end; | |
drop q3_limit q1_limit | |
run; | |
/* Get rid of stat table */ | |
proc datasets library=work nolist; | |
delete ZaehlstelleStat_&num.; | |
quit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment