【问题标题】:Calculating number of correct of multiple choice questions计算多项选择题的正确数
【发布时间】:2016-03-21 06:11:30
【问题描述】:

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

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

【问题讨论】:

  • 我是数组解决方案的粉丝 - 虽然在这种情况下我会考虑创建一个返回比较结果的自定义函数。

标签: sas data-manipulation


【解决方案1】:

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

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

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;

【讨论】:

    【解决方案2】:

    首先您必须创建变量num_questions 并将其设置为问题数。然后您需要编写与问题一样多的if-then-else 语句来创建二进制变量(标志)以检查每个答案是否正确(例如Correct_Q1)。使用sum(of 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;
    

    【讨论】:

      【解决方案3】:

      如果您只有四个问题,最快的解决方案可能是只使用条件语句: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/想压缩你的代码,你可以将最后两个数据步 + 排序合并到一个语句中。

      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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-16
        • 1970-01-01
        相关资源
        最近更新 更多