BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
cosmid
Lapis Lazuli | Level 10

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

6 REPLIES 6
ballardw
Super User

Use the NMISS function

 

proc sql;
  select nmiss(x) from sample; /*returns 1*/
quit;
cosmid
Lapis Lazuli | Level 10
Wow! Thanks for the quick reply. I"m guessing there isn't a way to count a var that include its missing values? I thought there's something like a count_miss(x) that will return a 4:) Thanks for your help!
Steve_SAS
Fluorite | Level 6

Very elegant answer.

So, it appears and confirmed after testing, that count(var) does not count missing values of var.

Steve_SAS
Fluorite | Level 6
nmiss(x) is the elegant answer.
Tom
Super User Tom
Super User

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;
cosmid
Lapis Lazuli | Level 10
Wow! Thanks for the quick reply. I"m guessing there isn't a way to count a var that include its missing values? I thought there's something like a count_miss(x) that will return a 4:) Thanks for your help!

Sorry for duplicated messages. Would be nice if there's a reply to all:)

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 6 replies
  • 15556 views
  • 2 likes
  • 4 in conversation