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;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.