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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.