一旦多个变量满足条件,就放弃观察 [英] Drop observations once condition is met by multiple variables

查看:18
本文介绍了一旦多个变量满足条件,就放弃观察的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据并使用现有的已回答问题之一来解决我的数据问题,但无法得到我想要的.这是我的数据中的内容

I have the following data and used one of the existing answered questions to solve my data problem but could not get what I want. Here is what I have in my data

  • 当 Evt_type 为 Fee 时填充 Amt1
  • 当 Evt_type 为 REF1/REF2 时填充 Amt2
  • 我不想在最后一个 Flag='Y' 之后显示任何观察结果
  • 如果没有 Flag='Y' 那么我想要该 id 的所有观察结果(例如 id=102)
  • 我想显示该 id 的下一行是否是费用,然后是 flag='Y' 之后的 REF1/REF2(例如 id=101)但是如果没有 REF1/REF2,我不想要(egid=103)

有:

       id   Date        Evt_Type   Flag   Amt1   Amt2
      101  2/2/2019      Fee              5
      101  2/3/2019      REF1      Y             5
      101  2/4/2019      Fee              10
      101  2/6/2019      REF2      Y             10
      101  2/7/2019      Fee               4
      101  2/8/2019      REF1
      102  2/2/2019      Fee              25
      102  2/2/2019      REF1      N      25
      103  2/3/2019      Fee              10
      103  2/4/2019      REF1      Y             10
      103  2/5/2019      Fee              10

想要:

      id   Date        Evt_Type   Flag   Amt1   Amt2
     101  2/2/2019      Fee              5
     101  2/3/2019      REF1      Y             5
     101  2/4/2019      Fee              10
     101  2/6/2019      REF2      Y             10
     101  2/7/2019      Fee               4
     101  2/8/2019      REF1
     102  2/2/2019      Fee              25
     102  2/2/2019      REF1      N      25
     103  2/4/2019      REF1      Y             10
     103  2/5/2019      Fee              10

我尝试了以下

data want;
    set have;
    by id Date;
    drop count;

    if (first.id or first.date) and FLAG='Y' then
        do;
            retain count;
            count=1;
            output;
            return;
        end;

    if count=1 and ((first.id or first.date) and Flag ne 'Y') then
        do;
            retain count;
            delete;
            return;
        end;
    output;
run;

感谢任何帮助.

谢谢

推荐答案

一种称为 DOW 循环 的技术可以执行以某种方式测量组的计算,然后在第二个循环中应用将该计算传递给该组的成员.

A technique known as DOW loop can perform a computation that measures a group in some way and then, in a second loop, apply that computation to members of the group.

DOW 依赖于循环内的 SET 语句.在这种情况下,计算是组中的哪一行是最后一个具有 flag="Y" 的行.

The DOW relies on a SET statement inside the loop. In this case the computation is 'what row in the group is the last one having flag="Y".

data want;
  * DOW loop, contains computation;

  _max_n_with_Y = 1e12;

  do _n_ = 1 by 1 until (last.id);
    set have;
    by id;
    if flag='Y' then _max_n_with_Y = _n_;
  end;

  * Follow up loop, applies computation;
  do _n_ = 1 to _n_;
    set have;
    if _n_ <= _max_n_with_Y then OUTPUT;
  end;
  drop _:;
run;

这篇关于一旦多个变量满足条件,就放弃观察的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