- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello all,
The mean function should consider the number of non-missing values to calculate the mean, but in my case, it considers the number of missing values as well; how can I fix this issue?
Mean(of var1 var2 ……)
Thank you!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You could replace these missing value with zeros by a simple PROC STDIZE, before using MEAN()
data have;
set sashelp.class;
if _n_ in (2:8) then call missing(height);
run;
proc stdize data=have out=want missing=0 reponly;
var _numeric_;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Can you show an example that illustrates the problem? As you say, the mean function is smart enough to ignore missing values. So perhaps you don't have missing values, or there is a mistake in your code.
data have ;
input x y z ;
cards ;
1 2 .
;
data want ;
set have ;
mymean=mean(x,y,z) ;
put mymean= ;
run ;
Returns
375 data want ; 376 set have ; 377 mymean=mean(x,y,z) ; 378 put mymean= ; 379 run ; mymean=1.5
Next up: SAS Trivia Quiz hosted by SAS on Wednesday May 21.
Register now at https://www.basug.org/events.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@bhr-q wrote:
Hello all,
The mean function should consider the number of non-missing values to calculate the mean, but in my case, it considers the number of missing values as well; how can I fix this issue?
Mean(of var1 var2 ……)
Thank you!
Example values and code or it didn't happen...
I've only been using SAS since 1987 off-an-on and you have to work to force SAS to consider "missing" values in Mean calculations. Perhaps you have some odd convention like -99 or 0 that you think is missing. SAS won't. It has a missing (actually 28 missing values) that would not be included and is typically displayed as "." by default. If your values are anything else they aren't missing.
For anyone reading this in future please note the question included in the original post, as seen in the quoted area above, is nothing like what the response marked as "correct" addresses.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@bhr-q wrote:
Hello all,
The mean function should consider the number of non-missing values to calculate the mean, but in my case, it considers the number of missing values as well; how can I fix this issue?
Mean(of var1 var2 ……)
Thank you!
Hello @bhr-q , no one knows your data and no one knows the code you are using. As a general rule, provide example data and the code with the problem in your very first post from now on. You will get faster answers and better help if you provide example data and your code in the first message of the thread.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The mean()
function in SAS already ignores missing values by default, so it should only consider non-missing values.
If you're still encountering issues, double-check if your missing values are properly represented as .
(for numeric data). You don’t need to modify the mean()
function; it automatically excludes missing values.
Here’s the correct usage:
mean(of var1 var2 ...);
To check for missing values, use:
proc means data=yourdata nmiss;
var var1 var2 ...;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@PaigeMiller wrote:
@bhr-q wrote:
sorry, my question wasn't clearOk, @bhr-q , apparently you have received a correct answer, but I have no idea what question it is answering. Could you make that clear?
Based on the answer and re-reading the question, OP wants to INCLUDE missing values in the mean function - so he wants to have them considered as 0, but the default behaviour is to exclude these values. He wants them included in the calculations.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@Reeza wrote:
@PaigeMiller wrote:
@bhr-q wrote:
sorry, my question wasn't clearOk, @bhr-q , apparently you have received a correct answer, but I have no idea what question it is answering. Could you make that clear?
Based on the answer and re-reading the question, OP wants to INCLUDE missing values in the mean function - so he wants to have them considered as 0, but the default behaviour is to exclude these values. He wants them included in the calculations.
Is that correct, @bhr-q ? Treating missings as zeros is a very dangerous thing to do, and should be avoided unless you have very strong subject matter justification for doing so. Anyway, that's up to you, we don't need to discuss it further.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
yes, I just wanted to include missing as their request to make some comparison; that's it, not as a final result; you are totally correct; that would be dangerous, thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You could replace these missing value with zeros by a simple PROC STDIZE, before using MEAN()
data have;
set sashelp.class;
if _n_ in (2:8) then call missing(height);
run;
proc stdize data=have out=want missing=0 reponly;
var _numeric_;
run;