SAS:了解滞后功能以根据工作进度保留日期

时间:2017-04-27 15:58:22

标签: sas lag retain

我有一份工作进度表。 因此,如果我们有一个工作进度表,如新的,进度,开始,结束和重新启动,一些规则是:

  1. 首先,当工作为NEW时,开始日期设置为'1/01/2013',其他work_progress设置为相同。

  2. 其次,如果工作被END-ed并再次添加,则开始日期设置为'01 / 01/2016'(下方:Work_id = 3)。以下work_progress必须具有相同的值。

  3. 最后一种情况,当工作(work_id:1,2)RESTART时,开始日期设置为收到工作的开始。以后的日期必须遵循相同的日期 '01 / 05 / 2017' 。下面是用我的逻辑输出的数据集。

  4. text -indent

    +---------+---------------+-------------------+------------+------------+
    | work_id | work_progress |   received_date   |   start    |    end     |
    +---------+---------------+-------------------+------------+------------+
    |       1 | NEW           | November 19, 2016 | 01/01/2013 | 31/12/2020 |
    |       1 | PROGRESS      | December 25, 2016 | 01/01/2013 | 31/12/2020 |
    |       1 | END           | January 1, 2017   | 01/01/2013 | 02/02/2017 |
    |       1 | RESTART       | February 5, 2017  | 01/05/2017 | 31/12/2020 |
    |       1 | PROGRESS      | March 20, 2017    | 01/01/2013 | 31/12/2020 |
    |       2 | NEW           | November 19, 2016 | 01/01/2013 | 31/12/2020 |
    |       2 | PROGRESS      | December 25, 2016 | 01/01/2013 | 31/12/2020 |
    |       2 | END           | January 1, 2017   | 01/01/2013 | 31/12/2020 |
    |       2 | RESTART       | February 5, 2017  | 01/05/2017 | 31/12/2020 |
    |       2 | PROGRESS      | March 20, 2017    | 01/01/2013 | 31/12/2020 |
    |       3 | NEW           | November 19, 2016 | 01/01/2013 | 31/12/2020 |
    |       3 | END           | December 25, 2016 | 01/01/2013 | 02/02/2017 |
    |       3 | NEW           | January 1, 2017   | 01/01/2016 | 31/12/2020 |
    |       3 | END           | February 5, 2017  | 01/01/2013 | 02/02/2017 |
    |       3 | END           | March 20, 2017    | 01/01/2013 | 03/03/2017 |
    |       3 | END           | April 21, 2017    | 01/01/2013 | 04/04/2017 |
    +---------+---------------+-------------------+------------+------------+  
    

    我的输出实际上是什么:

    +---------+---------------+-------------------+------------+------------+
    | work_id | work_progress |   received_date   |   start    |    end     |
    +---------+---------------+-------------------+------------+------------+
    |       1 | NEW           | November 19, 2016 | 01/01/2013 | 31/12/2020 |
    |       1 | PROGRESS      | December 25, 2016 | 01/01/2013 | 31/12/2020 |
    |       1 | END           | January 1, 2017   | 01/01/2013 | 02/02/2017 |
    |       1 | RESTART       | February 5, 2017  | 01/05/2017 | 31/12/2020 |
    |       1 | PROGRESS      | March 20, 2017    | 01/05/2017 | 31/12/2020 |
    |       2 | NEW           | November 19, 2016 | 01/01/2013 | 31/12/2020 |
    |       2 | PROGRESS      | December 25, 2016 | 01/01/2013 | 31/12/2020 |
    |       2 | END           | January 1, 2017   | 01/01/2013 | 31/12/2020 |
    |       2 | RESTART       | February 5, 2017  | 01/05/2017 | 31/12/2020 |
    |       2 | PROGRESS      | March 20, 2017    | 01/05/2017 | 31/12/2020 |
    |       3 | NEW           | November 19, 2016 | 01/01/2013 | 31/12/2020 |
    |       3 | END           | December 25, 2016 | 01/01/2013 | 02/02/2017 |
    |       3 | NEW           | January 1, 2017   | 01/01/2016 | 31/12/2020 |
    |       3 | END           | February 5, 2017  | 01/01/2016 | 02/02/2017 |
    |       3 | END           | March 20, 2017    | 01/01/2016 | 02/02/2017 |
    |       3 | END           | April 21, 2017    | 01/01/2016 | 02/02/2017 |
    +---------+---------------+-------------------+------------+------------+    
    

    要求:

    1. 当NEW和时,应将开始日期添加到以后的工作进度中 RESTART。
    2. 在work_id = 3和work_progress = END的结束日期。游行和四月 两者的结束日期应该是feb。
    3. 我需要在这里使用滞后来保留开始和结束日期。除了这个滞后使用部分,我已经实现了问题逻辑的一半。 部分sas代码:

      data m_out_ds;
       set m_in_ds;
       by work_id work_received_date;
       /*--------
       Some logic to derive my rules, that gave output, first table above.
        ----------*/
       prevstart = lag(start);
       prevend = lag(end); 
       prev_work_progress = lag(work_progress);
      
       if work_progress = 'END' and prev_work_progress = 'END' then end = prevend;
      
      /*---This gave 02/02/2017 for march received date only, 
        but we require for april too, obvious the work has ended.----*/
      
      if work_progress = 'PROGRESS' and prev_work_progress ='RESTART' 
        then start = prevstart; 
      
      /*---This however worked---*/
      
      run;
      

      如果你理解这一点,请告诉我。 感谢。

