Date: Thu, 21 Jan 2010 21:14:47 -0600
Reply-To: Yu Zhang <zhangyu05@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Yu Zhang <zhangyu05@GMAIL.COM>
Subject: Re: Find the position of max/min value in an unsorted array
In-Reply-To: <201001220205.o0LKwXB6027680@malibu.cc.uga.edu>
Content-Type: text/plain; charset=ISO-8859-1
WhichN function will give you the position of that matching maximum value.
if you have more than 1 maximum value, the position return is the first one.
data _null_;
array _A{*} X1-X10;
do j=1 to 10;
_A[j]=ranuni(999);
put _A[j]=;
end;
maxv=max(of _A[*]);
Index=whichN(maxv,of _A[*]);
put (maxv index)(=/);
run;
HTH
Yu
On Thu, Jan 21, 2010 at 8:05 PM, oloolo <dynamicpanel@yahoo.com> wrote:
> Just come across this in my mind. MAX/MIN function in SAS is able to find
> the Max or Min value of a given array. If we want to position this max /min
> value in the array, we probablly have to do something like this:
>
> data _null_;
> array _A{*} X1-X10;
> do j=1 to 10; _A[j]=ranuni(999); end;
> maxv=max(of _A[*]);
> notfound=1;
> do j=1 to dim(_A) & notfound;
> notfound=(maxv=_A[j]);
> end;
> put j=;
> run;
>
> this is inefficient when encounter a very large unsorted matrix _A[*] and
> the worst case need dim(_A) operations.
>
> any more elegent solutions in SAS?
>
|