Hi I have this code
%let K=6;
data thesis.test7;
set thesis.test2;
array returns SPY--AFL;
do i= 1+&K to dim(returns);
returns[i]=(returns[i]-lag(returns[i]))/lag(returns[i]);
end;
run;
I would like the return computation to start at row 1+K and have the previous rows blank but it starts at column 1+K with the stock prices shown for previous columns.
What am I doing wrong?
Thanks in advance.
The automatic variable _n_ has the row number and can be used for such processing:
data thesis.test7;
set thesis.test2;
array returns SPY--AFL;
if _n_ > &k then do i= 1 to dim(returns);
returns[i]=(returns[i]-lag(returns[i]))/lag(returns[i]);
end;
run;
HOWEVER, the lag function, when it executes for row 7 all the lagged values will be missing because that is the first time the lag function is called.
You might be better off creating an array of non-conditional lagged values and reference that array instead of using lag(returns[i])
To you mean to reverse the order of processing (AFL first and SPY last)? Easiest would be to change the index of the do loop:
Do i = dim(returns) to 1 by (-1);
If you are thinking of something else you may need to go into more details and the reasons why. Preferrably in a separate topic.
Better post your data as text , No one would like to type it for you . And most important thing is post your output too .
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.