BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
mayasak
Quartz | Level 8

Hi,

 

I'm using the following code to print the outliers in my data. 

proc print data=MDRO_Report_2021 noobs;
where abs(susceptibility-ps_mean) > 2*ps_std ;
by drug_bug;
var hospital_DB drug bug isolates_hospital_n isolates_tested susceptibility isolates_susceptible;
run;

I still need to remove these data from the data source so that it won't mess up my analysis.

Is there a way to create a dataset that is free from those outliers?

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

Here's an analog using variable MSRP from sashelp.cars.  It has three steps (generate mean and std, generate upper and lower limits into macro variables, apply the WHERE filter:

 

proc univariate data=sashelp.cars noprint;
  var msrp;
  output out=need mean=msrp_mean  std=msrp_std ;
run;

proc sql noprint;
  select msrp_mean - 2*msrp_std ,msrp_mean + 2*msrp_std
    into :lower_limit           ,:upper_limit
  from need;
quit;

proc print data= sashelp.cars;
  where &lower_limit <= msrp <= &upper_limit;
run;

You might prefer to do a more non-symmetrical (no-paframetric?) outlier filter:

 

proc univariate data=sashelp.cars noprint;
  var msrp;
  output out=need p5=p5 p95=p95;
run;

proc sql noprint;
  select p5           , p95 
    into :lower_limit ,:upper_limit
  from need;
quit;

proc print data= sashelp.cars;
  where &lower_limit <= msrp <= &upper_limit;
run;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

4 REPLIES 4
mkeintz
PROC Star

Here's an analog using variable MSRP from sashelp.cars.  It has three steps (generate mean and std, generate upper and lower limits into macro variables, apply the WHERE filter:

 

proc univariate data=sashelp.cars noprint;
  var msrp;
  output out=need mean=msrp_mean  std=msrp_std ;
run;

proc sql noprint;
  select msrp_mean - 2*msrp_std ,msrp_mean + 2*msrp_std
    into :lower_limit           ,:upper_limit
  from need;
quit;

proc print data= sashelp.cars;
  where &lower_limit <= msrp <= &upper_limit;
run;

You might prefer to do a more non-symmetrical (no-paframetric?) outlier filter:

 

proc univariate data=sashelp.cars noprint;
  var msrp;
  output out=need p5=p5 p95=p95;
run;

proc sql noprint;
  select p5           , p95 
    into :lower_limit ,:upper_limit
  from need;
quit;

proc print data= sashelp.cars;
  where &lower_limit <= msrp <= &upper_limit;
run;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
mayasak
Quartz | Level 8

Thank you mkeintz, how can I export it to an Excel worksheet?

mayasak
Quartz | Level 8

Thank you a lot.

ballardw
Super User

It may help to define what you mean by 'remove'. Do want to delete an entire observation? Set variables to missing? Something else?

 

Example deleting entire observations when an outlier, assuming that is what your Where actually finds:

Data want; 
   set MDRO_REport_2021;
   where not (abs(susceptibility-ps_mean) > 2*ps_std);
run;

Or to set variables missing:

Data want; 
   set MDRO_REport_2021;
    if abs(susceptibility-ps_mean) > 2*ps_std then call missing(<comma delimited list of variable names goes here>);
run;

If remove is something other than this, describe.

 

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 4 replies
  • 847 views
  • 2 likes
  • 3 in conversation