- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Proc sql;
Create table want as
Select survey, subject, count (*) as count_var
From have
Where answer is null;
Quit;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
<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!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Please post sample test data (in the form of a datastep) and required output otherwise code will just be guessing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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>