<?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: How do I apply element-wise condition within optimisation procedure in proc IML in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-apply-element-wise-condition-within-optimisation/m-p/451111#M4114</link>
    <description>Thanks Rick. I used the ‘choose’ function as you siggested and it worked. Thanks so much.</description>
    <pubDate>Wed, 04 Apr 2018 14:24:02 GMT</pubDate>
    <dc:creator>ZMX</dc:creator>
    <dc:date>2018-04-04T14:24:02Z</dc:date>
    <item>
      <title>How do I apply element-wise condition within optimisation procedure in proc IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-apply-element-wise-condition-within-optimisation/m-p/449782#M4104</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm working within Proc IML and&amp;nbsp;have already set up a maximum likelihood function and using nlpnrr method to optimise. The optimisation itself works well. The problem is when I added certain If-Then_else statements within this optimization procedure. I need to put condition on certain elements/observations&amp;nbsp;in the matrices. For example,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if income[,m] &amp;lt; 0 then income[,m]=0; else income[,m]=income[,m];&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The rows of the matrix are individuals and m is the&amp;nbsp;number of&amp;nbsp; ways that I calculate income in the previous lines through a do loop..&lt;/P&gt;&lt;P&gt;I noticed that when&amp;nbsp;I put this condition, the way SAS reads it is - if income is negative for&amp;nbsp;ALL the elements in each column then it'll give zeros to ALL the individuals in that column, otherwise it keeps income as it is for everyone. However, what I need to see is to only give 0 to those observations that have a negative income. When I add "i"&amp;nbsp;in the above code so that;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;do i=1 to num;&amp;nbsp; /*num is number of observations*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if income[i,m] &amp;lt; 0 then income[i,m]=0; else income[i,m]=income[i,m];&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;this works but the problem is that this code doesn't work in&amp;nbsp;optimisation procedure and I&amp;nbsp;get error.&lt;/P&gt;&lt;P&gt;So my question is that how can I do this within the nlpnrr optimisation procedure? I appreciate any help..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Mar 2018 21:52:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-apply-element-wise-condition-within-optimisation/m-p/449782#M4104</guid>
      <dc:creator>ZMX</dc:creator>
      <dc:date>2018-03-29T21:52:04Z</dc:date>
    </item>
    <item>
      <title>Re: How do I apply element-wise condition within optimisation procedure in proc IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-apply-element-wise-condition-within-optimisation/m-p/449895#M4105</link>
      <description>&lt;P&gt;First read the article &lt;A href="https://blogs.sas.com/content/iml/2015/01/26/if-then-matrix.html" target="_self"&gt;"IF-THEN logic with matrix expressions"&lt;/A&gt;, which explains that the condition&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;if income[,m] &amp;lt; 0 then...&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;is equivalent to&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;if ALL( income[,m] &amp;lt; 0 ) then...&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;.&lt;/P&gt;
&lt;P&gt;It sounds like you want to use a different logical condition:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;if ANY( income[,m] &amp;lt; 0 ) then...&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If that is correct, then you can use the LOC function or the CHOOSE function.&amp;nbsp;In either&amp;nbsp;case, you get rid of the IF-THEN statement.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;To use the LOC function, see the article &lt;A href="https://blogs.sas.com/content/iml/2015/01/20/elements-satisfy-logical-expression.html" target="_self"&gt;"Finding matrix elements that satisfy a logical expression"&lt;/A&gt;. (But &lt;A href="https://blogs.sas.com/content/iml/2012/11/19/beware-the-naked-loc.html" target="_self"&gt;be sure to make sure&amp;nbsp;at least one observation meets the condition.&lt;/A&gt;)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Alternatively, you can&lt;A href="https://blogs.sas.com/content/iml/2011/08/15/complex-assignment-statements-choose-wisely.html" target="_self"&gt; use the CHOOSE function&lt;/A&gt;, which has a slightly simpler&amp;nbsp;syntax, as follows.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;income[,m] = choose(&amp;nbsp;income[,m] &amp;lt; 0,&amp;nbsp; 0,&amp;nbsp;&amp;nbsp;income[,m] );&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Mar 2018 12:15:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-apply-element-wise-condition-within-optimisation/m-p/449895#M4105</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2018-03-30T12:15:10Z</dc:date>
    </item>
    <item>
      <title>Re: How do I apply element-wise condition within optimisation procedure in proc IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-apply-element-wise-condition-within-optimisation/m-p/451111#M4114</link>
      <description>Thanks Rick. I used the ‘choose’ function as you siggested and it worked. Thanks so much.</description>
      <pubDate>Wed, 04 Apr 2018 14:24:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-apply-element-wise-condition-within-optimisation/m-p/451111#M4114</guid>
      <dc:creator>ZMX</dc:creator>
      <dc:date>2018-04-04T14:24:02Z</dc:date>
    </item>
  </channel>
</rss>

