Here's the scenario:
data sample;
input x;
datalines;
1
2
.
4
run;
proc sql;
select count(*) from sample; /*returns 4*/
select count(x) from sample; /*returns 3*/
quit;
Is there a way to count that missing value for x?
Is there a built in variable in proc sql that represents the total number of observations?
Either just use subtraction:
proc sql;
select count(*) as N
, count(x) as NX
, count(*)-count(x) as MISSX
from sample
;
quit;
or SUM() a boolean expression. SAS evaluates boolean expressions to 1 or 0.
proc sql;
select count(*) as N
, count(x) as NX
, sum(missing(x)) as MISSX
from sample
;
quit;
Use the NMISS function
proc sql; select nmiss(x) from sample; /*returns 1*/ quit;
Very elegant answer.
So, it appears and confirmed after testing, that count(var) does not count missing values of var.
Either just use subtraction:
proc sql;
select count(*) as N
, count(x) as NX
, count(*)-count(x) as MISSX
from sample
;
quit;
or SUM() a boolean expression. SAS evaluates boolean expressions to 1 or 0.
proc sql;
select count(*) as N
, count(x) as NX
, sum(missing(x)) as MISSX
from sample
;
quit;
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.