generating average with missing more than X values


New Member
hi all,

let's say i want to take the average of 5 scores for class tests for each student in a class. the data looks like the below EXCEPT averagescore. That is the column I want to generate!

student test1 test2 test3 test4 test5 averagescore
1 7 8 NA 7 9 7.75
2 5 5 NA NA 9 NA
3 9 9 9 9 9 9

how do i dictate NA in aveagescore if 2 OR MORE scores are missing? thank you!


Less is more. Stay pure. Stay poor.
Is your question, can I still calculate average with missing data; How do I get rid of the NAs, etc. I am unclear of your intention/goal.


New Member
I want to create averages for each student AND if a student has 2 or more missing test scores I want there average score to be NA. Thank you!


New Member
I'm late here, but if I understand correctly, you could create a variable giving you the number of missing for your 5 variables, using egen; I will call this variable averagescore_miss. Then you can create your averagescore variable, still using egen, adding the condition that you want with if. So, it should look like this:

egen averagescore_miss = rowmiss (test1 test2 test3 test4 test5)
egen ageragescore = rowmean (test1 test2 test3 test4 test5) if averagescore_miss < 2

You should double check to be sure that I didn't make a mistake.