data test;
infile cards dlm='|';
input id stk year qtr;
qtrSeq = (year-1995)*4 + qtr;
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;
/*Start finding stock entry points */;
proc sort data=test; by stk year qtr qtrSeq; run;
proc means data=test noprint;
by stk year qtr qtrSeq;
output out=Stk_entries(keep=stk year qtr qtrSeq) N=N;
run;
data stk_entries; set stk_entries;
by stk;
if first.stk then flag=1;
else if qtrSeq - lag(qtrSeq) > 1 then flag = 1;
if flag =1 then output;
run;
/*Finish finding Stock Entry points */;
/* Flag customer entry points: initial entry and reentry */;
proc sort data=test; by stk id qtrSeq;
data test2; set test; by stk id;
if first.id then flag = 1; /* a customer's initial entry of the stk */;
stk_last = lag(stk);
id_last = lag(id);
qtrSeq = (year-1995)*4 + qtr;
qtrSeq_last = lag(qtrSeq);
qtrSeq_prev = qtrSeq-1;
if stk_last = stk and id_last = id and qtrSeq_last < qtrSeq -1 then flag = 1; /* a customer's reentry of the stk */
run;
/* Computing new total new entry by stock and quarter */
proc sort data=test2; by stk year qtr qtrSeq;
proc means data=test2(where=(flag=1)) noprint;
by stk year qtr qtrSeq qtrSeq_prev;
output out=stk_Summary N=N;
run;
/* Computing Ratios */;
data stk_Summary;
merge stk_Summary(keep=stk year qtr qtrseq N) stk_Summary(keep=stk qtrSeq_prev N rename=(qtrSeq_prev=qtrSeq N=N_next));
by stk qtrSeq;
x = sum(N, 0); y = sum(N_next, 0);
if x+y>1 then z = (x-1)/(x+y-1);
if z > . then output;
run;
data stk_summary_final;
merge stk_Summary(in=ina) stk_entries(keep=stk year qtr in=inb);
by stk year qtr;
if ina*inb;
run;
proc print data=stk_summary_final(obs=2); run;
/** Results */
[td]
| Obs__ | stk__ | year__ | qtr__ | qtrSeq__ | N__ | N_next__ | x__ | y__ | z__ |
| 1__ | 3__ | 1995__ | 1__ | 1__ | 2__ | 1__ | 2__ | 1__ | 0.5 |
| 2__ | 3__ | 1998__ | 1__ | 13__ | 1__ | 2__ | 1__ | 2__ | 0.0__ |