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
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;
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.
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;
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;
The array is exactly what I was looking for and it worked perfectly. Thanks so much!
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!
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.