BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lmtamina
Obsidian | Level 7

Hi there,

I would like to ask for assistance on how to  write the SAS code to count number of variables with values.

I have the following variables in my table : Diagnosis1,Diagnosis2, Diagnosis3, Diagnosis4, Diagnosis5

And I would like to count the number of variables only if they have a value.

For example, if I only have values for Diagnosis1 , Diagnosis2 and Diagnosis3 , and there are NO values for Diagnosis4 and Diagnosis5  , my diagnosis count will be  = 3 .

Greatly appreciate any assistance.

Thank you in advance.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

In a SAS data step, use the N() function, something like this will work:

 

data two;
    set one; 
    count=n(of diagnosis1-diagnosis5);
run;

 

 

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

In a SAS data step, use the N() function, something like this will work:

 

data two;
    set one; 
    count=n(of diagnosis1-diagnosis5);
run;

 

 

--
Paige Miller
ballardw
Super User

Are your variables numeric or character?

If the values are numeric you can use the N function

 

diagnosis_count = n (diagnosis1,diagnosis2,diagnosis3,diagnosis4,diagnosis5);

PaigeMiller
Diamond | Level 26

If the values are character, you can use the CMISS() Function to count the missings and then the number of non-missing is

 

count = 5 - cmiss(of diagnosis1-diagnosis5);
--
Paige Miller
lmtamina
Obsidian | Level 7
Thank you all! The variable type is character so I used the CMISS option to count. Appreciate all your expertise.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 3470 views
  • 5 likes
  • 3 in conversation