<?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 Re: macro to read data and declare VAR and IMPVAR in Mathematical Optimization, Discrete-Event Simulation, and OR</title>
    <link>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79263#M554</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you Tom!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 11 Jun 2012 13:50:50 GMT</pubDate>
    <dc:creator>hadesmr</dc:creator>
    <dc:date>2012-06-11T13:50:50Z</dc:date>
    <item>
      <title>macro to read data and declare VAR and IMPVAR</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79256#M547</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;i have a original dataset "M" like this:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data M;&lt;BR /&gt;input week dep m1 m2 m3;&lt;BR /&gt;cards;&lt;BR /&gt;1 100 12 89 6&lt;BR /&gt;2 200 61 34 77&lt;BR /&gt;3 300 71 69 55&lt;BR /&gt;4 400 18 10 40&lt;BR /&gt;5 500 18 14 66&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc optmodel;&lt;BR /&gt; set w;&lt;BR /&gt; num weeks{w};&lt;BR /&gt; num dep{w};&lt;BR /&gt; num m1{w};&lt;BR /&gt; num m2{w};&lt;BR /&gt; num m3{w};&lt;BR /&gt;read data M into w = [_n_] dep m1 m2 m3;&lt;BR /&gt;var wt1{w} &amp;gt;= 0 &amp;lt;= 1 init 1;&lt;BR /&gt;var wt2{w} &amp;gt;= 0 &amp;lt;= 1 init 1;&lt;BR /&gt;var wt3{w} &amp;gt;= 0 &amp;lt;= 1 init 1;&lt;BR /&gt;impvar sp1 {i in w} = m1&lt;I&gt; * wt3&lt;I&gt;;&lt;BR /&gt;impvar sp2 {i in w} = m2&lt;I&gt; * wt3&lt;I&gt;;&lt;BR /&gt;impvar sp3 {i in w} = m3&lt;I&gt; * wt3&lt;I&gt;;&lt;BR /&gt;print sp1 sp2 sp3;&lt;BR /&gt;quit;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;what i am trying to do here within Proc Optmodel is:&lt;/P&gt;&lt;P&gt;1. read m1 m2 and m3 and dep from dataset M;&lt;/P&gt;&lt;P&gt;2. declare VAR: wt1 wt2 and wt3;&lt;/P&gt;&lt;P&gt;3. decalre IMPVAR: sp1 (m1*wt1) sp2 (m2*wt2) and sp3 (m3*wt3);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;i know how to do it in a slow way as above. However, in the real dataset i have 100 variables (m1, m2, ..m100) and thus will have 100 wt variables (VAR) and sp variables (impvar). Is there any macro or faster/smarter way to achieve the bove 3 steps?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks a lot!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE border="0" cellpadding="0" cellspacing="0" width="704"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" width="64"&gt;week&lt;/TD&gt;&lt;TD class="xl63" width="64"&gt;dep&lt;/TD&gt;&lt;TD class="xl63" width="64"&gt;m1&lt;/TD&gt;&lt;TD class="xl63" width="64"&gt;m2&lt;/TD&gt;&lt;TD class="xl63" width="64"&gt;m3&lt;/TD&gt;&lt;TD class="xl63" width="64"&gt;wt1&lt;/TD&gt;&lt;TD class="xl63" width="64"&gt;wt2&lt;/TD&gt;&lt;TD class="xl63" width="64"&gt;wt3&lt;/TD&gt;&lt;TD class="xl63" width="64"&gt;sp1&lt;/TD&gt;&lt;TD class="xl63" width="64"&gt;sp2&lt;/TD&gt;&lt;TD class="xl63" width="64"&gt;sp3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD align="right" class="xl63" height="20" width="64"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;100&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;12&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;89&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;6&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;2&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;3&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;12&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;178&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;18&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD align="right" class="xl63" height="20" width="64"&gt;2&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;200&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;61&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;34&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;77&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;2&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;3&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;61&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;68&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;231&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD align="right" class="xl63" height="20" width="64"&gt;3&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;300&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;71&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;69&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;55&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;2&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;3&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;71&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;138&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;165&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD align="right" class="xl63" height="20" width="64"&gt;4&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;400&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;18&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;10&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;40&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;2&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;3&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;18&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;20&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;120&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD align="right" class="xl63" height="20" width="64"&gt;5&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;500&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;18&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;14&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;66&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;2&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;3&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;18&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;28&lt;/TD&gt;&lt;TD align="right" class="xl63" width="64"&gt;198&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 08 Jun 2012 20:46:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79256#M547</guid>
      <dc:creator>hadesmr</dc:creator>
      <dc:date>2012-06-08T20:46:05Z</dc:date>
    </item>
    <item>
      <title>Re: macro to read data and declare VAR and IMPVAR</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79257#M548</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If you really have such a simple variable naming convention then simple iterative %DO loops should work fine.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%macro doit(N);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;proc optmodel;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;set w;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;num weeks{w};&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;num dep{w};&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%do j=1 %to &amp;amp;n;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; num m&amp;amp;j{w};&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;read data M into w = [_n_] dep&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%do j=1 %to &amp;amp;n; m&amp;amp;j %end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%do j=1 %to &amp;amp;n;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; var wt&amp;amp;j{w} &amp;gt;= 0 &amp;lt;= 1 init 1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%do j=1 %to &amp;amp;n;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; impvar sp&amp;amp;j {i in w} = m&amp;amp;j&lt;I&gt; * wt&amp;amp;n&lt;I&gt;;&lt;/I&gt;&lt;/I&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;print&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%do j=1 %to &amp;amp;n; sp&amp;amp;j %end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%mend doit;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%doit(3);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;MPRINT(DOIT):&amp;nbsp;&amp;nbsp; proc optmodel;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;MPRINT(DOIT):&amp;nbsp;&amp;nbsp; set w;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;MPRINT(DOIT):&amp;nbsp;&amp;nbsp; num weeks{w};&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;MPRINT(DOIT):&amp;nbsp;&amp;nbsp; num dep{w};&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;MPRINT(DOIT):&amp;nbsp;&amp;nbsp; num m1{w};&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;MPRINT(DOIT):&amp;nbsp;&amp;nbsp; num m2{w};&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;MPRINT(DOIT):&amp;nbsp;&amp;nbsp; num m3{w};&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;MPRINT(DOIT):&amp;nbsp;&amp;nbsp; read data M into w = [_n_] dep m1 m2 m3 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;MPRINT(DOIT):&amp;nbsp;&amp;nbsp; var wt1{w} &amp;gt;= 0 &amp;lt;= 1 init 1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;MPRINT(DOIT):&amp;nbsp;&amp;nbsp; var wt2{w} &amp;gt;= 0 &amp;lt;= 1 init 1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;MPRINT(DOIT):&amp;nbsp;&amp;nbsp; var wt3{w} &amp;gt;= 0 &amp;lt;= 1 init 1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;MPRINT(DOIT):&amp;nbsp;&amp;nbsp; impvar sp1 {i in w} = m1&lt;I&gt; * wt3&lt;I&gt;;&lt;/I&gt;&lt;/I&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;MPRINT(DOIT):&amp;nbsp;&amp;nbsp; impvar sp2 {i in w} = m2&lt;I&gt; * wt3&lt;I&gt;;&lt;/I&gt;&lt;/I&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;MPRINT(DOIT):&amp;nbsp;&amp;nbsp; impvar sp3 {i in w} = m3&lt;I&gt; * wt3&lt;I&gt;;&lt;/I&gt;&lt;/I&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;MPRINT(DOIT):&amp;nbsp;&amp;nbsp; print sp1 sp2 sp3 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;MPRINT(DOIT):&amp;nbsp;&amp;nbsp; quit;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 08 Jun 2012 22:39:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79257#M548</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2012-06-08T22:39:14Z</dc:date>
    </item>
    <item>
      <title>Re: macro to read data and declare VAR and IMPVAR</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79258#M549</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;thanks Tom,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;one more question, what if i need to create two more impvar in the code, such that &lt;/P&gt;&lt;P&gt;impvar wt_sum {i in w} = wt1&lt;I&gt; + wt2&lt;I&gt; + wt3&lt;I&gt;;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;impvar sp_sum {i in w} = sp1&lt;I&gt; + sp2&lt;I&gt; + sp3&lt;I&gt;;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;any macro to do that? thanks!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;by the way, the original variable names are not that simple but i have a macro to change all the variables in order to be macro-friendly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanks a lot and have a good weekend!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 09 Jun 2012 03:21:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79258#M549</guid>
      <dc:creator>hadesmr</dc:creator>
      <dc:date>2012-06-09T03:21:24Z</dc:date>
    </item>
    <item>
      <title>Re: macro to read data and declare VAR and IMPVAR</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79259#M550</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You could create those statements with these loops.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;impvar wt_sum {i in w} = &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; %do j=1 %to &amp;amp;n; wt&amp;amp;j&lt;I&gt; %if &amp;amp;j&amp;lt;&amp;amp;n %then + ; %end;&lt;/I&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;impvar sp_sum {i in w} =&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; %do j=1 %to &amp;amp;n; sp&amp;amp;j&lt;I&gt; %if &amp;amp;j&amp;lt;&amp;amp;n %then + ; %end;&lt;/I&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 09 Jun 2012 14:00:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79259#M550</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2012-06-09T14:00:18Z</dc:date>
    </item>
    <item>
      <title>Re: macro to read data and declare VAR and IMPVAR</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79260#M551</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Here's an alternative approach that uses fewer PROC OPTMODEL statements:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let n = 3;&lt;/P&gt;&lt;P&gt;proc optmodel;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num n = &amp;amp;n;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set w;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num weeks{w};&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num dep{w};&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num m{w,1..n};&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; read data M into w = [_n_] dep {j in 1..n} &amp;lt;m[_N_,j]=col('m'||j)&amp;gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var wt {w,1..n} &amp;gt;= 0 &amp;lt;= 1 init 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; impvar sp {i in w, j in 1..n} = m[i,j] * wt[i,j];&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print sp;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 09 Jun 2012 14:53:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79260#M551</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2012-06-09T14:53:50Z</dc:date>
    </item>
    <item>
      <title>Re: macro to read data and declare VAR and IMPVAR</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79261#M552</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;And here's how you can do the sums if you declare wt and sp as in my earlier reply:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; impvar wt_sum {i in w} = sum {j in 1..n} wt[i,j];&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; impvar sp_sum {i in w} = sum {j in 1..n} sp[i,j];&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 09 Jun 2012 14:59:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79261#M552</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2012-06-09T14:59:33Z</dc:date>
    </item>
    <item>
      <title>Re: macro to read data and declare VAR and IMPVAR</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79262#M553</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you RobPratt!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 11 Jun 2012 13:50:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79262#M553</guid>
      <dc:creator>hadesmr</dc:creator>
      <dc:date>2012-06-11T13:50:29Z</dc:date>
    </item>
    <item>
      <title>Re: macro to read data and declare VAR and IMPVAR</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79263#M554</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you Tom!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 11 Jun 2012 13:50:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79263#M554</guid>
      <dc:creator>hadesmr</dc:creator>
      <dc:date>2012-06-11T13:50:50Z</dc:date>
    </item>
    <item>
      <title>Re: macro to read data and declare VAR and IMPVAR</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79264#M555</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hey RobPratt,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A follow up question. Below is the code based on your advice. It works fine but i am wondering if there is a way to simply VAR wt a little bit highlighted in RED.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;what i am trying to do here is to create a weight factor (VAR wt) with 2 constrains: 1), sum of wt1, wt2 and wt3 equals to 1; 2). weighting factors are same across all the weeks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I feel like i don't need "w" in wt {w, 1..n} but don't know how to make the change.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;by the way, if i can't make the above change, how to create a dataset only for wt {w, 1..n} in week1?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks again for your help!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data M;&lt;BR /&gt;input week dep m1 m2 m3;&lt;BR /&gt;cards;&lt;BR /&gt;1 100 12 89 6&lt;BR /&gt;2 200 61 34 77&lt;BR /&gt;3 300 71 69 55&lt;BR /&gt;4 400 18 10 40&lt;BR /&gt;5 500 18 14 66&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let n = 3;&lt;/P&gt;&lt;P&gt;proc optmodel;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num n = &amp;amp;n;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set w;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num weeks{w};&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num dep{w};&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num m{w,1..n};&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; read data M into w = [_N_] dep {j in 1..n} &amp;lt;m[_N_,j]=col('m'||j)&amp;gt;;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #ff0000;"&gt;var wt {w,1..n} &amp;gt;= 0 &amp;lt;= 1 init 1;&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; impvar prdt {i in w, j in 1..n} = m[i,j] * wt[i,j];&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; impvar swt {i in w} = sum {j in 1..n} wt[i,j];&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; impvar sp {i in w} = sum {j in 1..n} prdt[i,j];&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num wks;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; wks = card (w);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; impvar correlation = (wks*sum{i in w}(sp&lt;I&gt;*dep&lt;I&gt;) - (sum{i in w}(sp&lt;I&gt;)) * (sum{i in w}(dep&lt;I&gt;)))&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; / (sqrt(wks*sum{i in w}(sp&lt;I&gt;^2) - (sum{i in w}(sp&lt;I&gt;))^2) * sqrt(wks*sum{i in w}(dep&lt;I&gt;^2) &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; - (sum{i in w}(dep&lt;I&gt;))^2));&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; min mincorrelation = correlation;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #ff0000;"&gt;con weighting {i in w, j in 1..n}: wt[i,j] = if i = 1 then wt[i,j] else wt[i-1,j];&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="color: #ff0000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; con swt [1] = 1; &lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; solve WITH NLP / tech=ActiveSet;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print correlation percentn10.2 wt prdt swt sp dep;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; create data work.sumproduct from &lt;I&gt; = (w) sp;&lt;BR /&gt;quit;&lt;/I&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 11 Jun 2012 16:08:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79264#M555</guid>
      <dc:creator>hadesmr</dc:creator>
      <dc:date>2012-06-11T16:08:47Z</dc:date>
    </item>
    <item>
      <title>Re: macro to read data and declare VAR and IMPVAR</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79265#M556</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, remove w from wt declaration:&lt;/P&gt;&lt;P&gt;var wt {1..n} &amp;gt;= 0 &amp;lt;= 1 init 1;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Replace wt[i,j] with wt&lt;J&gt; everywhere.&lt;/J&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also, omit weighting constraint.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Finally:&lt;/P&gt;&lt;P&gt;con swt_con: swt = 1;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Or you can omit impvar swt and do this:&lt;/P&gt;&lt;P&gt;con swt_con: sum {j in 1..n} wt&lt;J&gt; = 1;&lt;/J&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Jun 2012 03:29:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79265#M556</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2012-06-12T03:29:21Z</dc:date>
    </item>
    <item>
      <title>Re: macro to read data and declare VAR and IMPVAR</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79266#M557</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi RobPratt,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Running a problem when maximizing the correlation. As you can see below, i intended to make m3 all ZERO across all 5 observations (it also happens in the real dataset). When i run solver in Excel, it maximizes the correlation between the "dep" and "sp" by giving weighting values to wt1 and wt2 and &lt;STRONG&gt;ZERO&lt;/STRONG&gt; to wt3 (because m3 is all zero) with the constrain that wt1+wt2+wt3 = 1. The below code (with your help) replicates the same process &lt;SPAN style="text-decoration: underline;"&gt;&lt;SPAN style="color: #575757; text-decoration: underline;"&gt;but it gives wt3 value (instead of zero).&lt;/SPAN&gt;&lt;/SPAN&gt; The results show wt1=.656, wt2=0 and wt3=.344. Basically, what i want to do is if all the obs in any given m&lt;J&gt; = 0 the wt&lt;J&gt; must be 0.&lt;/J&gt;&lt;/J&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can you please take a look and help me solve this problem? Thanks!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;--------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data M;&lt;BR /&gt;input week dep m1 m2 m3;&lt;BR /&gt;cards;&lt;BR /&gt;1 100 12 89 &lt;SPAN style="color: #ff0000;"&gt;0&lt;/SPAN&gt;&lt;BR /&gt;2 200 61 34 &lt;SPAN style="color: #ff0000;"&gt;0&lt;/SPAN&gt;&lt;BR /&gt;3 300 71 69 &lt;SPAN style="color: #ff0000;"&gt;0&lt;/SPAN&gt;&lt;BR /&gt;4 400 18 10 &lt;SPAN style="color: #ff0000;"&gt;0&lt;/SPAN&gt;&lt;BR /&gt;5 500 18 14 &lt;SPAN style="color: #ff0000;"&gt;0&lt;/SPAN&gt;&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%let n = 3;&lt;/P&gt;&lt;P&gt;proc optmodel;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num n = &amp;amp;n;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set w;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num weeks{w};&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num dep{w};&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num m{w,1..n};&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; read data M into w = [_N_] dep {j in 1..n} &amp;lt;m[_N_,j]=col('m'||j)&amp;gt;;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var wt {1..n} &amp;gt;= 0 &amp;lt;= 1 init 1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; impvar prdt {i in w, j in 1..n} = m[i,j] * wt&lt;J&gt;;&lt;BR /&gt; impvar sp {i in w} = sum {j in 1..n} prdt[i,j];&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; num wks;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; wks = card (w);&lt;/J&gt;&lt;/P&gt;&lt;P&gt; impvar correlation = (wks*sum{i in w}(sp&lt;I&gt;*dep&lt;I&gt;) - (sum{i in w}(sp&lt;I&gt;)) * (sum{i in w}(dep&lt;I&gt;)))&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; / (sqrt(wks*sum{i in w}(sp&lt;I&gt;^2) - (sum{i in w}(sp&lt;I&gt;))^2) * sqrt(wks*sum{i in w}(dep&lt;I&gt;^2) &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; - (sum{i in w}(dep&lt;I&gt;))^2));&lt;BR /&gt; max maxcorrelation = correlation;&lt;BR /&gt; con swt_con: sum {j in 1..n} wt&lt;J&gt; = 1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; solve WITH NLP / tech=ActiveSet;&lt;/J&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print correlation percentn10.2 wt prdt sp dep;&lt;BR /&gt; create data work.sumproduct from [week] = (w) sp;&lt;BR /&gt; create data work.weighting from [variable] = {1..n} wt;&lt;BR /&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Jun 2012 16:24:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79266#M557</guid>
      <dc:creator>hadesmr</dc:creator>
      <dc:date>2012-06-29T16:24:34Z</dc:date>
    </item>
    <item>
      <title>Re: macro to read data and declare VAR and IMPVAR</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79267#M558</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Add this line before the SOLVE statement:&lt;/P&gt;&lt;P&gt;for {j in 1..n: and {i in w} (m[i,j] = 0)} fix wt&lt;J&gt; = 0;&lt;/J&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Jun 2012 17:46:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79267#M558</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2012-06-29T17:46:07Z</dc:date>
    </item>
    <item>
      <title>Re: macro to read data and declare VAR and IMPVAR</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79268#M559</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;great, thank you, as always!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Jun 2012 18:57:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/macro-to-read-data-and-declare-VAR-and-IMPVAR/m-p/79268#M559</guid>
      <dc:creator>hadesmr</dc:creator>
      <dc:date>2012-06-29T18:57:46Z</dc:date>
    </item>
  </channel>
</rss>

