Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- How do you impose an 'excepted value' for outliers?

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 01-16-2021 09:47 AM
(748 views)

If I have outliers in my variable. How can I program SAS to find those outliers and make them the average of the previous value and the subsequent value? For example,

if malaria cases are 1714 for Feb 2018 I want SAS to take the average between Jan 2018 (200 cases) and March 2018 (47 cases).

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Here's a way to average the prior and post values. You still have to decide what makes a value an outlier and incude that logic in the program.

```
data want;
merge have have (obs=2 keep=variable rename=(variable=subsequent));
prior = lag(variable);
if /* your logic determines that current VARIABLE value is an outlier */ then do;
if _n_=1 then capped_value = subsequent;
else capped_value = mean(subsequent, prior);
end;
else capped_value = variable;
run;
```

Then use CAPPED_VALUE instead of VARIABLE.

3 REPLIES 3

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Here's a way to average the prior and post values. You still have to decide what makes a value an outlier and incude that logic in the program.

```
data want;
merge have have (obs=2 keep=variable rename=(variable=subsequent));
prior = lag(variable);
if /* your logic determines that current VARIABLE value is an outlier */ then do;
if _n_=1 then capped_value = subsequent;
else capped_value = mean(subsequent, prior);
end;
else capped_value = variable;
run;
```

Then use CAPPED_VALUE instead of VARIABLE.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thank you. Can you please explain what datasets I would put in the 2 'have' part and what 'keep 2 observations' means?

What can be place in the logic if I want the outlier to be +/- 3 SD from mean?

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

"Have" is the name of the data set that you have that contains all the data.

The obs=2 tells SAS to start reading that data set with the second observation. So one "have" starts at the first observation, and the other "have" reference starts at the second observation, thus getting the subsequent value for your variable.

Are you ready for the spotlight? We're accepting content ideas for **SAS Innovate 2025** to be held May 6-9 in Orlando, FL. The call is **open **until September 16. Read more here about **why** you should contribute and **what is in it** for you!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.