创建一个指标,用于判断同一 ID 是否在 6 个月内发生过事件

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

我有这样的东西:

ID       Visit_Date        VISIT TYPE

001       JAN032000         OFFICE        
001       DEC141999         SURGERY       
001       FEB082002         OFFICE        
002       JUN272000         OFFICE        
002       APR142003         OFFICE        
003       MAR292000         SURGERY       
004       DEC072001         SURGERY       
004       FEB142002         OFFICE        

我想创建一个列,表明每位患者在每次就诊前 6 个月内是否接受过手术:

ID       Visit_Date        VISIT TYPE    SX_6MO_B4_VISIT

001       JAN032000         OFFICE        1
001       DEC141999         SURGERY       NA
001       FEB082002         OFFICE        0
002       JUN272000         OFFICE        0
002       APR142003         OFFICE        0
003       MAR292000         SURGERY       NA 
004       DEC072001         SURGERY       NA
004       FEB142002         OFFICE        1

我正在尝试这样的解决方案,但出现错误,无法找到 DateDiff 函数。任何想法都非常感谢!

PROC SQL;
SELECT *,
CASE WHEN EXISTS
(SELECT 1 FROM DATA D2 
WHERE D2.ID=D.ID AND
D2.VISIT_TYPE= "SURGERY" AND
AND D.VISIT_TYPE = "OFFICE" AND 
DATEDIFF(MM,D2.VISIT_DATE D.VISIT_DATE) <=6 )
THEN 1 ELSE 0 END AS SX_6MO_B4_VISIT
FROM DATA D;
QUIT;
sql date sas subquery
1个回答
0
投票

试试这个:

SELECT *,
   CASE WHEN EXISTS
        (SELECT 1 FROM Data D2 
        WHERE D2.ID=D.ID 
            AND D2.visit_type = 'SURGERY' 
            AND D.visit_type = 'OFFICE' 
            AND DATEDIFF(MM, D2.VISIT_DATE, D.VISIT_DATE) <=6 )
     THEN 1 ELSE 0 
   END AS SX_6MO_B4_VISIT
FROM Data D;
© www.soinside.com 2019 - 2024. All rights reserved.