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
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.
Maybe function dif() is what you need.Check the documentation.
dif(t) dif2(t) ....................
Ksharp
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
how proc expand works for lead and lags? can you show me the programming ?
What is ETS?
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.