BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
bhr-q
Pyrite | Level 9

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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;

View solution in original post

11 REPLIES 11
Quentin
Super User

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
The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
bhr-q
Pyrite | Level 9
sorry, my question wasn't clear
ballardw
Super User

@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.

PaigeMiller
Diamond | Level 26

@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
saiteja_reddy
Calcite | Level 5

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;

bhr-q
Pyrite | Level 9
sorry, my question wasn't clear
PaigeMiller
Diamond | Level 26

@bhr-q wrote:
sorry, my question wasn't clear

Ok, @bhr-q , apparently you have received a correct answer, but I have no idea what question it is answering. Could you make that clear?

--
Paige Miller
Reeza
Super User

@PaigeMiller wrote:

@bhr-q wrote:
sorry, my question wasn't clear

Ok, @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. 

PaigeMiller
Diamond | Level 26

@Reeza wrote:

@PaigeMiller wrote:

@bhr-q wrote:
sorry, my question wasn't clear

Ok, @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
bhr-q
Pyrite | Level 9

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. 

Ksharp
Super User

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;

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!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 11 replies
  • 1270 views
  • 5 likes
  • 7 in conversation