【问题标题】:Calculating percentages and scores计算百分比和分数
【发布时间】:2016-03-21 00:43:52
【问题描述】:

假设我有如下数据:

ID   A1Q A2Q B1Q B2Q Continued
23      1  2 2 3 
24      1  2 3 3 

为了理解它转换成的表格,ID 为 23 的人对问题 A1、A2、B1、B2 的答案分别为 1、2、2、4。我想知道如何知道在整个数据集中回答 1、2 或 3 的学生的百分比。

我尝试过使用

PROC FREQ data = test.one;
tables A2Q-A2Q;
tables B1Q-B2Q;
RUN;

但这并没有得到我想要的。它单独分析每个问题,输出很长。我只需要把它放到一张表中,告诉我这个百分比回答 1,这个百分比回答 2 等等。

输出可能是:

Question:         1    2   3
Percentage A1Q    40%  40%  20%
Percentage A2Q    60%  20%  20%
Total Percentage  30%  30%  40%

因此对于问题 A1Q,40% 的人选择了 1,40% 的人选择了 2,30% 的人选择了 3。总百分比是在所有给出答案的人中,30% 选择 1 30% 选择 2 和 40% 选择 3。

【问题讨论】:

  • 您能否再添加几行并演示您希望输出的样子?
  • 好的,我编辑原帖。
  • 完成,感谢您的观看!

标签: sas data-manipulation


【解决方案1】:

您仍然需要对其进行一些工作并转置最终结果,但这可能是一个开始...如果您有很多问题,请考虑将其包装在一个宏程序中。

data quest;
input ID  A1Q A2Q B1Q B2Q;
datalines;
21 2 3 1 2
22 3 2 2 3
23 1 2 2 3 
24 1 2 3 3
25 2 1 3 3
run;

options missing = 0;    

proc freq data=quest;
  table A1Q / nocol nocum nofreq out = freq1(rename=(A1Q=Answer Count=A1Q));
  table A2Q / nocol nocum nofreq out = freq2;
  table B1Q / nocol nocum nofreq out = freq3;
  table B2Q / nocol nocum nofreq out = freq4;
run;

proc sql;
  create table results as
    select freq1.Answer,
           freq1.Percent as pctA1Q,
           freq2.Percent as pctA2Q,
           freq3.Percent as pctB1Q,
           freq4.Percent as pctB2Q
      from freq1
        left join freq2 
          on freq1.Answer = freq2.A2Q
        left join freq3
          on freq1.Answer = freq3.B1Q
        left join freq4
          on freq1.Answer = freq4.B2Q;
quit;

【讨论】:

  • 我不是 Proc Tabulate 的忠实粉丝,但正如 @Reeza 所建议的那样,探索这条道路可能是值得的
【解决方案2】:

我的建议是转置您的数据,然后执行 proc freq 或 proc tabulate。我会推荐 proc tabulate 这样你就可以格式化你的输出,因为看起来你有分组的问题。

 data long;
    set have;

    array qs(*) a1q--b2q; *list first and last variable and everything in between will be included;

    do i=1 to dim(qs);
       question=vname(qs(i));
       response=qs(i);
       output;
     end;

     keep id question response;
 run;

 proc freq data=long;
     table question*response/list;
 run;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-01
    • 2021-01-10
    • 2023-03-09
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    相关资源
    最近更新 更多