需要SAS来分析组内的数据

时间:2014-03-28 19:01:29

标签: sql sas

我正在尝试分析SAS 9.3中的一些分组数据。这就是它的结构:

VisitID     PtCls       CC       DX
A             E         NULL           NULL
A             E         CP             NULL
A             E         CP             NULL
A             I         CP             HEART ATTACK
A             I         CP             HEART ATTACK

B             E         shortbreath    NULL
B             E         shortbreath    NULL
B             E         shortbreath    NULL
B             E         shortbreath    NULL

C             I         CHECKUP   DEFICIENT FE
C             I         CHECKUP   DEFICIENT FE

D             U         NULL      NULL

E             E         NULL      NULL
E             E         CP        NULL
E             O         CP        POOR SURGERY
E             O         CP        POOR SURGERY
E             O         CP        POOR SURGERY

F             E         NULL      NULL
F             E         NULL      NULL
F             E         NULL      NULL

每个唯一的visitID是一次患者就诊(因此在这一组中共有6次访问)

我需要计算访问次数:

  • DX和CC始终为空(在那里永远不会有真正的值) - 2
  • CC为空,但DX不为空 - 0
  • PtCls在访问期间至少有一次'E' - 4
  • PtCls课程在访问期间绝不是'E' - 2

另外,如何移除Ptcls永远不会E

的群组

有什么想法吗?我甚至不知道从哪里开始!

1 个答案:

答案 0 :(得分:1)

您可以使用proc sql和嵌套聚合聚合来执行此操作。首先在访问级别定义条件:

select VisitID,
       (case when max(DC) is null and max(CC) is null then 1 else 0 end) as flag1,
       (case when max(DC) is not null and max(CC) is null then 1 else 0 end) as flag2,
       max(case when PtCls = 'E' then 1 else 0 end) as flag3,
       max(case when PtCls = 'E' then 0 else 1 end) as flag4
from table t
group by VisitID;

接下来,重新聚合:

select sum(flag1) as cnt1, sum(flag2) as cnt2, sum(flag3) as cnt3, sum(flag4) as cnt4
from (select VisitID,
             (case when max(DC) is null and max(CC) is null then 1 else 0 end) as flag1,
             (case when max(DC) is not null and max(CC) is null then 1 else 0 end) as flag2,
             max(case when PtCls = 'E' then 1 else 0 end) as flag3,
             max(case when PtCls = 'E' then 0 else 1 end) as flag4
      from table t
      group by VisitID
     ) v;

您可以使用exists

删除Ptcls永远不是E的组
select t.*
from table t
where exists (select 1
              from table t2
              where t2.visitId = t.visitId and
                    t2.PtCls = 'E'
             );