DATA Step, Macro, Functions and more

Repeated Calculation across leading observations

Reply
Learner
Posts: 1

Repeated Calculation across leading observations

Using SAS 9.4, I want to calculate compound company stock returns following intermittent director trades. Many companies have director trades on consecutive days or several days apart, yet the compounding period extends up to 60 days. 

 

There are 2 compounding periods for the calculation of returns:

  1. Until the director-trade announcement date plus one day, usually up to 6 days.
  2. 60 days after the director trade.

 

A simplified data sample looks like this:

 

Company

date

director_tradeID

Return+1

trade_announcementdate

return_count

AAA

30APR2010

 

1

 

0

AAA

3MAY2010

a

1

06MAY2010

3

AAA

4MAY2010

 

1

 

1

AAA

05MAY2010

b

1.01

05MAY2010

1

AAA

06MAY2010

c

0.98

12MAY2010

1

AAA

7MAY2010

 

0.88

 

1

AAA

10MAY2010

 

1.22

 

3

AAA (up to 1,500 observations)

   

 

 

 

 

 

AAB

     

AAB

     

(similar data to AAA)

 

Where:

company

date = trading date:  thinly traded stocks have missing dates – no trade means no observation.

Return+1 = value of stock return plus 1. Return value may be for a period of more than one day.

director_tradeID = = signals a director’s trade

trade_announcement_date = usually up to 6 days after trading date.

return_count = the number of days between consecutive observations

 

 

My preliminary code for calculating compound returns up to 60 observations from the date of director trade, and the error messages, are as follows:

 

Data returns2 ;

Set returns;

By company date;

if (director_tradeID ' ') then do;

x =  _n_  ;

y = (_n_ + 60);

end;

call symputx ('firstobserv', x);

call symputx ('observ', y);

 

Set returns   (firstobs = &firstobserv   obs = &observ  ) ;

if first.company then do;

compoundreturn_plus1 = 1;

compoundreturn_daycount  = 0;

End;

 

Else do;

compoundreturn_plus1 = (compoundreturn_plus1 * return);

compoundreturn_daycount  = (compoundreturn_daycount  + return_count);

 

End;

Retain compoundreturn_plus1 compoundreturn_daycount;

Run;

 

The identical error messages for the firstobs and obs options are:

ERROR 23-7: Invalid value for FIRSTOBS option

ERROR: Invalid number conversion on &.

WARNING: Apparent symbolic reference FIRSTOBSERV not resolved.

NOTE: The SAS System stopped processing this step because of errors.

 

Proc sql methods to calculate returns may be more efficient, so I’m open to alternate suggestions.

Many thanks,

TT. 

Super Contributor
Posts: 308

Re: Repeated Calculation across leading observations

Posted in reply to tangotrain

Hello,

 

The macro variables within call symput functions can not be used in the same data step.

 

Please read the section

Problem Trying to Reference a SYMPUT-Assigned Value Before It Is Available

from this link:

 

http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000210266.htm

Ask a Question
Discussion stats
  • 1 reply
  • 181 views
  • 1 like
  • 2 in conversation