<?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 Do until in proc iml in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/320943#M3170</link>
    <description>&lt;P&gt;Hi I am new to this board and trying to translate a matlab code to sas and am having some problems with the do until loop in proc iml:&lt;/P&gt;&lt;P&gt;The matlab code is:&lt;/P&gt;&lt;P&gt;while any(a&amp;lt;0)&amp;nbsp;&lt;BR /&gt;a(a&amp;lt;0) = 0;&lt;BR /&gt;ind = find(a&amp;gt;0);&lt;BR /&gt;b = a(ind);&lt;BR /&gt;k = length(b);&lt;BR /&gt;b = b - 1/k *(sum(b)-1);&lt;BR /&gt;a(ind) = b;&lt;BR /&gt;end&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and the sas code that I wrote is:&lt;/P&gt;&lt;P&gt;proc iml;&lt;BR /&gt;use XXX&amp;nbsp;&lt;BR /&gt;read all&amp;nbsp;XXX&lt;BR /&gt;close&amp;nbsp;XXX&lt;BR /&gt;...&lt;BR /&gt;do until (ncol(loc(a&amp;lt;0))=0);&lt;BR /&gt;a[1,loc(a&amp;lt;0)]=0;&lt;BR /&gt;ind=loc(a&amp;gt;0); b=a[ind]; k=ncol(b); b=b-((sum(b)-1)/k);&lt;BR /&gt;a[ind]=b;&lt;BR /&gt;end;&lt;BR /&gt;run;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I am trying to run teh do loop till none of the element in matrix a is negative (using&amp;nbsp;&lt;SPAN&gt;do until (ncol(loc(a&amp;lt;0))=0);) not sure if thats whats causing the problem...&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 23 Dec 2016 15:08:49 GMT</pubDate>
    <dc:creator>Newph</dc:creator>
    <dc:date>2016-12-23T15:08:49Z</dc:date>
    <item>
      <title>Do until in proc iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/320943#M3170</link>
      <description>&lt;P&gt;Hi I am new to this board and trying to translate a matlab code to sas and am having some problems with the do until loop in proc iml:&lt;/P&gt;&lt;P&gt;The matlab code is:&lt;/P&gt;&lt;P&gt;while any(a&amp;lt;0)&amp;nbsp;&lt;BR /&gt;a(a&amp;lt;0) = 0;&lt;BR /&gt;ind = find(a&amp;gt;0);&lt;BR /&gt;b = a(ind);&lt;BR /&gt;k = length(b);&lt;BR /&gt;b = b - 1/k *(sum(b)-1);&lt;BR /&gt;a(ind) = b;&lt;BR /&gt;end&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and the sas code that I wrote is:&lt;/P&gt;&lt;P&gt;proc iml;&lt;BR /&gt;use XXX&amp;nbsp;&lt;BR /&gt;read all&amp;nbsp;XXX&lt;BR /&gt;close&amp;nbsp;XXX&lt;BR /&gt;...&lt;BR /&gt;do until (ncol(loc(a&amp;lt;0))=0);&lt;BR /&gt;a[1,loc(a&amp;lt;0)]=0;&lt;BR /&gt;ind=loc(a&amp;gt;0); b=a[ind]; k=ncol(b); b=b-((sum(b)-1)/k);&lt;BR /&gt;a[ind]=b;&lt;BR /&gt;end;&lt;BR /&gt;run;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I am trying to run teh do loop till none of the element in matrix a is negative (using&amp;nbsp;&lt;SPAN&gt;do until (ncol(loc(a&amp;lt;0))=0);) not sure if thats whats causing the problem...&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Dec 2016 15:08:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/320943#M3170</guid>
      <dc:creator>Newph</dc:creator>
      <dc:date>2016-12-23T15:08:49Z</dc:date>
    </item>
    <item>
      <title>Re: Do until in proc iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/320960#M3171</link>
      <description>&lt;P&gt;Here is another translation of your original Matlab code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;do while (any(a&amp;lt;0));
    a[loc(a&amp;lt;0)] = 0;
    ind = loc(a&amp;gt;0);
    b = a[ind];
    k = nrow(b)#ncol(b);
    b = b - 1/k #(sum(b)-1);
    a[ind] = b;
end;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I ran it with randomly generated vectors and matrices, and it *seemed* to give reasonable output.&amp;nbsp; Without a better understanding of what you are trying to do, I can't say whether it is correct or not.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Dec 2016 16:07:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/320960#M3171</guid>
      <dc:creator>dougc</dc:creator>
      <dc:date>2016-12-23T16:07:04Z</dc:date>
    </item>
    <item>
      <title>Re: Do until in proc iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/320966#M3172</link>
      <description>&lt;P&gt;Thanks so much for the help! The code ran but the results seems a little strange... essentially what I am trying to do is to convert my matrix (in this example below matrix5, its a 22*22 matrix) to its closest stochastic matrix with the below logic: 1) go through the matrix row by row starting with row 1 2) if any of the element in row 1 is negative, then replace the negative number with 0 and if the rest in the row are positive, then normalize all other elements with ind=loc(a&amp;gt;0); b=a[ind]; k=nrow(b)#ncol(b); b=b-1/k#(sum(b)-1); a[ind]=b; keep on looping till all elements are &amp;gt;=0 3) once row 1 satisfies the above condition, go to row 2, and keep on going till row 22 4) output to a below is my updated code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml; 
