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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 414 views
  • 2 likes
  • 3 in conversation