SAS sumifs - 查找患者使用的某些类型的药丸数量

时间:2013-09-02 03:15:25

标签: sql-server excel sas

输入数据:

user    DOB                 UseDate            numPills Type
1   2013-01-01 00:00:00.000 2013-04-11 00:00:00.000 4   A
1   2013-01-01 00:00:00.000 2013-07-20 00:00:00.000 5   A
1   2013-01-01 00:00:00.000 2014-01-02 00:00:00.000 1   A
2   2013-01-02 00:00:00.000 2013-04-12 00:00:00.000 1   A
3   2013-01-01 00:00:00.000 2013-04-11 00:00:00.000 5   A
3   2013-01-01 00:00:00.000 2013-07-20 00:00:00.000 5   A
4   2013-01-02 00:00:00.000 2013-04-12 00:00:00.000 1   A
5   2013-01-01 00:00:00.000 2013-04-11 00:00:00.000 9   A
5   2013-01-01 00:00:00.000 2013-07-20 00:00:00.000 1   B
6   2013-01-01 00:00:00.000 2013-04-11 00:00:00.000 1   A

期望的输出:

user    DOB                  UseDate            numPills Type      sumifs
1   2013-01-01 00:00:00.000 2013-04-11 00:00:00.000 4   A   9
1   2013-01-01 00:00:00.000 2013-07-20 00:00:00.000 5   A   5
1   2013-01-01 00:00:00.000 2014-01-02 00:00:00.000 1   A   0
2   2013-01-02 00:00:00.000 2013-04-12 00:00:00.000 1   A   1
3   2013-01-01 00:00:00.000 2013-04-11 00:00:00.000 5   A   10
3   2013-01-01 00:00:00.000 2013-07-20 00:00:00.000 5   A   5
4   2013-01-02 00:00:00.000 2013-04-12 00:00:00.000 1   A   1
5   2013-01-01 00:00:00.000 2013-04-11 00:00:00.000 9   A   9
5   2013-01-01 00:00:00.000 2013-07-20 00:00:00.000 1   B   1
6   2013-01-01 00:00:00.000 2013-04-11 00:00:00.000 1   A   1

我正在尝试在SAS Enterprise Guide中实施Excel的SUMIFS。我已经使用游标在MS SQL Server 2008中做了类似的事情。

我的最终目标是能够通过元组遍历元组。我也在考虑以某种方式将相关记录分组,看看我是否可以使用任何集合操作。

数据: 我的样本记录是患者的药物处方,每个记录详细说明了购买了多少药丸。因此,我需要计算每位患者的药丸数量,最好是计入不同的药箱。 即

患者#1,丸A,3粒

患者#1,丸A,2粒

患者#1,药丸B,1丸

我需要为第一行创建一个新列(使用“SUMIFS”): 患者#1,丸A,5粒

谢谢你们!

根据要求(抱歉格式化,我是这个论坛的新手):

/****** Script for SelectTopNRows command from SSMS  ******/
/* Cursor code begins here */
DECLARE @baseRWpointer CURSOR                       -- Base (read, write) cursor
DECLARE @offsetRpointer CURSOR                      -- Offset (read only) cursor

DECLARE @baseDate datetime
DECLARE @baseUser integer
DECLARE @baseType varchar(5)
DECLARE @baseDOB datetime

DECLARE @cumulative integer
DECLARE @offsetCount integer

DECLARE @offsetDate datetime
DECLARE @offsetUser integer
DECLARE @offsetType varchar(5)
DECLARE @offsetNumPills integer
DECLARE @offsetDOB datetime

SET @baseDate = '1900-01-01'
SET @baseUser = -1
SET @baseType = NULL

SET @cumulative = 0
SET @offsetCount = 0

SET @offsetRpointer = CURSOR SCROLL KEYSET
    FOR
        SELECT D.[user], D.[DOB], D.[UseDate], D.[numPills], D.[Type]   
        FROM [Charles_DB].[dbo].[Table1] D
        ORDER BY D.[ID], D.[Type], D.[UseDate]
    --FOR READ ONLY
OPEN @offsetRpointer

SET @baseRWpointer = CURSOR
    FOR
        SELECT D.[user], D.[DOB], D.[UseDate], D.[numPills], D.[Type]   
        FROM [Charles_DB].[dbo].[Table1] D
        ORDER BY D.[user], D.[Type], D.[UseDate]
    FOR update of D.[sumifs]

OPEN @baseRWpointer

FETCH NEXT from @baseRWpointer
INTO @offsetUser, @offsetDOB, @offsetDate, @offsetNumPills, @offsetType

FETCH NEXT from @offsetRpointer
INTO @offsetUser, @offsetDOB, @offsetDate, @offsetNumPills, @offsetType

WHILE   (@@FETCH_STATUS = 0)
BEGIN

    SET @baseUser = @offsetUser
    SET @baseType = @offsetType
    SET @baseDOB = @offsetDOB
    SET @cumulative = 0
    SET @offsetCount = 0

    /*  Main "SUMIFS" loop  */
    while ( 
            @@FETCH_STATUS = 0 AND 
            (@baseUser = @offsetUser)
            AND (@baseType = @offsetType)
            AND ((DATEDIFF(day,@baseDOB,@offsetDate)) <= 365)  )
    BEGIN
        Set @cumulative = @cumulative + @offsetNumPills
        Set @offsetCount = @offsetCount + 1

        FETCH NEXT from @offsetRpointer
        INTO @offsetUser, @offsetDOB, @offsetDate, @offsetNumPills, @offsetType

    END

    /*  Update the column "sumifs" for base row  
        Recall that D is [Charles_DB].[dbo].[Table1] */
    UPDATE [Charles_DB].[dbo].[Table1] 
        Set [Charles_DB].[dbo].[Table1].[sumifs] = @cumulative
        WHERE CURRENT OF @baseRWpointer

    /*  Make offset pointer catch up to base pointer */
    Set @offsetCount = -(@offsetCount - 1)
    FETCH RELATIVE @offsetCount from @offsetRpointer
    INTO @offsetUser, @offsetDOB, @offsetDate, @offsetNumPills, @offsetType

    /* Place this at the end to get the correct @@FETCH_STATUS output to avoid infinite loop  */
    FETCH NEXT from @baseRWpointer
    INTO @offsetUser, @offsetDOB, @offsetDate, @offsetNumPills, @offsetType




END

CLOSE @baseRWpointer
CLOSE @offsetRpointer
DEALLOCATE @baseRWpointer
DEALLOCATE @offsetRpointer

2 个答案:

答案 0 :(得分:0)

SAS有程序PROC SQL,PROC FREQ,PROC SUMMARY和PROC TABULATE。 SAS Enterprise Guide中的查询和过滤器节点将运行PROC SQL步骤,因此您应该能够通过专利,药丸对结果进行分组,然后将药丸数量相加。

答案 1 :(得分:0)

也许我还没有完全理解你想要的东西。

我假设你的数据集看起来像你上面给出的那样(Patient,PillType,Pills)。这听起来像一个简单的SQL摘要。

PROC SQL noprint;
create table PillSummary as
select Patient,
       PillType,
       sum(Pills) as Pills
  from MyTable
  group by Patient, PillType;
quit;

这将创建一个名为PillSummary的表,其中包含患者和类型的药丸总和。如果您正在寻找别的东西,请告诉我。