I am trying to create a lagged variable with arrays in proc optmodel. I receive an error message saying that array subscripts which are missing are invalid. It seems that there should be some line of code to say not to create the lagged variable in the cases in which the array subscript is missing. The optmodel language is new to me, and I don't know what statements are necessary to do this.
Here is some code I tried (also attached):
*Ideally, I would like to write the program using just one date variable, as follows.;
proc optmodel;
*create a numerical index based on permno (stock identifier) and date, read in below;
set <num,num> STOCKS_DATES;
*create another numerical index just based on permnos;
set STOCKS = setof {<i,t> in STOCKS_DATES} i;
*create another numerical index just based on dates;
set DATES = setof {<i,t> in STOCKS_DATES} t;
*name variables that will be read in from the data set;
num weight {STOCKS_DATES};
*read in the sas data set;
read data optmom.sample_data into STOCKS_DATES=[permno date]
weight;
num beg_wgt {STOCKS_DATES};
*create variable beg_wgt for each stock equal to the lagged value of weight. ;
for {<i,t> in STOCKS_DATES} do; if t=1 then beg_wgt[i,t]=0; if t>1 then beg_wgt[i,t]=weight[i,t-1]; else; end;
quit;
*Otherwise, I could create a second date variable unique to each stock, as follows.;
*The "date" variable is necessary because I will need to aggregate over dates for all stocks.;
proc optmodel;
*create a numerical index based on permno (stock identifier), date, and unique date, read in below;
set <num,num,num> STOCKS_FDATES_DATES;
*create another numerical index just based on permnos;
set STOCKS = setof {<i,f,t> in STOCKS_FDATES_DATES} i;
*create another numerical index just based on dates;
set FDATES = setof {<i,f,t> in STOCKS_FDATES_DATES} f;
*create another numerical index just based on dates unique to each permno;
set DATES = setof {<i,f,t> in STOCKS_FDATES_DATES} t;
*name variables that will be read in from the data set;
num weight {STOCKS_FDATES_DATES};
*read in the sas data set;
read data optmom.sample_data into STOCKS_FDATES_DATES=[permno firmdate date]
weight;
*Here is one attempt at what I thought might work.;
num beg_wgt {i in STOCKS, f in FDATES};
*create variable beg_wgt for each stock equal to the lagged value of weight. ;
for {i in STOCKS, f in FDATES} do; if f=1 then beg_wgt[i,f]=0; if f>1 then beg_wgt[i,f]=weight[i,f-1,t]; else; end;
*Here is another attempt.;
num beg_wgt2 {<i,f,t> in STOCKS_FDATES_DATES};
*create variable beg_wgt for each stock equal to the lagged value of weight. ;
for {<i,f,t> in STOCKS_FDATES_DATES} do; if f=1 then beg_wgt2[i,f,t]=0; if f>1 then beg_wgt2[i,f,t]=weight[i,f-1,t]; else; end;
quit;
Here is the code that solves the problem!
proc optmodel;
*create a numerical index based on permno (stock identifier) and date, read in below;
set <num,num> STOCKS_DATES;
*create another numerical index just based on permnos;
set STOCKS = setof {<i,t> in STOCKS_DATES} i;
*create another numerical index just based on dates;
set DATES = setof {<i,t> in STOCKS_DATES} t;
*name variables that will be read in from the data set;
num weight {STOCKS_DATES};
*read in the sas data set;
read data optmom.sample_data into STOCKS_DATES=[permno date]
weight;
num beg_wgt {<i, t> in STOCKS_DATES};
*create variable beg_wgt for each stock equal to the lagged value of weight. ;
for {<i,t> in STOCKS_DATES} do; if <i,t-1> in STOCKS_DATES then beg_wgt[i,t]=weight[i,t-1]; else beg_wgt[i,t]=0; end;
*the code just above is the key;
Here is the code that solves the problem!
proc optmodel;
*create a numerical index based on permno (stock identifier) and date, read in below;
set <num,num> STOCKS_DATES;
*create another numerical index just based on permnos;
set STOCKS = setof {<i,t> in STOCKS_DATES} i;
*create another numerical index just based on dates;
set DATES = setof {<i,t> in STOCKS_DATES} t;
*name variables that will be read in from the data set;
num weight {STOCKS_DATES};
*read in the sas data set;
read data optmom.sample_data into STOCKS_DATES=[permno date]
weight;
num beg_wgt {<i, t> in STOCKS_DATES};
*create variable beg_wgt for each stock equal to the lagged value of weight. ;
for {<i,t> in STOCKS_DATES} do; if <i,t-1> in STOCKS_DATES then beg_wgt[i,t]=weight[i,t-1]; else beg_wgt[i,t]=0; end;
*the code just above is the key;
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 how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.