- data rawdata;
- input id$ stk$ year qtr;
- qtr2=yyq(year, qtr);
- format qtr2 yyq6.;
- cards;
- 1 2 1996 3
- 1 2 1996 4
- 1 2 1997 1
- 1 3 1995 1
- 1 3 1995 2
- 1 3 1998 2
- 1 3 1998 3
- 1 3 1999 3
- 2 3 1995 1
- 2 3 1995 2
- 2 3 1995 3
- 2 3 1998 1
- 3 3 1995 2
- 3 3 1995 3
- 3 3 1998 2
- 3 3 2000 1
- 3 4 1996 1
- 3 4 1996 2
- 3 2 1997 1
- ;
- run;
- **数据整理;
- proc sort data=rawdata(drop=year qtr) out=test;
- by stk qtr2 id;
- run;
- data test2;
- set test;
- by stk qtr2 id;
- length ids $20.;
- retain ids cnt;
- if first.qtr2 then do
- ids="";
- cnt=0;
- end;
- ids=catx(',',ids,id);
- cnt+1;
- if last.qtr2 then output;
- drop id;
- run;
- **定义首次出现;
- data test3;
- set test2;
- qtr_lag=lag(qtr2);
- length remark $20.;
- by stk;
- if first.stk then remark="首次出现";
- else if qtr_lag^=intnx('qtr',qtr2,-1) then remark="首次出现";
- else remark="非首次出现";
- drop qtr_lag;
- run;
- **数据整理;
- data test4;
- merge test3 test3(firstobs=2 keep=ids cnt rename=(ids=nextids cnt=nextcnt));
- run;
- data test4;
- set test4;
- by stk;
- if last.stk then do nextids=.; nextcnt=0; end;
- run;
- **计算x\y\z;
- data test5;
- set test4;
- if remark="首次出现" then
- do;
- x=cnt;
- i=1;y=0;
- do while(scan(nextids,i,',') ne "");
- if index(ids,scan(nextids,i,','))=0 then y=y+1;
- i+1;
- end;
- end;
- drop i cnt nextcnt nextids;
- if x+y>1 then z=(x-1)/(x+y-1);
- run;