SAS: PROC TABULATE Rearrange Header Subgroups

主宰稳场 提交于 2019-12-13 07:04:47

问题


Question: How do I control the order of subgroups in the output of PROC TABULATE?

For instance, suppose I have this output, namely (A, B) within segment:

data example;
    input group $ segment $;
    datalines;
    1 A
    1 B
    2 A
    2 A
    3 B
    3 B
    ;
run;

proc tabulate data = example;
    class group segment;
    table group all, all = 'Total'*n segment*n;
run;

+-----------------------+------------+-------------------------+
+                       +            +         segment         +
+                       +            +------------+------------+
+                       +   Total    +     A      +     B      +
+                       +------------+------------+------------+
+                       +     N      +     N      +     N      +
+-----------------------+------------+------------+------------+
+    group              +            +            +            +
+-----------------------+            +            +            +
+    1                  +     2.00   +     1.00   +     1.00   +
+-----------------------+------------+------------+------------+
+    2                  +     2.00   +     2.00   +        .   +
+-----------------------+------------+------------+------------+
+    3                  +     2.00   +        .   +     2.00   +
+-----------------------+------------+------------+------------+
+    All                +     6.00   +     3.00   +     3.00   +
+-----------------------+------------+------------+------------+

How would I reorder the subgroups of segment to appear in the opposite order, (B, A)?

+-----------------------+------------+-------------------------+
+                       +            +         segment         +
+                       +            +------------+------------+
+                       +   Total    +     B      +     A      +
+                       +------------+------------+------------+
+                       +     N      +     N      +     N      +
+-----------------------+------------+------------+------------+
+    group              +            +            +            +
+-----------------------+            +            +            +
+    1                  +     2.00   +     1.00   +     1.00   +
+-----------------------+------------+------------+------------+
+    2                  +     2.00   +        .   +     2.00   +
+-----------------------+------------+------------+------------+
+    3                  +     2.00   +     2.00   +        .   +
+-----------------------+------------+------------+------------+
+    All                +     6.00   +     3.00   +     3.00   +
+-----------------------+------------+------------+------------+

It seems like there may be an option to order subgroups using DESC or DESCENDING=, but neither of those work for me. Part of my problem may be the terminology I'm using. I don't think "A" and "B" are called subgroups within the SAS lexicon.


回答1:


If you sort the data first and then user order=data you should be able to get what you need:

proc sort data=example;
 by group descending segment;
run;

proc tabulate data = example order=data;
    class group segment;
    table group all, all = 'Total'*n segment*n;
run;


来源:https://stackoverflow.com/questions/39479668/sas-proc-tabulate-rearrange-header-subgroups

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!