use OLD; 
read all var _ALL_ into matrix5[colname=varNames]; 
close OLD; 
a=matrix5; 
do ii=1 to nrow(matrix5); 
   call symputx("iii",ii); 
   a=matrix5[num(symget("iii")), ]; 
   do while (any(a&amp;lt;0)); 
      a[loc(a&amp;lt;0)]=0; 
      ind=loc(a&amp;gt;0); 
      b=a[ind]; k=nrow(b)#ncol(b); 
      b=b-1/k#(sum(b)-1); 
      a[ind]=b; 
   end; 
   rA[num(symget("iii")), ] = a; 
   print rA[L="new4" format=7.4]; 
end; 
quit; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;any help would be greatly appreciated!&lt;/P&gt;</description>
      <pubDate>Sat, 24 Dec 2016 10:41:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/320966#M3172</guid>
      <dc:creator>Newph</dc:creator>
      <dc:date>2016-12-24T10:41:00Z</dc:date>
    </item>
    <item>
      <title>Re: Do until in proc iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/320969#M3173</link>
      <description>&lt;P&gt;Post code into the box opened using the {i} menu item in the form to preserve formatting.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Dec 2016 17:11:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/320969#M3173</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-12-23T17:11:32Z</dc:date>
    </item>
    <item>
      <title>Re: Do until in proc iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/320973#M3174</link>
      <description>&lt;P&gt;If I understand what you are trying to do correctly, I believe the following re-working of your code is what you want (with some comments):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;do ii = 1 to nrow(a);
    do while (any(a[ii,] &amp;lt; 0));     /* search for negative value in row ii */
        a[ii,loc(a[ii,] &amp;lt; 0)] = 0;  /* set negative values in row ii to 0 */
        ind=loc(a[ii,]&amp;gt;0);          /* find remaining non-negative elements in row ii */
        b=a[ii,ind];                /* extract non-negative elements into column vector */
        k=nrow(b)#ncol(b);          /* length of vector, could just use nrow(b) */
        b=b-(sum(b)-1)/k;           /* normalize */
        a[ii,ind]=b;                /* update positive elements */
    end;
end;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I misunderstand your algorithm, perhaps you could provide an example and what you expect as output.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Dec 2016 17:38:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/320973#M3174</guid>
      <dc:creator>dougc</dc:creator>
      <dc:date>2016-12-23T17:38:44Z</dc:date>
    </item>
    <item>
      <title>Re: Do until in proc iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/321033#M3176</link>
      <description>&lt;PRE&gt;

So you want process it row by row .



proc iml;
a={-1 -2 1 2,
   -3 3 4 6,
   -3 3 9 -2,
   9 3 4 -2};
   
do i=1 to nrow(a);
 temp=a[i,];
 do while(any(temp&amp;lt;0));
  temp[loc(temp&amp;lt;0)]=0;
  ind=loc(temp&amp;gt;0);
  b=temp[ind];
  k=nrow(b);
  b=b-(1/k)#(sum(b)-1);
  temp[ind]=b;
 end;
 a[i,]=temp;
end;
print a;
quit;

&lt;/PRE&gt;</description>
      <pubDate>Sat, 24 Dec 2016 06:39:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/321033#M3176</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-12-24T06:39:55Z</dc:date>
    </item>
    <item>
      <title>Re: Do until in proc iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/321035#M3177</link>
      <description>&lt;PRE&gt;
Maybe you want this.


http://blogs.sas.com/content/iml/2012/11/28/computing-the-nearest-correlation-matrix.html

&lt;/PRE&gt;</description>
      <pubDate>Sat, 24 Dec 2016 07:00:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/321035#M3177</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-12-24T07:00:01Z</dc:date>
    </item>
    <item>
      <title>Re: Do until in proc iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/321039#M3178</link>
      <description>&lt;P&gt;You say that your goal is&amp;nbsp;"to&amp;nbsp;&lt;SPAN&gt;convert my matrix to its closest stochastic matrix."&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Why do you think that this process will converge to a stochastic matrix? Do you have a reference for this algorithm? Or is this your attempt to create such an algorithm.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Notice&amp;nbsp;that if the elements of the input matrix are all positive, but the row sums are not unity, then the&amp;nbsp;algorithm you are implementing does nothing to &amp;nbsp;change the matrix. For example,&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;a = {
   0.1 0.2 0.1 0.2 ,
   0.3 0.3 0.4 0.6 ,
   0.3 0.3 0.9 0.2 ,
   0.9 0.3 0.4 0.2 };
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Dec 2016 10:50:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/321039#M3178</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2016-12-24T10:50:42Z</dc:date>
    </item>
    <item>
      <title>Re: Do until in proc iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/322272#M3202</link>
      <description>&lt;P&gt;Thanks so much! &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jan 2017 22:41:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Do-until-in-proc-iml/m-p/322272#M3202</guid>
      <dc:creator>Newph</dc:creator>
      <dc:date>2017-01-03T22:41:44Z</dc:date>
    </item>
  </channel>
</rss>

