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

Hi all,

 

First time poster, long time forum user.  All the advice provided has been super helpful.  I am currently using SAS 9.3.

 

I am trying to analyze the number of symptoms individuals experienced but the types of symptoms are in different columns, for example:

 

Cough  Fatigue    Fever    Headache    Rash

  Yes         Yes          No         Yes          No

   No         Yes          No          No           No

  Yes          No          No          No           Yes

   No          No          No          No            No

 

 

I want to know how many symptoms each individual experienced, but I am having a hard time thinking of a way to count the number of times "yes" appears across these variables without creating many if/then statements and a new variable. Like:

IF FATIG="Yes" AND FVR="No" AND HDCH="No" AND COUGH="No" AND RASH="No" THEN NUM_SXS=1;

Is there an easier way to count the number of times "Yes" appears across the row?  I don't necessarily need there to be a new variable, although it would be helpful later down the line.

 

Cough  Fatigue    Fever    Headache    Rash  Num_Symptoms

  Yes         Yes          No         Yes          No             3

   No         Yes          No          No           No              1

  Yes          No          No          No           Yes             2

   No          No          No          No            No              0

 

 

 

 Any help is greatly appreciated.

 

Thank you

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

For a more complex scenario, where a variable might contain "Yoyo" or "Yesterday", you could also use:

 

n_yes = (cough="Yes") + (fatigue="Yes") + (fever="Yes") + (headache="Yes") + (rash="Yes");

 

If there are a lot of symptoms, you could throw them into an array:

 

array symptoms {5} cough fatigue fever headache rash;

n_yes=0;

do i=1 to 5;

   if symptoms{i}="Yes" then n_yes + 1;

end;

View solution in original post

5 REPLIES 5
Reeza
Super User

In this case either consider transposing your data to a long format and then it's a straightforward proc freq. 

Otherwise, use an array to loop through the diagnosis and count the number of Yes. 

rogerjdeangelis
Barite | Level 11
HAVE

Up to 40 obs WORK.HAVE total obs=4

Obs    COUGH    FATIGUE    FEVER    HEADACHE    RASH

 1      Yes       Yes       No        Yes       No
 2      No        Yes       No        No        No
 3      Yes       No        No        No        Yes
 4      No        No        No        No        No


WANT (cout of 'Yes' by row)

Up to 40 obs WORK.WANT total obs=4

Obs    COUGH    FATIGUE    FEVER    HEADACHE    RASH    CNTYES

 1      Yes       Yes       No        Yes       No         3
 2      No        Yes       No        No        No         1
 3      Yes       No        No        No        Yes        2
 4      No        No        No        No        No         0

FULL SOLUTION
==============

data have;
input (Cough Fatigue Fever Headache Rash) (: $3.);
cards4;
Yes Yes No  Yes No
No  Yes No  No  No
Yes No  No  No  Yes
No  No  No  No  No
;;;;
run;quit;

data want;
  set have;
  cntyes=countc(cats(of _character_),'Y');
run;quit;

Astounding
PROC Star

For a more complex scenario, where a variable might contain "Yoyo" or "Yesterday", you could also use:

 

n_yes = (cough="Yes") + (fatigue="Yes") + (fever="Yes") + (headache="Yes") + (rash="Yes");

 

If there are a lot of symptoms, you could throw them into an array:

 

array symptoms {5} cough fatigue fever headache rash;

n_yes=0;

do i=1 to 5;

   if symptoms{i}="Yes" then n_yes + 1;

end;

jecol72
Fluorite | Level 6

The array is exactly what I was looking for and it worked perfectly.  Thanks so much!

jecol72
Fluorite | Level 6

This was a great option!  I left off that I have a bunch of other variables in my data that have "yes" as answers as well and I was having a hard time limiting the count to just those variables without doing a new dataset.  I will keep this in mind for future reference though, thank you!

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
  • 5 replies
  • 2163 views
  • 7 likes
  • 4 in conversation