1 个答案:

答案 0 :(得分:0)

这似乎与您的数据相符,但我仍然不确定我理解规则。首先,让我们将您的文本转换为数据。

original

现在让我们尝试转换它。

data have ;
  infile cards dsd dlm='|' truncover ;
  row+1;
  length work_id 8 work_progress $8 received_date start end 8 ;
  informat received_date anydtdte. start end ddmmyy.;
  format received_date  start end yymmdd10.;
  input work_id -- end ;
CARDS;
   1|NEW     | November 19, 2016|01/01/2013|31/12/2020 
   1|PROGRESS| December 25, 2016|01/01/2013|31/12/2020 
   1|END     | January 1, 2017  |01/01/2013|02/02/2017 
   1|RESTART | February 5, 2017 |01/05/2017|31/12/2020 
   1|PROGRESS| March 20, 2017   |01/01/2013|31/12/2020 
   2|NEW     | November 19, 2016|01/01/2013|31/12/2020 
   2|PROGRESS| December 25, 2016|01/01/2013|31/12/2020 
   2|END     | January 1, 2017  |01/01/2013|31/12/2020 
   2|RESTART | February 5, 2017 |01/05/2017|31/12/2020 
   2|PROGRESS| March 20, 2017   |01/01/2013|31/12/2020 
   3|NEW     | November 19, 2016|01/01/2013|31/12/2020 
   3|END     | December 25, 2016|01/01/2013|02/02/2017 
   3|NEW     | January 1, 2017  |01/01/2016|31/12/2020 
   3|END     | February 5, 2017 |01/01/2013|02/02/2017 
   3|END     | March 20, 2017   |01/01/2013|03/03/2017 
   3|END     | April 21, 2017   |01/01/2013|04/04/2017 
;
data want ;
  infile cards dsd dlm='|' truncover ;
  row+1;
  length work_id 8 work_progress $8 received_date start end 8 ;
  informat received_date anydtdte. start end ddmmyy.;
  format received_date  start end yymmdd10.;
  input work_id -- end ;
CARDS;
   1|NEW        |November 19, 2016|01/01/2013|31/12/2020 
   1|PROGRESS   |December 25, 2016|01/01/2013|31/12/2020 
   1|END        |January 1, 2017  |01/01/2013|02/02/2017 
   1|RESTART    |February 5, 2017 |01/05/2017|31/12/2020 
   1|PROGRESS   |March 20, 2017   |01/05/2017|31/12/2020 
   2|NEW        |November 19, 2016|01/01/2013|31/12/2020 
   2|PROGRESS   |December 25, 2016|01/01/2013|31/12/2020 
   2|END        |January 1, 2017  |01/01/2013|31/12/2020 
   2|RESTART    |February 5, 2017 |01/05/2017|31/12/2020 
   2|PROGRESS   |March 20, 2017   |01/05/2017|31/12/2020 
   3|NEW        |November 19, 2016|01/01/2013|31/12/2020 
   3|END        |December 25, 2016|01/01/2013|02/02/2017 
   3|NEW        |January 1, 2017  |01/01/2016|31/12/2020 
   3|END        |February 5, 2017 |01/01/2016|02/02/2017 
   3|END        |March 20, 2017   |01/01/2016|02/02/2017 
   3|END        |April 21, 2017   |01/01/2016|02/02/2017 
;

enter image description here