计算百分比和分数

时间:2016-03-21 00:43:53

标签: sas data-manipulation

假设我有类似的数据:

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%

所以它会翻译成40%的人选择1,40%的人选择2,30%的人选择3来回答问题A1Q。所有给出答案的人中总有百分比,30%选择1 30%选择2,40%选择3。

2 个答案:

答案 0 :(得分:1)

我的建议是转置您的数据,然后执行proc freq或proc制表。我建议使用proc制表,以便您可以格式化输出,因为看起来您有问题已分组。

 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;

答案 1 :(得分: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;

Output Table

相关问题