Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

How do I count the number of columns with valid values per row?

Reply
Occasional Contributor
Posts: 7

How do I count the number of columns with valid values per row?

IDICD1ICD2ICD3ICD4ICD5
1001808.1201101.9003922
1002421.0556707.3997
1003787.2308599.8

I have 5 ICD codes in 5 columns, they are character variables. I want to be able to count how many valid values each subject has in the 5 ICD columns. So, for 1001, the result would be 5, for 1002 it will be 4 and 1003 will be 3. Or we could see it as the number of missing values in an array of variables per subject. Thank you.

Super User
Posts: 10,483

Re: How do I count the number of columns with valid values per row?

in a data step:

data want;

     set have;

     numvalid = n( of ICD1-icd5);

run;

The function NMISS with the same basic syntax would give the missing per record.

Valued Guide
Posts: 858

Re: How do I count the number of columns with valid values per row?

for character variables you would use the function cmiss().

For count of not missing:

data want;

set have;

valid = 5 - cmiss(of icd1-icd5);

run;

or for count of missing:

data want;

set have;

valid = cmiss(of icd1-icd5);

run;

Occasional Contributor
Posts: 7

Re: How do I count the number of columns with valid values per row?

This works! Thank you! I also tried the longer method that seems to work.

array _icdvar ICD1-ICD5;

     cnticd=0;

     do i=1 to 5;

     if missing(_icdvar(i)) = 0 then cnticd=cnticd+1;
end;

drop i;

Ask a Question
Discussion stats
  • 3 replies
  • 360 views
  • 2 likes
  • 3 in conversation