-1

输入数据:

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 中使用游标做过类似的事情。

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

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

患者 #1,药片 A,3 片

患者 #1,药片 A,2 片

患者 #1,药片 B,1 片

我需要为第一行创建一个新列(使用“SUMIFS”):Patient #1, Pill A, 5 pills

谢谢大家!!!

根据要求(对不起格式,我是这个论坛的新手):

/****** 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
4

2 回答 2

0

SAS 具有过程 PROC SQL、PROC FREQ、PROC Summary 和 PROC TABULATE。SAS Enterprise Guide 中的查询和过滤节点将运行一个 PROC SQL 步骤,因此您应该能够按专利、药丸对结果进行分组,然后对药丸的数量求和。

于 2013-09-02T14:23:27.340 回答
0

也许我没有完全理解你想要什么。

我假设你的数据集看起来像你上面给出的(病人、药丸类型、药丸)。这听起来像是一个简单的 SQL 总结。

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

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

于 2013-09-02T15:51:23.833 回答