全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
2755 5
2015-06-13
悬赏 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;
二维码

扫码加我 拉你入群

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

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

全部回复
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;
      
二维码

扫码加我 拉你入群

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

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

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;
二维码

扫码加我 拉你入群

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

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

2015-6-15 11:21:55
连续出现>15,还是说只需要知道大于15的个数?

二维码

扫码加我 拉你入群

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

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

2015-6-15 17:28:32
littlepig818 发表于 2015-6-15 09:31
data a;
input id year qtr r;
cards;
这个应该是4    而不是1333
附件列表
1.jpg

原图尺寸 20.7 KB

1.jpg

1.jpg

原图尺寸 20.7 KB

1.jpg

二维码

扫码加我 拉你入群

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

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

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;
二维码

扫码加我 拉你入群

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

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

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

说点什么

分享

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