3 weeks ago
I use the method group by to obtain stock volitality from crsp daily stock file. But my problem is that the observations go from 36 thousand to more than 1 billion. And there are many same results, with same lpermno, same fyear and same lstkvol. I couldn't find the reason. Thanks!
create table exe_ccn_lancmpcrsp as
select a.*,std(b.RET)*sqrt(250) as lstkvol "lag annualized stock volitality"
from exe_ccn_lancmpllq a left join crspa.DSF b
on a.lpermno=b.permno and b.DATE between a.lfybegdt and a.lfyenddt
group by a.lpermno,a.fyear
having sum(b.RET is not missing)>1;
3 weeks ago - last edited 2 weeks ago
But my problem is that the observations go from 36 thousand to more than 1 billion. And there are many same results, with same lpermno, same fyear and same lstkvol. I couldn't find the reason.
I take it that your dataset A has about 36000 observations, but you are wondering why the table resulting from this left join consists of >1 billion observations and that this is mainly due to duplicate observations ("same results, with same lpermno, ...").
The reason is that "The query requires remerging summary statistics back with the original data." -- as the SAS log must have told you. To avoid the duplicates, you can either insert the keyword distinct after "select" or – better – extend the group by clause by all other variables from A (in particular a.lfybegdt, a.lfyenddt). The advantage of extending the group by clause is that remerging does not occur (see SAS log), which reduces run time.
3 weeks ago
To add to @FreelanceReinhard's excellent suggestion...
Why a left join with the having condition sum(b.RET is not missing)>1. Table exe_ccn_lancmpllq records without a match in crspa.DSF will have all RET values missing, so they will be dropped by the having condition. You might as well use an inner join.