I want to calculate 3 lag values of invest i.e. lag1, lag2, lag3, but the problem is some of the years in my data are missing, how could it be treated
Instead of using LAG it will be easier to transpose the dataset and then
define array of yearly invests. Try:
proc transpose data=have out=temp(drop=_name_) profix=yr; by CUSIP; id YEAR; var INVEST; run;
array y_invest yr2000-yr2015;
... any other code to do the job ...
I take it that you only want exact 1-year, 2-year, and 3-year lags. This program does that.
Note I use nested IFN's. That's because every alternative outcome in an IFN is evaluated, whether the if-condition is true or not (i.e. regardless of which outcome is chosen). This ensures that the fifo queues underlying the LAG functions are always properly synchronized.
data want (drop=seq);
if first.cusip then seq=1;
Now, if you only want to keep years which have a complete set of non-missing lags, then put a subsetting IF statement
just before the RUN statement.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.