1
votes

I would like to know if it's possible to select the 5 minimum or maximum values by rows with IML ?

This is my code :

Proc iml ;
  use table;
  read all var {&varlist} into matrix ; 

n=nrow(matrix) ; /* n=369 here*/
p=ncol(matrix);  /* p=38 here*/

test=J(n,5,.) ; 

Do i=1 to n ;
    test[i,1]=MIN(taux[i,]);
End;
Quit ;

So I would like to obtain a matrix test that contains for the 1rst column the maximal minimum value, then for the 2nd column the minimum value of my row EXCEPTING the 1rst value, etc...

If you have any idea ! :) Event if it's not with IML (but with SAS : base, sql..)

So for example :

    Data test;                                                                                                                input x1-x10 ;                                                                                                              cards;     
1 9 8 7 3 4 2 6
9 3 2 1 4 7 12 -2
;run;

And I would like to obtain the results sorted by row:

1 2 3 4 6 7 8 9
-2  1 2 3 4 7 12

in order to select my 5 minimum values in another table :

y1 y2 y3 y4 y5
1  2 3 4  6
-2 1 2 3 4
2
I think you were downvoted because you had some pretty generic tags against your question. I've removed those tags for you to keep this question just within those who follow the 'sas' tags...Robert Penridge
I'm not fully sure what you are looking for. An example with data and expected output would be helpful.DomPazz
I edited the question with an example , sorry if it isn't clearchocolat

2 Answers

1
votes

You can use call sort() in PROC IML to sort a column. Because you want to separate the columns and not sort the whole matrix, extract the column, sort it, and then update the original.

You want to sort rows, so transpose your matrix, do the sorting, and then transpose back.

proc iml;

have = {1 9 8 7 3 4 2 6,
9 3 2 1 4 7 12 -2};

print have;

n = nrow(have);

have = have`; /*Transpose because sort works on columns*/

do i=1 to n;
    tmp = have[,i];
    call sort(tmp,1);
    have[,i]=tmp;
end;

have = have`;

want = have[,1:5];
print want;
quit;
1
votes

Read the article "Compute the kth smallest data value in SAS" Define the modules as in the article. Then use the following:

have = {1 9 8 7 3 4 2 6,
        9 3 2 1 4 7 12 -2};
x = have`;    /* transpose */

ord = j(5,ncol(x));
do j = 1 to ncol(x);
   ord[,j] = ordinal(1:5, x[,j]);
end;
print ord;

If you have missing values in your data and want to exclude them, use the SMALLEST module instead of the ORDINAL module.