计算多项选择题的正确数量

时间:2016-03-21 06:11:31

标签: sas data-manipulation

我有关于学生回答的问题的数据。格式是这样的

Student     Q1   Q2  Q3    Q4
A            1   3   2    3
B            2   3   2    2    
C            1   2   1    2
D            3   3   1    2

对于这个例子,假设1是问题1的正确答案,2是问题2,3和4的正确答案。

我如何生成一个统计表,告诉我学生正确回答了多少问题?在上面的例子中,它会说像

Student    Answered Correct:
A          2/4

3 个答案:

答案 0 :(得分:2)

您可以创建正确答案的数组,然后循环学生答案以进行比较。

我已将最终变量创建为要以您显示的格式显示的字符。显然,这意味着您将无法访问基础值,因此您可能希望在数据中保留正确答案的数量以用于其他分析目的。

data have;
input Student $ Q1 Q2 Q3 Q4;
datalines;
A 1 3 2 3
B 2 3 2 2
C 1 2 1 2
D 3 3 1 2
;
run;

data want;
set have;
array correct{4} (1 2 3 4); /* create array of correct answers */
array answer{4} q1-q4; /* create array of student answers */
_count=0; /* reset count to 0 */
do i = 1 to dim(correct);
    if answer{i} = correct{i} then _count+1; /* compare student answer to correct answer and increment count by 1 if they match */
end;
length answered_correct $8; /* set length for variable */
answered_correct = catx('/',_count,dim(correct)); /* display result in required format */
drop q: correct: i _count; /* drop unwanted variables */
run;

答案 1 :(得分:0)

首先,您必须创建变量num_questions并将其设置为问题数。然后,您需要编写尽可能多的if-then-else语句作为问题来创建二进制变量(标志)以检查每个答案是否正确(例如Correct_Q1)。使用sum(of Correct:)获取每个学生的正确答案总数。 Correct:引用以'Correct'开头的所有变量名。

data want;
    set have;
    num_questions = 4;
    if Q1 = 1 then Correct_Q1 = 1; else Correct_Q1 = 0;
    if Q2 = 2 then Correct_Q2 = 1; else Correct_Q2 = 0;
    if Q3 = 2 then Correct_Q3 = 1; else Correct_Q3 = 0;
    if Q4 = 2 then Correct_Q4 = 1; else Correct_Q4 = 0;
    format Answered_Correct $3. Answered_Correct_pct percent.;
    Answered_Correct = compress(put(sum(of Correct:),$8.)||'/'||put(num_questions, 8.));
    Answered_Correct_pct = sum(of Correct:) / num_questions;
    label Student = 'Student' Answered_Correct = 'Answered correct' Answered_Correct_pct = 'Answered correct (%)';
    keep Student Answered_Correct Answered_Correct_pct;
run;

proc print data=want noobs label;
run;

答案 2 :(得分:0)

如果您只有四个问题,那么最快的解决方案可能就是使用条件语句:if Q1 = 1 then answer + 1; 有关使用查找/答案表的更一般的解决方案:

转置数据,合并答案表,总结学生。

data broad_data;
   infile datalines missover;
   input Student $ Q1   Q2  Q3    Q4;
   datalines;
A            1   3   2    3
B            2   3   2    2    
C            1   2   1    2
D            3   3   1    2
;

data answers;
   infile datalines missover;
   input question $ correct_answer ;
   datalines;
Q1          1  
Q2          2  
Q3          2  
Q4          2 
;


data long_data;
    set broad_data;
    length question $10 answer 8;
    array long[*] Q1--Q4; 

    do i = 1 to dim(long);
        question    = vname(long[i]);
        answer      = long[i];
        output;
    end;
    keep Student question answer;
run;
proc sort data = long_data; by question student; run;

data long_data_answers;
    merge  long_data
           answers
           ;
    by question;
run;
proc sort data = long_data_answers; by student; run;

data result;
    do i = 1 by 1 until (last.student);
        set long_data_answers;
        by student;
        count = sum(count, answer eq correct_answer);
    end;
    result = count/i;
    keep student result;
    format result fract8.;
run;

如果您喜欢sql /想要压缩代码,可以将最后两个datasteps + sorts组合成一个语句。

proc sql;
create table result as
    select student, sum(answer eq correct_answer)/count(*) as result format fract8.
        from long_data a
            inner join answers b
            on a.question eq b.question
        group by student
    ;
quit;
相关问题