Solved
Contributor
Posts: 40

# Drop variables if mean = 0

I have a dataset of 1000 plus variables - all numeric. Not all of them are useful. I wanted to drop the variables where the mean if less than a threshold value. I thought of two approaches but couldn't execute either as I am a complete beginner in SAS.

Approach 1: Use proc means to calculate mean of all variables in input dataset x and output it to another dataset y. Then write a step which checks dataset y where mean = 0 and matches the name with dataset x. if match = positive then delete.

Approach 2: Is there a way to write if mean of var = 0, then delete var.

thanks.

Accepted Solutions
Solution
‎01-13-2015 03:03 PM
Super User
Posts: 23,661

## Re: Drop variables if mean = 0

This is a variant on a frequently asked question - how to drop variables that all have missing values.

Change the code slightly here in the first step will get you what you want. Replace the _nobs_ with _mean_

*generate sample data;

data class;

set sashelp.class;

new_var=0;

run;

proc univariate data=class outtable=ot(keep=_var_ _mean_) noprint;

var _numeric_;

run;

proc sql noprint;

select _var_ into :vList separated by " "

from ot where _mean_=0;

quit;

data clean;

set class;

drop &vList;

run;

All Replies
Solution
‎01-13-2015 03:03 PM
Super User
Posts: 23,661

## Re: Drop variables if mean = 0

This is a variant on a frequently asked question - how to drop variables that all have missing values.

Change the code slightly here in the first step will get you what you want. Replace the _nobs_ with _mean_

*generate sample data;

data class;

set sashelp.class;

new_var=0;

run;

proc univariate data=class outtable=ot(keep=_var_ _mean_) noprint;

var _numeric_;

run;

proc sql noprint;

select _var_ into :vList separated by " "

from ot where _mean_=0;

quit;

data clean;

set class;

drop &vList;

run;

🔒 This topic is solved and locked.