<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic trying to create a lagged variable with arrays in proc optmodel in Mathematical Optimization, Discrete-Event Simulation, and OR</title>
    <link>https://communities.sas.com/t5/Mathematical-Optimization/trying-to-create-a-lagged-variable-with-arrays-in-proc-optmodel/m-p/109093#M656</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am trying to create a lagged variable with arrays in proc optmodel.&amp;nbsp; I receive an error message saying that array subscripts which are missing are invalid.&amp;nbsp; 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.&amp;nbsp; The optmodel language is new to me, and I don't know what statements are necessary to do this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is some code I tried (also attached):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*Ideally, I would like to write the program using just one date variable, as follows.;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc optmodel;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create a numerical index based on permno (stock identifier) and date, read in below;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set &amp;lt;num,num&amp;gt; STOCKS_DATES;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create another numerical index just based on permnos;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set STOCKS = setof {&amp;lt;i,t&amp;gt; in STOCKS_DATES} i;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create another numerical index just based on dates;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set DATES = setof {&amp;lt;i,t&amp;gt; in STOCKS_DATES} t;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *name variables that will be read in from the data set;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num weight {STOCKS_DATES};&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *read in the sas data set;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; read data optmom.sample_data into STOCKS_DATES=[permno date]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; weight;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num beg_wgt {STOCKS_DATES};&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create variable beg_wgt for each stock equal to the lagged value of weight. ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for {&amp;lt;i,t&amp;gt; in STOCKS_DATES} do; if t=1 then beg_wgt[i,t]=0; if t&amp;gt;1 then beg_wgt[i,t]=weight[i,t-1]; else;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*Otherwise, I could create a second date variable unique to each stock, as follows.;&lt;/P&gt;&lt;P&gt;*The "date" variable is necessary because I will need to aggregate over dates for all stocks.;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc optmodel;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create a numerical index based on permno (stock identifier), date, and unique date, read in below;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set &amp;lt;num,num,num&amp;gt; STOCKS_FDATES_DATES;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create another numerical index just based on permnos;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set STOCKS = setof {&amp;lt;i,f,t&amp;gt; in STOCKS_FDATES_DATES} i;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create another numerical index just based on dates;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set FDATES = setof {&amp;lt;i,f,t&amp;gt; in STOCKS_FDATES_DATES} f;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create another numerical index just based on dates unique to each permno;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set DATES = setof {&amp;lt;i,f,t&amp;gt; in STOCKS_FDATES_DATES} t;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *name variables that will be read in from the data set;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num weight {STOCKS_FDATES_DATES};&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *read in the sas data set;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; read data optmom.sample_data into STOCKS_FDATES_DATES=[permno firmdate date]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; weight;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*Here is one attempt at what I thought might work.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num beg_wgt {i in STOCKS, f in FDATES};&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create variable beg_wgt for each stock equal to the lagged value of weight. ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for {i in STOCKS, f in FDATES} do; if f=1 then beg_wgt[i,f]=0; if f&amp;gt;1 then beg_wgt[i,f]=weight[i,f-1,t]; else;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*Here is another attempt.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num beg_wgt2 {&amp;lt;i,f,t&amp;gt; in STOCKS_FDATES_DATES};&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create variable beg_wgt for each stock equal to the lagged value of weight. ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for {&amp;lt;i,f,t&amp;gt; in STOCKS_FDATES_DATES} do; if f=1 then beg_wgt2[i,f,t]=0; if f&amp;gt;1 then beg_wgt2[i,f,t]=weight[i,f-1,t]; else;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 17 Jun 2013 15:41:47 GMT</pubDate>
    <dc:creator>opti_miser</dc:creator>
    <dc:date>2013-06-17T15:41:47Z</dc:date>
    <item>
      <title>trying to create a lagged variable with arrays in proc optmodel</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/trying-to-create-a-lagged-variable-with-arrays-in-proc-optmodel/m-p/109093#M656</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am trying to create a lagged variable with arrays in proc optmodel.&amp;nbsp; I receive an error message saying that array subscripts which are missing are invalid.&amp;nbsp; 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.&amp;nbsp; The optmodel language is new to me, and I don't know what statements are necessary to do this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is some code I tried (also attached):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*Ideally, I would like to write the program using just one date variable, as follows.;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc optmodel;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create a numerical index based on permno (stock identifier) and date, read in below;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set &amp;lt;num,num&amp;gt; STOCKS_DATES;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create another numerical index just based on permnos;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set STOCKS = setof {&amp;lt;i,t&amp;gt; in STOCKS_DATES} i;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create another numerical index just based on dates;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set DATES = setof {&amp;lt;i,t&amp;gt; in STOCKS_DATES} t;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *name variables that will be read in from the data set;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num weight {STOCKS_DATES};&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *read in the sas data set;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; read data optmom.sample_data into STOCKS_DATES=[permno date]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; weight;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num beg_wgt {STOCKS_DATES};&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create variable beg_wgt for each stock equal to the lagged value of weight. ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for {&amp;lt;i,t&amp;gt; in STOCKS_DATES} do; if t=1 then beg_wgt[i,t]=0; if t&amp;gt;1 then beg_wgt[i,t]=weight[i,t-1]; else;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*Otherwise, I could create a second date variable unique to each stock, as follows.;&lt;/P&gt;&lt;P&gt;*The "date" variable is necessary because I will need to aggregate over dates for all stocks.;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc optmodel;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create a numerical index based on permno (stock identifier), date, and unique date, read in below;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set &amp;lt;num,num,num&amp;gt; STOCKS_FDATES_DATES;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create another numerical index just based on permnos;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set STOCKS = setof {&amp;lt;i,f,t&amp;gt; in STOCKS_FDATES_DATES} i;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create another numerical index just based on dates;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set FDATES = setof {&amp;lt;i,f,t&amp;gt; in STOCKS_FDATES_DATES} f;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create another numerical index just based on dates unique to each permno;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set DATES = setof {&amp;lt;i,f,t&amp;gt; in STOCKS_FDATES_DATES} t;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *name variables that will be read in from the data set;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num weight {STOCKS_FDATES_DATES};&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *read in the sas data set;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; read data optmom.sample_data into STOCKS_FDATES_DATES=[permno firmdate date]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; weight;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*Here is one attempt at what I thought might work.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num beg_wgt {i in STOCKS, f in FDATES};&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create variable beg_wgt for each stock equal to the lagged value of weight. ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for {i in STOCKS, f in FDATES} do; if f=1 then beg_wgt[i,f]=0; if f&amp;gt;1 then beg_wgt[i,f]=weight[i,f-1,t]; else;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*Here is another attempt.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num beg_wgt2 {&amp;lt;i,f,t&amp;gt; in STOCKS_FDATES_DATES};&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create variable beg_wgt for each stock equal to the lagged value of weight. ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for {&amp;lt;i,f,t&amp;gt; in STOCKS_FDATES_DATES} do; if f=1 then beg_wgt2[i,f,t]=0; if f&amp;gt;1 then beg_wgt2[i,f,t]=weight[i,f-1,t]; else;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Jun 2013 15:41:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/trying-to-create-a-lagged-variable-with-arrays-in-proc-optmodel/m-p/109093#M656</guid>
      <dc:creator>opti_miser</dc:creator>
      <dc:date>2013-06-17T15:41:47Z</dc:date>
    </item>
    <item>
      <title>Re: trying to create a lagged variable with arrays in proc optmodel</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/trying-to-create-a-lagged-variable-with-arrays-in-proc-optmodel/m-p/109094#M657</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Here is the code that solves the problem! &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc optmodel;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create a numerical index based on permno (stock identifier) and date, read in below;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set &amp;lt;num,num&amp;gt; STOCKS_DATES;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create another numerical index just based on permnos;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set STOCKS = setof {&amp;lt;i,t&amp;gt; in STOCKS_DATES} i;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create another numerical index just based on dates;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set DATES = setof {&amp;lt;i,t&amp;gt; in STOCKS_DATES} t;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *name variables that will be read in from the data set;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; num weight {STOCKS_DATES};&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *read in the sas data set;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; read data optmom.sample_data into STOCKS_DATES=[permno date]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; weight;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; num beg_wgt {&amp;lt;i, t&amp;gt; in STOCKS_DATES}; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *create variable beg_wgt for each stock equal to the lagged value of weight. ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for {&amp;lt;i,t&amp;gt; in STOCKS_DATES} do; if &amp;lt;i,t-1&amp;gt; in STOCKS_DATES then beg_wgt[i,t]=weight[i,t-1]; else beg_wgt[i,t]=0;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*the code just above is the key;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Jun 2013 20:23:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/trying-to-create-a-lagged-variable-with-arrays-in-proc-optmodel/m-p/109094#M657</guid>
      <dc:creator>opti_miser</dc:creator>
      <dc:date>2013-06-17T20:23:03Z</dc:date>
    </item>
  </channel>
</rss>

