turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-23-2016 10:08 AM

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:

The matlab code is:

while any(a<0)

a(a<0) = 0;

ind = find(a>0);

b = a(ind);

k = length(b);

b = b - 1/k *(sum(b)-1);

a(ind) = b;

end

and the sas code that I wrote is:

proc iml;

use XXX

read all XXX

close XXX

...

do until (ncol(loc(a<0))=0);

a[1,loc(a<0)]=0;

ind=loc(a>0); b=a[ind]; k=ncol(b); b=b-((sum(b)-1)/k);

a[ind]=b;

end;

run;quit;

So I am trying to run teh do loop till none of the element in matrix a is negative (using do until (ncol(loc(a<0))=0) not sure if thats whats causing the problem...

Accepted Solutions

Solution

01-03-2017
05:41 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-24-2016 01:39 AM

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-23-2016 11:07 AM

Here is another translation of your original Matlab code:

do while (any(a<0)); a[loc(a<0)] = 0; ind = loc(a>0); b = a[ind]; k = nrow(b)#ncol(b); b = b - 1/k #(sum(b)-1); a[ind] = b; end;

I ran it with randomly generated vectors and matrices, and it *seemed* to give reasonable output. Without a better understanding of what you are trying to do, I can't say whether it is correct or not.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-23-2016 12:00 PM - last edited on 12-24-2016 05:41 AM by Rick_SAS

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>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 >=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:

```
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<0));
a[loc(a<0)]=0;
ind=loc(a>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;
```

any help would be greatly appreciated!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-23-2016 12:11 PM

Post code into the box opened using the {i} menu item in the form to preserve formatting.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-23-2016 12:38 PM

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):

do ii = 1 to nrow(a); do while (any(a[ii,] < 0)); /* search for negative value in row ii */ a[ii,loc(a[ii,] < 0)] = 0; /* set negative values in row ii to 0 */ ind=loc(a[ii,]>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;

If I misunderstand your algorithm, perhaps you could provide an example and what you expect as output.

Solution

01-03-2017
05:41 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-24-2016 01:39 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-03-2017 05:41 PM

Thanks so much!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-24-2016 02:00 AM

Maybe you want this. http://blogs.sas.com/content/iml/2012/11/28/computing-the-nearest-correlation-matrix.html

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-24-2016 05:50 AM

You say that your goal is "to convert my matrix to its closest stochastic matrix."

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.

Notice that if the elements of the input matrix are all positive, but the row sums are not unity, then the algorithm you are implementing does nothing to change the matrix. For example,

```
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 };
```