求助高人:样本间隔的统计
下列程序是想统计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;
我想得到下列形式:
x x_1jg x_2jg
1 0 .
2 . 1
1 1 .
1 0 .
2 . 2
2 . 0
1 2 .
1 0 .
1 0 .
1 0 .
2 . 4
2 . 0
2 . 0
2 . 0
1 4 .
2 . 1
2 . 0
1 2 .
2 . 1
2 . 0
2 . 0
1 3 .
谢谢!! <script type="text/javascript"></script><script src="https://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script><script></script>