Date: Tue, 8 May 2001 00:31:27 -0000
Reply-To: sashole@bellsouth.net
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Paul Dorfman <paul_dorfman@HOTMAIL.COM>
Subject: Re: elements in arrays
Content-Type: text/plain; format=flowed
Dennis,
Actually, N's C code returns 1 if there is at least 1 pair of unequal items
in the array. This of course can be ascertained by comparing each element to
every other element (when an item is compared to itself, it incurs extra
computer labor but does not make the result invalid):
do i=1 to 10 until (return);
do j=1 to 10;
if a(i) ne a(j) then do; return = 1; leave; end;
end;
end;
However, why bother comparing all items with each other? If all of them were
equal, then comparing *any* arbitrarily chosen item ("pivot") with the rest
of the crowd would result in no inequalities. Therefore, it suffices to
compare any item (first, say) to all other elements and if an inequality is
detected, return 1 and stop. Most efficiently, it can be done by placing a
sentinel unequal to the pivot in the 11th occurrence (this way, the inner
loop contains a single comparison):
a(11) = a(1);
do i=2 by 1 until (a(i) ne a(1)); end;
if i < 11 then return = 1;
Kind regards,
=======================
Paul M. Dorfman
Jacksonville, Fl
=======================
>From: "Diskin, Dennis" <Dennis.Diskin@PHARMA.COM>
>Array data (0:10); /* assuming dat0-data10 are filled in somewhere */
>
>do i = 0 to 10;
> do j = 0 to 10;
> if data(i) eq data(j) then.... will always find a match when i = j;
> end;
>end;
>
>/* maybe you want : ?? */
>do i = 0 to 9;
> do j = i+1 to 10;
> if data(i) eq data(j) then....
> end;
>end;
>
>Dennis Diskin
>
> > -----Original Message-----
> > From: N Yiannakoulias [SMTP:nwy@GPU.SRV.UALBERTA.CA]
> > Sent: Monday, May 07, 2001 2:51 PM
> > To: SAS-L@listserv.uga.edu
> > Subject: elements in arrays
> >
> > Hi all,
> >
> > I'm used to arrays in languages other than SAS where the _elements_ of
> > a vector are accessed using an index. So:
> >
> > for(i=0;i<=10;++i)
> > {
> > if (data[i]>=100)
> > {
> > return 1;
> > }
> >
> > }
> >
> > Returns a "1" if any element in the array data[i] is over 100.
> > Of course this is very easy to do in SAS without worrying
> > about SAS 'arrays' at all. However, I can't figure out a
> > way to compare the elements of the array themselves. For example:
> >
> > for(i=0;i<=10;++i)
> > {
> > for(j=0;j<=10;++j)
> > {
> > if(data[i]!=data[j])
> > {
> > return 1;
> > }
> >
> > }
> > }
> >
> > I want to look within the vector itself and make comparisons.
> > This is the one big thing that stumps me in SAS so
> > I always come up with really convoluted work-arounds. Is
> > there an easy way to simply compare the elements?
> >
> > N
> >
> >
> > --
> > -----------------------------------------------
> > Conveniently located at lat 53 31'.01, lon 113 30'.26
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com
|