SAS proc freq 或 proc sql 获取数据子集和整个数据的频率

问题描述 投票:0回答:1

我试图根据两个不同的数据集(及其子集)获取频率。考虑下面的例子

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

sql sas frequency sas-macro
1个回答
1
投票

这是一个很好的例子,说明使用 MULTILABLE 格式可以提供帮助。

首先使用 GROUP 变量将数据合并到一个数据集中,以便您可以一次性处理所有数据。 (请注意,您发布的数据集步骤无法工作,因为您将 EDUCATION 定义为数字。)

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 TABULATE)来生成报告。

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|
---------------------------------------------------------------------------------------------
© www.soinside.com 2019 - 2024. All rights reserved.