Hi all,
I have a dataset: q22_1 through q22_9 where I need to count whether an individual answered "Y=Yes" to 5 of the 9 questions asked. They would be divided into one group and the ones that answered 4 or less would go into another. Does anyone know who to do this? Thanks!
If your variables contain 0 and 1, then all you have to do is sum the values and see if the sum is 5 or greater. That would be the easiest approach. (If the values are not 0 and 1, then you might want to consider gathering data in this form in the future)
Nevertheless, if the value are Y or N then this would work
data want;
set have;
array q22(*) q22_1-q22_9;
sumY=0;
do i=1 to dim(q22);
if q22(i)='Y' then sumY=sumY+1;
end;
drop i;
run;
If your variables contain 0 and 1, then all you have to do is sum the values and see if the sum is 5 or greater. That would be the easiest approach. (If the values are not 0 and 1, then you might want to consider gathering data in this form in the future)
Nevertheless, if the value are Y or N then this would work
data want;
set have;
array q22(*) q22_1-q22_9;
sumY=0;
do i=1 to dim(q22);
if q22(i)='Y' then sumY=sumY+1;
end;
drop i;
run;
A @PaigeMiller indicates there are many reasons to code values as 1/0 instead of Yes/No, True/False.
Unfortunately some data sources may be provided in that format.
Fortunately you can read such into numeric values with a custom informat easily.
Example:
proc format; invalue YNTF (upcase) 'Y','YES','T','TRUE' = 1 'N','NO','F','FALSE' = 0 other= .; run; data example; input v1 : yntf.; datalines; y Y Yes yES T n/a F f t n No NO false fAlSe Fred ;
The proc format makes an informat to read several common ways of doing Yes/No or True/False data into 1/0 coded numeric variables. The option UPCASE turns what ever text is encountered when reading the field into upper case before comparing with the assignment values. This is very helpful if data is manually entered and the users can't be trained to actually spell with consistent case. Any value except those listed in this case is assigned missing.
A minor variation: other = _error_ would provide invalid data diagnostics when encountering other values, perhaps such as "Flase" or "Yse" and assigns a missing value.. You could adjust the informat to accept those unexpected values and reread the data instead of gyrations to "fix" values.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.