BookmarkSubscribeRSS Feed
dht115
Calcite | Level 5

I need to calculate outlier (2 standard deviation). To get the desire output, I want to calculate mean and standard deviation by using the group and condition. 

 

If condition match, I want to assign that value for group (or I want to use that value to calculate lower and upper limit) 

Lower_limit = mean - 2 * sd

Upper_limit = mean + 2 * sd

If var2 is less than lower limit or greater than upper limit than I want to delete that observation from my dataset. 

 

This is what I want: 

var1 

var1var2meansdlower_limitupper_limit
b13.43.97-4.5411.34
b103.43.97-4.5411.34
b23.43.97-4.5411.34
b43.43.97-4.5411.34
b03.43.97-4.5411.34
c3....
c3....
c2....
c1....
c1....

 

In this example, I do not have any demand less than lower limit or greater than upper limit. So I need to keep all observation. 

 

data have; 

input var1 var2; 

datalines; 

b 1 

b 10 

b 2

b 4

b 0

c 3

c 3

c 2

c 1

c 1

;

run;

 

proc sql; 

create table want as 

select*, avg(var2) as mean format 10.3, std(var2) as sd format 10.3

from have

group by var1

having var2 > 4;

quit

 

*cannot get the desire (same) value by group. 

 

*delete observations based on lower and upper limit creteria. 

data want;

set want;

lower_limit = mean - (2*sd);

upper_limit = mean + (2*sd); 

if demand = . then output;

if demand < lower_limit then delete;

if demand >  upper_limit then delete;

run;

3 REPLIES 3
PaigeMiller
Diamond | Level 26

What is your question?

--
Paige Miller
dht115
Calcite | Level 5

How to calculate mean and standard deviation based on the condition by group?

PaigeMiller
Diamond | Level 26
data have; 
input group $ var2; 
datalines; 
b 1 
b 10 
b 2
b 4
b 0
c 3
c 3
c 2
c 1
c 1
;
proc summary nway data=have;
    class group;
    var var2;
    output out=_stats_ mean=mean stddev=sd;
run;
data want;
    if _n_=1 then set _stats_(drop=_:);
    set have;
    /* If you want the confidence limits, you type the formula here */
run;
--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 791 views
  • 0 likes
  • 2 in conversation