## Using Proc IML to analyze data rowwise

Occasional Contributor
Posts: 9

# Using Proc IML to analyze data rowwise

I have a macro which orders means (ascending/descending) using PROC IML. But I want to do this for each simulation number. Is it possible to do this, given that in the usual data step I can use the 'BY statement'?

Here is it this SAS macro and a small dataset is attached (with variables sim, meanX1--meanX5). This macro is support to arrange the means in a descending order per 'sim'.

%macro mean_will(numbersim=, J1=, J2=, J3=, J4=, J5=, df=);

proc iml;

start MLE(x,n);

k=ncol(x);

m=j(1,k,.);

a=j(1,k,.);

c=j(1,k,.);

do i=1 to k;

do u=1 to i;

do v=i to k;

a=sum(x[u:v]#n[u:v])/sum(n[u:v]);

end;

c=max(a);

a=j(1,k,.);

end;

m=min(c);

c=j(1,k,.);

end;

return(m);

finish;

use meanXit;

x=Xmat[,2]||Xmat[,3]||Xmat[,4]||Xmat[,5]||Xmat[,6];

n={&J1 &J2 &J3 &J4 &J5};

m=MLE(x,n);

create meanX from m;

append from m;

run;

quit;

%mend;

%mean_will(numbersim=2, J1=7, J2=2, J3=2, J4=2, J5=2, df=20);

SAS Super FREQ
Posts: 4,178

## Re: Using Proc IML to analyze data rowwise

You say that you want to "order means for each simulation number," but I don't understand the connection to the %mean_will macro, which is not sorting anything.

The example data are

sim meanX1 meanX2 meanX3 meanX4 meanX5

1 1.85570 1.82321 1.85598 1.80608 1.81799

2 1.84527 1.82912 1.85502 1.85474 1.82042

What do you want the results to be, and why? Do you want each row to appear in sorted order?  Do you want to run the %mean_will macro and then sort the resulting vector?

BTW, in the macro I notice that you are computing with x[u:v] when x is a matrix. Are you trying to work with the columns x[ ,u:v]?

Occasional Contributor
Posts: 9

## Re: Using Proc IML to analyze data rowwise

Yes the proc iml inside %mean_will macro looks at the mean values meanX1-meanX5 to see if they are all in a non-increasing order, if that's not the case it will take the average of those means that violate this restriction.

I want to be able to do this for each sim=1, 2, etc. The reason is that I'm doing some analysis where the alternative hypothesis is ordered this way. For this macro I want to be able to get the means computed/arranged as indicated in the proc iml but this done for each 'sim'. The whole macro works perfect when only 1 row is considered, problems start when I include several rows.

Yes, I'm working with the columns.

All in all it's more like this: For each sim (row) : The value in column 1 >= the value in column 2 >= etc.

I hope this helps.

SAS Super FREQ
Posts: 4,178

## Re: Using Proc IML to analyze data rowwise

I can't make sense of what your algorithm is doing, but here are some tips:

In the innermost loop, you have a computation that is equivalent to

w = x[u:v]#n[u:v];

a=w[+] / sum(n[u:v]);

This expression is incorrect when x is a matrix.  Instead, you might consider a vector expression like this:

w = x[ ,u:v]#n[ ,u:v];

a[ ,v]=w[ ,+] / sum(n[u:v]);

where now a is now a matrix: a = j(nrow(x), ncol(x), .);

Then you have to do something with the MIN and MAX functions, so that you get the min and max for each row(?).  Perhaps you can use  something like this:

maxOfRows = a[ , <>];

minOfRows = a[ , ><];

In short, express everything as vectors and use subscript reduction operators. Subscript reduction operators are described here: Compute statistics for each row by using subscript operators - The DO Loop

Good luck.

Occasional Contributor
Posts: 9

## Re: Using Proc IML to analyze data rowwise

Thanks a lot, will look into your considerations carefully!

Discussion stats
• 4 replies
• 447 views
• 3 likes
• 2 in conversation