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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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