Turn on suggestions

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

Showing results for

Options

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

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 12-23-2016 10:08 AM
(1989 views)

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...

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

8 REPLIES 8

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thanks so much! 😉

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

**Available on demand!**

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.