请选择 进入手机版 | 继续访问电脑版
楼主: 小鳄鱼a
2337 5

连续出现次数 [推广有奖]

  • 6关注
  • 10粉丝

学科带头人

3%

还不是VIP/贵宾

-

威望
0
论坛币
125 个
通用积分
0.0040
学术水平
40 点
热心指数
45 点
信用等级
43 点
经验
32801 点
帖子
1185
精华
0
在线时间
1539 小时
注册时间
2009-7-16
最后登录
2018-10-5

小鳄鱼a 发表于 2015-6-13 16:02:29 |显示全部楼层 |坛友微信交流群
50论坛币
做法是 , 判断r是否大于等于15   大于等于15取1   生成 r15   然后con是同一id连续出现大于等于15的次数。






idyearqtrrr15con

1

1997

1

7

1

1997

2

8

1

1997

3

9

1

1997

4

10

1

1998

1

11

1

1998

2

12

1

1998

3

13

1

1998

4

14

1

1999

1

15

1

4

1

1999

2

16

1

4

1

1999

3

17

1

4

1

1999

4

16

1

4

2

1996

3

15

1

1

2

1996

4

14

2

1997

1

13

2

1997

2

12

2

1997

3

14

2

1997

4

16

1

4

2

1998

1

18

1

4

2

1998

2

20

1

4

2

1998

3

22

1

4

2

1998

4

7

2

1999

1

17

1

2

2

1999

2

16

1

2



最佳答案

pobel 查看完整内容

一个data步: proc sort data=a; by id year qtr; run; data b; do tmp=1 to nobs; set a nobs=nobs point=tmp; con=.; if r=15 then stop; tmp=_end-1; end; end; stop; drop _:; run;
关键词:year con ear
pobel 在职认证  发表于 2015-6-13 16:02:30 |显示全部楼层 |坛友微信交流群
一个data步:
proc sort data=a;
by id year qtr;
run;

data b;
  do tmp=1 to nobs;
           set a nobs=nobs point=tmp;
           con=.;
           if r<15 then output;
           else do;
              con=1;
              _start=tmp;
                  _end=tmp;
                  _endflag=0;
              do while(_endflag=0 and _end<nobs);
                     _end=_end+1;
                         _id=id;
                         set a point=_end;
                         if r>=15 and _id=id and _end<=nobs then con=con+1;
                         else _endflag=1;
                   end;
                   do i=_start to ifn(_end=nobs and r>=15,_end,_end-1);
                            set a point=i;
                            output;
                        end;
                        if _end=nobs and r>=15 then stop;
                        tmp=_end-1;
                end;
   end;
   stop;
   drop _:;
run;
      
已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
小鳄鱼a + 5 + 5 + 5 + 5 精彩帖子

总评分: 论坛币 + 5  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

使用道具

littlepig818 发表于 2015-6-15 09:31:33 |显示全部楼层 |坛友微信交流群
data a;
input id year qtr r;
cards;
1 1997 1 7
1 1997 2 8
1 1997 3 9
1 1997 4 10
1 1998 1 11
1 1998 2 12
1 1998 3 13
1 1998 4 14
1 1999 1 15
1 1999 2 16
1 1999 3 17
1 1999 4 16
2 1996 3 15
2 1996 4 14
2 1997 1 13
2 1997 2 12
2 1997 3 14
2 1997 4 16
2 1998 1 18
2 1998 2 20
2 1998 3 22
2 1998 4 7
2 1999 1 17
2 1999 2 16
;
proc sort data=a;
by id year qtr;
run;
data b;
set a;
if r ge 15 then r15=1;
run;
proc sql;
create table mice as
select id,year,sum(r15) as con
from b
group by id,year;
create table abc as
select *
from b,mice
where b.id=mice.id and b.year=mice.year;
quit;
data result;
set abc;
if r15=. then con=.;
run;

proc print data=result;
run;
已有 1 人评分论坛币 收起 理由
小鳄鱼a + 5 精彩帖子

总评分: 论坛币 + 5   查看全部评分

使用道具

连续出现>15,还是说只需要知道大于15的个数?

使用道具

小鳄鱼a 发表于 2015-6-15 17:28:32 |显示全部楼层 |坛友微信交流群
littlepig818 发表于 2015-6-15 09:31
data a;
input id year qtr r;
cards;
这个应该是4    而不是1333
1.jpg
1.jpg

使用道具

pobel 在职认证  发表于 2015-6-17 08:35:16 |显示全部楼层 |坛友微信交流群
一般方法:
data c;
   set a;
   by id year qtr;
   if first.id then con=.;
   if r<15 then con=.;
   else con+1;
run;
   
proc sort data=c;
   by id descending year descending qtr;
run;

data d;
   set c;
   by id descending year descending qtr;
   retain _con;
   if missing(_con) ne missing(con) then _con=con;
   else con=_con;
   drop _con;
run;

proc sort data=d;
   by id year qtr;
run;

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-3-28 22:20