Operations Research topics: SAS/OR,
SAS Optimization, and SAS Simulation Studio

trying to create a lagged variable with arrays in proc optmodel

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

trying to create a lagged variable with arrays in proc optmodel

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;

Attachment
Attachment
Attachment

Accepted Solutions
Solution
‎06-17-2013 04:23 PM
Contributor
Posts: 22

Re: trying to create a lagged variable with arrays in proc optmodel

Here is the code that solves the problem! Smiley Happy

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;

View solution in original post


All Replies
Solution
‎06-17-2013 04:23 PM
Contributor
Posts: 22

Re: trying to create a lagged variable with arrays in proc optmodel

Here is the code that solves the problem! Smiley Happy

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;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 334 views
  • 0 likes
  • 1 in conversation