SAS从现有数据集计算新数据集的新变量

时间:2014-12-15 13:24:41

标签: variables sas creation

这是给定数据集projet.details_etest的一部分:

 "survey_instance_id"  "user_id"   "question_id"    "Item_correct"
'"2008"                "14389"        "4243"           "0"
'"2008"                "14489"        "4243"           "1"
'"2008"                "14499"        "4253"           "0"
'"2008"                "1669"         "4253"           "1"

我想创建一个名为projet.resume_question的新数据集,其中包含按question_id排序的数据集详细信息,其中包含变量:

  • survey_instance_id
  • question_id
  • nb_correct_answers
  • nb_incorrect answers
  • nb_omitted_answers
  • nb_total_with_omitted_answers
  • nb_total_without_omitted_answers

变量nb_omitted_answers是参与者总数减去nb_correct_answers,每个问题的正确答案数减去nb_incorrect_answers,每个问题的错误答案数。

变量nb_total_with_omitted_answers是参与测试的参与者总数。

变量nb_total_without_omitted_answers是回答每个问题的参与者总数。

这是我做的:

   data projet.resume_question;
set projet.details_etest;
by question_id;
keep survey_instance_id question_id nb_correct_answers nb_incorrect_answers;
retain nb_correct_answers 0 nb_incorrect_answers 0;
if Item_correct =1 then correct_answers= Item_correct;
else if Item_correct =0 then incorrect_answers= Item_correct;
nb_correct_answers = sum (correct_answers);
nb_incorrect_answers= sum (incorrect_answers);
run;
proc print data=projet.resume_question;
run;

我是这样开始的,当我打印它时,我发现的东西对我来说似乎不对。有人能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

首先按调查,问题,参与者对数据集进行排序。

proc sort data = projet.details_etest out = details;
    by survey_instance_id question_id user_id;
run;

现在获取每次调查的参与者数量。

proc sql;
    create table participated as
    select survey_instance_id,
        count(distinct user_id) as nb_total_with_omitted_answers
    from details
    group by survey_instance_id;
quit;

通过调查来计算聚合,问题。

data aggregated;
    set details;
    by survey_instance_id question_id;

    retain nb_total_without_omitted_answers
           nb_correct_answers nb_incorrect_answers 0;

    if first.question_id then do;
        nb_total_without_omitted_answers = 0;
        nb_correct_answers = 0;
        nb_incorrect_answers = 0;
    end;

    if item_correct in (0, 1) then nb_total_without_omitted_answers + 1;

    if item_correct = 1 then nb_correct_answers + 1;
    else if item_correct = 0 then nb_incorrect_answers + 1;

    if last.question_id then output;

    drop user_id item_correct;
run;

最后,计算每个问题省略答案的数量。

data projet.resume_question;
    merge participated aggregated;
    by survey_instance_id;

    nb_omitted_answers = nb_total_with_omitted_answers -
        nb_correct_answers - nb_incorrect_answers;
run;

这可以满足您的需求。

相关问题