I need to create a new variable (count_var) that counts the number of questions that were left unanswered in a survey. The value of an unanswered question is 0. There are 10 questions in the survey.
Ideally, the value of count_var will equal the number of unanswered questions for each subject.
I was not abCreate an enumeration variable with a condition" post in this forum.
Thanks for your assistance!
If the values for non-answered had been treated as MISSING instead of assigned a value this would be trivial with the CMISS function.
I strongly recommend considering use of missing values instead of 0 as you cannot expect to get really accurate values for things like "average response" or most statistics with your current coding.
However Assuming the values are 1) actually numeric 2) one observation per respondent then something like
Data want;
set have;
array q q1-q10; /* assumes the questions are named Q1 Q2 etc. put list of actual names if significantly different*/
Count_var=0;
do i=1 to dim(q);
count_var= count_var + (q[i] = 0);
end;
drop i;
run;
If your variables are actually character then use = "0".
If we knew the range of of the responses it might be possible to get the result in a single line use COUNTC and a contactenation of the values, but insufficient information of the values was provided.
<Are each question/answer on separate observations?>
Yes, there are 10 questions. The values of the observations can be between 0 and 3.
I'm a relatively novice SAS user and have never used PROC SQL. A few clarifying questions:
<Create table want as>
This is creating a table with the desired items? What does the "as" command do?
<Select survey, subject, count (*) as count_var>
Is survey a list of variables that make up the survey?
<Where answer is null;>
How does SAS define "null"?
Thanks!!!
Please post sample test data (in the form of a datastep) and required output otherwise code will just be guessing.
If the values for non-answered had been treated as MISSING instead of assigned a value this would be trivial with the CMISS function.
I strongly recommend considering use of missing values instead of 0 as you cannot expect to get really accurate values for things like "average response" or most statistics with your current coding.
However Assuming the values are 1) actually numeric 2) one observation per respondent then something like
Data want;
set have;
array q q1-q10; /* assumes the questions are named Q1 Q2 etc. put list of actual names if significantly different*/
Count_var=0;
do i=1 to dim(q);
count_var= count_var + (q[i] = 0);
end;
drop i;
run;
If your variables are actually character then use = "0".
If we knew the range of of the responses it might be possible to get the result in a single line use COUNTC and a contactenation of the values, but insufficient information of the values was provided.
Thank you so much for your help. This code worked!
All I needed to do was add (*) to the ARRAY statement:
< array q (*) q1-q10>
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.