Date: Fri, 2 Jan 2009 15:58:19 -0500
Reply-To: Muthia Kachirayan <muthia.kachirayan@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Muthia Kachirayan <muthia.kachirayan@GMAIL.COM>
Subject: Re: To find out the max value...
In-Reply-To: <30648bb30901010957v23f73dffm9e1b1ea4eff749c2@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
On Thu, Jan 1, 2009 at 12:57 PM, Ian Whitlock <iw1sas@gmail.com> wrote:
> Data _null_ ;
> input Name $ a b c d e f ;
> max1 = ordinal(6,of a--f);
> max2 = ordinal(5,of a--f);
> put name max1 max2 ;
> cards;
> Mahi 12 45 65 87 36 98
> Suri 32 20 54 86 47 100
> Nari 05 62 28 65 46 32
> ;;;;
>
> LARGEST function is not listed in version 8 documentation, but ORDINAL
> has been available for at least 25 years. LARGEST was probably
> introduced for people who cannot count backwards. On the other hand,
> it eliminates the need to know how many variables are involved.
>
> Ian Whitlock
> ===============
Ian and all,
For finding the First Maximum and the Second Maximum I did not choose
ORDINAL() function as I thought that besides background sorting, there may
be other computations to slow the work. After your post I wanted to check my
assumption. Data_NULL brought LARGEST() function then. I had a look at the
speed of the four ways.
1. Using SORTN() function as I did.
2. Using other solution where I used Max() function only.
3. The use of ORDINAL() function as Ian used.
4. The use of LARGEST() function as Data_NULL_ used.
The test data :
data test;
array m aa ab ac ad ae af ag ah ai aj
ba bb bc bd be bf bg bh bi bj
ca cb cc cd ce cf cg ch ci cj
da db dc dd de df dg dh di dj;
do _n_ = 1 to 1000000;
do over m;
m = ceil(ranuni(123) * 999);
end;
output;
end;
run;
** 1. Using Sortn() ;
data need;
set test ;
array k[*] _numeric_;
call sortn(of k[*]);
max1 = k[dim(k)];
max2 = k[dim(k) - 1];
drop a: b: c: d:;
run;
**2. Max() is used ;
In my solution posted earlier, the second Maximum was not correctly caught
in all circumstances and the corrected code is given below.
data need;
set test ;
array k[*] _numeric_;
do _n_ = 1 to dim(k);
if k[_n_] > max1 then max2 = max1;
else if k[_n_] > max2 then max2 = k[_n_];
max1 = max(max1,k[_n_]);
end;
drop aa--dj;
run;
** 3. LARGEST() is used ;
data need ;
set test ;
max1 = ordinal(40,of aa--dj);
max2 = ordinal(39,of aa--dj);
drop a: b: c: d:;
run;
** 4. ORDINAL() is used ;
data need;
set test ;
max1 = largest(1,of aa--dj);
max2 = largest(2,of aa--dj);
drop a: b: c: d:;
run;
The wall-clock times based on 10 replications are compared and to control
for the CPU-speeds, the relative speeds in time-units are as:
1. 2.07
2. 1.88
3. 1.34
4. 1.00
The SORTN() function (in 9.1 it is experimental ) does not go fast as I
presumed. I do not know whether this has been corrected in 9.2. The
LARGEST() looks best in 9.1. Hence, ORDINAL() seems better alternative in
V8.
Happy New Year to you and to all SAS-Lers.
Best regards,
Muthia Kachirayan