BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mei
Calcite | Level 5 mei
Calcite | Level 5

I would like to calculate lag and forward value of stock return:

how do write program for return of lag 1 (t-1), lag 2 (t-2), lag 3 (t-3), lag 4 (t-4)

how about prog for return for forward period that is ret for t+1, t+2

thanks

mei

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

mei,

You are likely to need variable names to hold each new piece of information.  Try this:

data want;

   set have nobs=_nobs_;

   back_1 = lag1(stock_return);

   back_2 = lag2(stock_return);

   back_3 = lag3(stock_return);

   back_4 = lag4(stock_return);

   if _n_ < _nobs_ then set have (keep=stock_return rename=(stock_return=forward_1) firstobs=2);

   if _n_ < _nobs_-1 then set have (keep=stock_return rename=(stock_return=forward_2) firstobs=3);

run;

Good luck.


View solution in original post

8 REPLIES 8
Ksharp
Super User

Maybe function dif() is what you need.Check the documentation.

dif(t)  dif2(t) ....................

Ksharp

Haikuo
Onyx | Level 15

if you have ETS, then proc expand seems to meet your needs, for both lag and lead. Esp. when you deal with stock market data, proc expand has a nice set of arsenal ready for launch, such moving average among many others.

Haikuo

mei
Calcite | Level 5 mei
Calcite | Level 5

how proc expand works for lead and lags? can you show me the programming ?

What is ETS?

Astounding
PROC Star

mei,

You are likely to need variable names to hold each new piece of information.  Try this:

data want;

   set have nobs=_nobs_;

   back_1 = lag1(stock_return);

   back_2 = lag2(stock_return);

   back_3 = lag3(stock_return);

   back_4 = lag4(stock_return);

   if _n_ < _nobs_ then set have (keep=stock_return rename=(stock_return=forward_1) firstobs=2);

   if _n_ < _nobs_-1 then set have (keep=stock_return rename=(stock_return=forward_2) firstobs=3);

run;

Good luck.


mei
Calcite | Level 5 mei
Calcite | Level 5

To avoid "lag" function to give me the "lag value" from last company if you don't put any restriction,

I have inserted this:

"
if permno ne lag1(permno) then lag1_idiorisk = .;

if permno ne lag2(permno) then lag2_idiorisk = .;

if permno ne lag3(permno) then lag3_idiorisk = .;

if permno ne lag4(permno) then lag4_idiorisk = .;

run;  "

so now the program read as

data vwretd.sorted_main_vwretd_test;

   set vwretd.sorted_main_vwretd_test nobs=_nobs_;

   lag1_idiorisk = lag1(idiorisk);

   lag2_idiorisk = lag2(idiorisk);

   lag3_idiorisk = lag3(idiorisk);

   lag4_idiorisk = lag4(idiorisk);

if permno ne lag1(permno) then lag1_idiorisk = .;

if permno ne lag2(permno) then lag2_idiorisk = .;

if permno ne lag3(permno) then lag3_idiorisk = .;

if permno ne lag4(permno) then lag4_idiorisk = .;

run;

if _n_ < _nobs_ then set vwretd.sorted_main_test (keep=idiorisk rename=(idiorisk=fwd1_idiorisk) firstobs=2);

   if _n_ < _nobs_-1 then set vwretd.sorted_main_test (keep=idiorisk rename=(idiorisk=fwd2_idiorisk) firstobs=3);

run;

Question:

I m not sure how to put the restrictions on the forward value to restrict the company pick the forward value from the previous permno.

Pls help.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 1491 views
  • 7 likes
  • 6 in conversation