首页 > 解决方案 > 如何将 SAS 中的 x 变量与 proc freq 分组?-- 在 SAS proc 中使用交叉表对类别进行分组

问题描述

我会很感激你在这方面的帮助......

我有两列,我想获取类型是 1 还是 2 的月数,所以我使用了 proc freq:

proc freq DATA= count_types;
output out= counts;
TABLE amount_of_months * Type / nocum nopercent norows;

proc freq 返回如下结果:

Months           |     Type                         |Grant Total
-----------------+-----------------+----------------+-----------
1                |count(type1)=41  | count(type2)=60| 101
2                |count(type1)=50  | count(type2)=20| 70
3                |count(type1)=60  | count(type2)=30| 90
4                |count(type1)=10  | count(type2)=40| 50
5                |count(type1)=45  | count(type2)=15| 60
...

输出表如下所示:

amount_of_months | Type (contains type1=t1 or type2=t2) |
------------------------------------------------------------------------------
1                | t1                                   |  41 =count(type1) 
1                | t2                                   |  60 =count(type 2)
2                | t1                                   |  50 =count(type 1)
2                | t2                                   |  20 =count(type 2)
3                | t1                                   |  60 =count(type 1)
3                | t2                                   |  30 =count(type 2)
4                | t1                                   |  10 =count(type 1)
4                | t2                                   |  40 =count(type 2)
5                | t1                                   |  45 =count(type 1)
5                | t2                                   |  15 =count(type 2)

我想得到一个如下所示的 proc freq...而不是 1、2、3、4、5:

所以我可以得到这样的组:

Months|           Type
---------------------+-------------------
<2    | count(type1) | count(type2)
2-4   | count(type1) | count(type2)
4>+   | count(type1) | count(type2)
---------------------+-------------------

是否可以直接使用 proc freq 或者我应该在两个不同的列中创建一个包含 2 种类型的表?

我尝试使用 proc 格式,但在 case 但它不起作用...这个问题来自 Type 列吗?

代码尝试

proc format; 
  value amount_of_months
    0-2 = '<2' 
    2-4 = '2-4'
    4-high = '4+'
  ;

proc freq data = count_types;
  tables amount_of_months * type / norow nocol nopct ;
  format amount_of_months amount_of_months. ;
run;

标签: sasfrequencycrosstabprocproc-sql

解决方案


推荐阅读