Tuesday, September 11, 2012

Count missing value in a row

There are several functions in SAS that can count missing values in a row. They are nmiss(), cmiss(). The function n() counts non-missing values.  

The "n" functions only work on numeric variables, while "c" function works for both numeric and character variables. So if you are not sure your variables are numeric or character, or the variables are mixed, it is save to use the cmiss() function than to use "n" functions. 
The following is an example to show how the "n" functions failed when character variable is included. 
char num  n    nmiss cmiss cnmiss
A    1    1    1     0     2     (no missing)
B    .   0    2     1     1     (1 missing)
     3    1    1     1     1     (1 missing)
D    4    1    1     0     2     (no missing)
     .    0    2     2     0     (2 missing)
F    .    0    2     1     1     (1 missing)
                                            correct correct

The code :
data miss;
input char $1 num 3;
cards;
A 1
B .
  3
D 4
   
F  
;
run;
data miss2;
set miss;
n=n(char, num);
nmiss=nmiss(char, num);
cmiss=cmiss(char, num);
cnmiss=2-cmiss(char, num);
run;

No comments:

Post a Comment