我正在尝试分析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次访问)
我需要计算访问次数:
另外,如何移除Ptcls
永远不会E
有什么想法吗?我甚至不知道从哪里开始!
答案 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
select t.*
from table t
where exists (select 1
from table t2
where t2.visitId = t.visitId and
t2.PtCls = 'E'
);