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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller
ballardw
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 569 views
  • 4 likes
  • 3 in conversation