如何根据SAS中前几行的条件添加标志

如何根据SAS中前几行的条件添加标志,sas,lag,retain,Sas,Lag,Retain,我有以下数据,如果在前一行中满足条件,我想在每一行中添加一个标志 在下面的数据中,如果Cntr=S,并且仅当下一行是FE后跟BC/ABC时,我想要一个标志=1。我不想要2019年2月8日对101的观测,也不想要102的数据,因为FE之后没有BC/ABC 拥有: 想要: 我尝试使用lag和retain函数来解决这个问题,但没有得到我想要的。请帮忙 这是DOW processing可以计算行的标记状态的另一种情况 数组可用于跟踪组中的值。阵列简化了在S之后对多个区域进行标记的计算。选择大于最大预期组

我有以下数据,如果在前一行中满足条件,我想在每一行中添加一个标志

在下面的数据中,如果Cntr=S,并且仅当下一行是FE后跟BC/ABC时,我想要一个标志=1。我不想要2019年2月8日对101的观测,也不想要102的数据,因为FE之后没有BC/ABC

拥有:

想要:


我尝试使用
lag
retain
函数来解决这个问题,但没有得到我想要的。请帮忙

这是DOW processing可以计算行的标记状态的另一种情况

数组可用于跟踪组中的值。阵列简化了在
S
之后对多个区域进行标记的计算。选择大于最大预期组大小的数组大小

data have;
infile datalines missover;
attrib 
  id format=4. 
  date informat=mmddyy10. format=mmddyy10. 
  evt length=$3 
  cntr length=$1
;
input 
  id   Date        Evt      Cntr; datalines;
  101  2/2/2019      FE         
  101  2/3/2019      BC      S 
  101  2/4/2019      FE
  101  2/5/2019      BC
  101  2/6/2019      FE
  101  2/7/2019      ABC
  101  2/8/2019      FE
  102  2/2/2019      FE
  ;

data want;
  array evts[-1:1000] $3 _temporary_ ;
  array flags[1000] $1 _temporary_;

  call missing(of evts[*]);
  call missing(of flags[*]);

  do _n_ = 1 to dim(flags) until (last.id);

    set have;
    by id;

    evts[_n_] = evt;

    if cntr='S' then _s_index = _n_;

    if 0 < _s_index < _n_ - 1 then 
      if evt in ('BC', 'ABC') then 
        if evts[_n_-1] = 'FE' then 
          do ;
            flags[_n_] = '1';
            flags[_n_-1] = '1';
          end;
  end;

  if not last.id then do;
    put 'ERROR: ' id= 'group size larger than array size';
    stop;
  end;

  * apply flag value computed for each row of the group;
  do _n_ = 1 to _n_;
    set have;
    flag = flags[_n_];
    output;
  end;

  drop _:;
run;
数据已经存在;
填充数据线;
阿特里布
id格式=4。
日期信息=mmddyy10。格式=mmddyy10。
evt长度=$3
cntr长度=$1
;
输入
id日期Evt Cntr;数据线;
101 2019年2月2日FE
公元前1012/3/2019年南部
101 2019年2月4日FE
公元前1012/5/2019
101 2019年2月6日FE
101 2019年2月7日美国广播公司
101 2019年2月8日FE
102 2019年2月2日FE
;
数据需求;
阵列EVT[-1:1000]$3;
数组标志[1000]$1\u临时\u;
呼叫丢失(EVT[*]);
调用丢失(标志[*]);
do _n_=1使(标志)变暗,直到(last.id);
集有;
按身份证;
电动汽车变速箱[\u n\u]=电动汽车变速箱;
如果cntr='S',则_S_index=\u n;
如果0<\u s\u索引<\u n\u-1,则
如果evt处于('BC','ABC'),则
如果evts[_n_-1]=“FE”,则
做
标志[_n_]=“1”;
标志[_n_-1]=“1”;
结束;
结束;
如果不是last.id,则执行;
放置'ERROR:'id='组大小大于数组大小';
停止
结束;
*应用为组的每一行计算的标志值;
do _n_=1至_n_;
集有;
flag=旗子;
产出;
结束;
落下;
跑

您可以向我们展示您的代码吗?请在您的描述中更加明确,并/或给出更多关于何时设置标志和何时不设置标志的示例。例如,如果在第7行之后,Evt
FE
BC
的id
101
后面还会有两行,会发生什么?我是否理解您的意思,您只能知道您需要在Evt
BC
ABC
行上设置标志,但是您也必须用
FE
在前一行设置它?
   id   Date        Evt      Cntr       flag
  101  2/2/2019      FE         
  101  2/3/2019      BC      S 
  101  2/4/2019      FE                  1
  101  2/5/2019      BC                  1 
  101  2/6/2019      FE                  1
  101  2/7/2019      ABC                 1  
  101  2/8/2019      FE
  102  2/2/2019      FE 
data have;
infile datalines missover;
attrib 
  id format=4. 
  date informat=mmddyy10. format=mmddyy10. 
  evt length=$3 
  cntr length=$1
;
input 
  id   Date        Evt      Cntr; datalines;
  101  2/2/2019      FE         
  101  2/3/2019      BC      S 
  101  2/4/2019      FE
  101  2/5/2019      BC
  101  2/6/2019      FE
  101  2/7/2019      ABC
  101  2/8/2019      FE
  102  2/2/2019      FE
  ;

data want;
  array evts[-1:1000] $3 _temporary_ ;
  array flags[1000] $1 _temporary_;

  call missing(of evts[*]);
  call missing(of flags[*]);

  do _n_ = 1 to dim(flags) until (last.id);

    set have;
    by id;

    evts[_n_] = evt;

    if cntr='S' then _s_index = _n_;

    if 0 < _s_index < _n_ - 1 then 
      if evt in ('BC', 'ABC') then 
        if evts[_n_-1] = 'FE' then 
          do ;
            flags[_n_] = '1';
            flags[_n_-1] = '1';
          end;
  end;

  if not last.id then do;
    put 'ERROR: ' id= 'group size larger than array size';
    stop;
  end;

  * apply flag value computed for each row of the group;
  do _n_ = 1 to _n_;
    set have;
    flag = flags[_n_];
    output;
  end;

  drop _:;
run;