Thanks. I will try the code and let you know. I did not read the documentation at this moment. Will you please send me the link?
Hi again, I need to calculate ca at time t+1 as well. So, ca values at t-1, t and t+1.
I should have guessed. :smileyplain: I should have asked before now. Do you have SAS/ETS software? Type in PROC EXPAND; RUN; and see what your messages are. If that works consult the documentation. It does lags leads and a lot more.
This sure sounds like a FIFO problem. What do you need to do with the lagged data once you get them?
The following keeps the current and up to the last three values for each variable and then calculates the average of the up to 4 values:
data want (drop=counter);
set have;
by companyISIN notsorted;
retain counter .;
array ca_stack {0:3};
array cl_stack {0:3};
array stdebt_stack {0:3};
retain ca_stack cl_stack stdebt_stack;
if first.companyISIN then do;
call missing(of ca_stack{*});
call missing(of cl_stack{*});
call missing(of stdebt_stack{*});
counter=0;
end;
counter+1;
ca_stack{mod(counter,4)} = ca;
cl_stack{mod(counter,4)} = cl;
stdebt_stack{mod(counter,4)} = stdebt;
if year not in (2003,2013) then do;
ca_avg = mean(of ca_stack{*});
cl_avg = mean(of cl_stack{*});
stdebt_avg = mean(of stdebt_stack{*});
output;
end;
run;
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.