Solved
New Contributor
Posts: 3

# SAS version of Excel's count ifs/then statement

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:

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

Accepted Solutions
Solution
‎03-14-2017 11:37 AM
Super User
Posts: 6,772

## Re: SAS version of Excel's count ifs/then statement

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;

All Replies
Super User
Posts: 23,721

## Re: SAS version of Excel's count ifs/then statement

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.

Valued Guide
Posts: 505

## Re: SAS version of Excel's count ifs/then statement

``````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;

``````
Solution
‎03-14-2017 11:37 AM
Super User
Posts: 6,772

## Re: SAS version of Excel's count ifs/then statement

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;

New Contributor
Posts: 3

## Re: SAS version of Excel's count ifs/then statement

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

New Contributor
Posts: 3