求助高人:样本间隔的统计
下列程序是想统计1出现时,距上次出现间隔了多少次,怎么不对,请高手帮助?
data aa;
input x @@;
cards;
1 2 1 1 2 2 1 1 1 1 2 2 2 2 1 2 2 1 2 2 2 1
;
run;
data aa1;
set aa;
if x=1 then x_1+1; else x_1=.;
if x^=1 then x_2+1; else x_2=.;
if x=1 and x^=lag(x) then x_1jg=lag(x_2);
if x=1 and x =lag(x) then x_1jg=0;
if x=2 and x^=lag(x) then x_2jg=lag(x_1);
if x=2 and x =lag(x) then x_2jg=0;
run;
[code:29vm67q9]
data a;
input x @@;
cards;
1 2 1 1 2 2 1 1 1 1 2 2 2 2 1 2 2 1 2 2 2 1
;
data a;
set a(keep=x);
if x=1 then x_1+1;
else x_1 =.;
if x=2 then x_2+1;
else x_2 = .;
if x=1 and _n_=1 then x_1jg=0;
if x=2 and _n_=1 then x_2jg=0;
if x=1 and lag(x)=1 then x_1jg=0;
else if x=1 and lag(x)=2 then do;
x_1jg=u;
u=0;
end;
else if x=2 then u+1;
if x=2 and lag(x)=2 then x_2jg=0;
else if x=2 and lag(x)=1 then do;
x_2jg=t;
t=0;
end;
else if x=1 then t+1;
drop u t;
run;
[/code:29vm67q9]
大哥~~~,就别要求这么多了,我也就想着最简单的方法,那就是一样的逻辑~~~
要么我就换成array来处理了~~~~
好像要简单一些~~~
只要解决问题就行了~~~:D:D..(如果数据量不是那么大~~),
况且if else if 的效率还是比较高的。。