data temp;
set raw(where=(b ne 5));
by id;
lag_c=lag(c);
if first.id=0 then
do;
bxor_c=bxor(c,lag_c);
if bxor_c=1 then output;
end;
run;
data _null_;
dsid=open('temp','i');
n=attrn(dsid,'nobs');
put "*** " n "***";
run;[/code:2bufe6jp]作者: shiyiming 时间: 2009-9-16 17:42 标题: Re: 如何找数据中某些观测的(有附加条件的)前一个观测并计数 data step and lag function...........作者: shiyiming 时间: 2009-9-16 20:26 标题: Re: 如何找数据中某些观测的(有附加条件的)前一个观测并计数 [quote:3cug7hli]...如果只要结果不要过程的话,把B=5的观测去掉,问题就简单多了[/quote:3cug7hli]
data temp;
set raw;
by id;
retain a1 b1 c1 .;
if first.id then do;
if b ^=5 then do;
a1=a; b1=b; c1=c;
end;
delete;
end;
if a=1 and b1 ^=5 then do;
output;
if b ^=5 then do;
a1=a; b1=b; c1=c;
end;
end;
else if b ^=5 then do;
a1=a; b1=b; c1=c;
delete;
end;
run;
data temp1;
set temp end=endof;
if ^missing(c1) then if c ^=c1 then count+1;
if endof then put '# of interested obs=' count;