全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
2428 3
2009-03-22
求助高人:样本间隔的统计
下列程序是想统计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>
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2009-3-23 22:33:00

问题解决一半。下一半应该可以同样解决。看看是不是这样。
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;
 x_lag = lag(x);
 if x=1 then
       do;     if x_lag = 2 then count_2 = 1;
          else if x_lag = 1 then count_2 = 0; 
       end;
  else if x = 2 then
    do;       if x_lag = 1 then  count_2 = 1;
      else if x_lag = 2 then  count_2 + 1;
    end;
run;

data aa2 (keep= x x_1jg);
    if _N_ = 1 then x_1jg=0;
    set aa1;
 count_2lag = lag(count_2);
 if x=1 and x_lag=2 then x_1jg = count_2lag;
 if x=1 and x_lag=1 then x_1jg = 0;
run;
proc print data=aa2;
   var x x_1jg;
run;

output
Obs  x    x_1jg
 1    1      0
 2    2      .
 3    1      1
 4    1      0
 5    2      .
 6    2      .
 7    1      2
 8    1      0
 9    1      0
10    1      0
11    2      .
12    2      .
13    2      .
14    2      .
15    1      4
16    2      .
17    2      .
18    1      2
19    2      .
20    2      .
21    2      .
22    1      3

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2009-3-24 09:14:00

谢谢 yongyitian 您的指教!!

这就是需要的答案。

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2009-3-30 10:01:00

给个稍微简单点的:

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 aa2;
retain t 0;
set aa;
if x=1 then do;
 m=t;
 t=0;
end;
else do;
 t+1;
end;
drop t;
run;

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群