Solved
Contributor
Posts: 62

# lag

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

Accepted Solutions
Solution
‎04-09-2012 11:24 AM
Super User
Posts: 6,785

## Re: lag

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.

All Replies
Frequent Contributor
Posts: 139

Super User
Posts: 10,784

## Re: lag

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

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

Ksharp

Regular Contributor
Posts: 184

## Re: lag

Respected Advisor
Posts: 3,167

## Re: lag

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

Contributor
Posts: 62

## Re: lag

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

What is ETS?

Frequent Contributor
Posts: 139

## Re: lag

Solution
‎04-09-2012 11:24 AM
Super User
Posts: 6,785

## Re: lag

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.

Contributor
Posts: 62

## Re: lag

Posted in reply to Astounding

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.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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