【问题标题】:Compare number of instances in a column for two different dates in SAS比较 SAS 中两个不同日期的列中的实例数
【发布时间】:2017-05-04 23:51:30
【问题描述】:

我有以下数据集 (items),其中包含任何日期的交易和下一个工作日支付的金额。

rate>5 的 id 在下一个工作日为每个 id 支付的金额为 10 美元

我的任务是比较rate > 5 与下一个工作日支付的金额一致的实例数(这将有一个标准的code 121

例如,4/14/2017' - The amount$40 (4*10)is paid on4/17/2017 上有四个带有rate > 5 的实例`

Date       id  rate code    batch
4/14/2017   1   12  100     A1
4/14/2017   1    2  101     A1
4/14/2017   1   13  101     A1
4/14/2017   1   10  100     A1
4/14/2017   1   10  100     A1
4/17/2017   1   40  121 
4/20/2017   2   12  100     A1
4/20/2017   2   2   101     A1
4/20/2017   2   3   101     A1
4/20/2017   2   10  100     A1
4/20/2017   2   10  100     A1
4/21/2017   2   30  121 

My code

proc sql;
   create table items2 as select 
     count(id) as id_count,
     (case when code='121' then rate/10 else 0 end) as rate_count
    from items
    group by date,id;
quit;

这并没有产生预期的结果,我面临的挑战是检查交易日期(4/14/20174/20/2017)和下一个工作日日期(4/17/2017,4/21/2017)。

感谢您的帮助。

【问题讨论】:

    标签: date sas compare proc


    【解决方案1】:

    LAG 函数将在这里解决问题。因为我们可以使用滞后值来创建我们想要的条件,而不必使用 rate>5 条件。

    这是解决方案:-

    Data items;
    set items;
    
    Lag_dt=LAG(Date);
    Lag_id=LAG(id);
    Lag_rate=LAG(rate);
    
    if ((id=lag_id) and (code=121) and (Date>lag_dt)) then rate_count=(rate/lag_rate);
    else rate_count=0;
    
    Drop lag_dt lag_id lag_rate;
    
    run;
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 2019-10-03
      相关资源
      最近更新 更多