BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
opti_miser
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
opti_miser
Calcite | Level 5

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

1 REPLY 1
opti_miser
Calcite | Level 5

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

Multiple Linear Regression in SAS

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.

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