DATA Step, Macro, Functions and more

proc sql, group by

Reply
New Contributor
Posts: 3

proc sql, group by

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!

proc sql;
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;
quit;

 

PROC Star
Posts: 1,283

Re: proc sql, group by

Posted in reply to zilong567

I don't understand your problem? Please elaborate.

Trusted Advisor
Posts: 1,247

Re: proc sql, group by

[ Edited ]
Posted in reply to zilong567

@zilong567 wrote:

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.

Esteemed Advisor
Posts: 5,526

Re: proc sql, group by

Posted in reply to zilong567

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.

PG
Ask a Question
Discussion stats
  • 3 replies
  • 98 views
  • 0 likes
  • 4 in conversation