我正在try 根据两个不同的数据集(及其子集)获取频率.请考虑下面的例子

DATA Group1;
INPUT Subject_ID education;
DATALINES;
1 LowerPrimary
2 Secondary
3 UpperPrimary
4 LowerPrimary
5 Secondary
6 Secondary
7 LowerPrimary
8 UpperPrimary
9 UpperPrimary
10 Secondary
;
RUN;
DATA Group2;
INPUT Subject_ID education;
DATALINES;
1 LowerPrimary
2 Secondary
3 UpperPrimary
4 LowerPrimary
5 Secondary
6 Secondary
7 LowerPrimary
8 UpperPrimary
9 UpperPrimary
10 Secondary
11 LowerPrimary
12 UpperPrimary
;
RUN;

我想要一个代码,以获得以下SAS输出.

         Lower_Primary Upper_Primary Primary_Total Secondary Group_Total
Group1      3               3            6           4          10
Group2      4               4            8           4          12

Primary_Total=Lower_Primary+Upper_Primary个小时 Group_Total=Lower_Primary+Upper_Primary+Secondary

推荐答案

这是一个很好的例子,说明了使用多标签格式可以有所帮助.

首先,使用一个组变量将数据合并到一个数据集中,这样您就可以一次处理所有数据.(请注意,您发布的数据集步骤不起作用,因为您将教育定义为数字.)

data Group1;
  input Subject_ID education $20.;
datalines;
1 LowerPrimary
2 Secondary
3 UpperPrimary
4 LowerPrimary
5 Secondary
6 Secondary
7 LowerPrimary
8 UpperPrimary
9 UpperPrimary
10 Secondary
;
data Group2;
  input Subject_ID education $20.;
datalines;
1 LowerPrimary
2 Secondary
3 UpperPrimary
4 LowerPrimary
5 Secondary
6 Secondary
7 LowerPrimary
8 UpperPrimary
9 UpperPrimary
10 Secondary
11 LowerPrimary
12 UpperPrimary
;

data both;
  set group1(in=in1) group2(in=in2);
  if in1 then group='GROUP1';
  else group='GROUP2';
run;

然后定义可生成所需类别的多标签格式.

proc format ;
value $education (multilabel notsorted)
  'LowerPrimary' = 'LowerPrimary'
  'UpperPrimary' = 'UpperPrimary'
  'LowerPrimary','UpperPrimary' = 'Primary'
  'Secondary' = 'Secondary'
  'LowerPrimary','UpperPrimary','Secondary' = 'Group_Total'
;
run;

现在,我们可以使用支持多标签格式的过程(如PROC TABLATE)来生成报告.

proc tabulate data=both order=data ;
  class group;
  class education / mlf preloadfmt;
  format education $education.;
  table group,education;
run;

结果

---------------------------------------------------------------------------------------------
|                          |                           education                            |
|                          |----------------------------------------------------------------|
|                          |LowerPrimary|UpperPrimary|  Primary   | Secondary  |Group_Total |
|                          |------------+------------+------------+------------+------------|
|                          |     N      |     N      |     N      |     N      |     N      |
|--------------------------+------------+------------+------------+------------+------------|
|group                     |            |            |            |            |            |
|--------------------------|            |            |            |            |            |
|GROUP1                    |        3.00|        3.00|        6.00|        4.00|       10.00|
|--------------------------+------------+------------+------------+------------+------------|
|GROUP2                    |        4.00|        4.00|        8.00|        4.00|       12.00|
---------------------------------------------------------------------------------------------

Sql相关问答推荐

创建每小时重置的序列号

使用SQL/R循环查找邻居

LAG函数通过丢弃空值返回前一行

Access VBA SQL命令INSERT FOR MULTIME VALUE

如何在SQL Server中统计按备注分组的记录数

将日期时间转换为日期格式

基于多参数的SQL Server条件过滤

是否可以为表中的所有列生成散列值?

Oracle PL/SQL:解决DBMS输出大小限制的问题

按连续相等值分组排序

在SQL中,筛选其他列中只有一个值的ID

从重复值中获取最新值

查询以从时间范围列表中查找唯一时间段

仅当 SQL Server 中的表为开时,才在存储过程中使用更改跟踪

SQL Server 查询 WHERE LIKE

JSON_VALUE 不适用于提取的 json 中的嵌套路径

在没有订单的情况下,如何生成一个值为0的顾客天数行

如何按日期和位置对最近 3 个报告日期的 SQL 查询结果进行透视?

添加一列并根据其他列值进行填充

包含多行的 SQL 查询