Date: Thu, 9 Dec 1999 20:13:07 -0600
Reply-To: shiling@math.wayne.edu
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Shiling Zhang <shiling@MATH.WAYNE.EDU>
Organization: Wayne State University
Subject: Re: weighted median -IML
Content-Type: text/plain; charset=us-ascii
Lynn,
The idea is, first, calculate the product(elementwise) say
w_value=weight#value, second, use 'build in rank function' to get the ranks
for vector w_value, third, check number of elements, if it is an even number,
median= the average of two values pointed by two middle ranks. if it is an odd
number, median=the value pointed by the middle rank.
Note the rank function in IML does not follow the conventional rules when
there are ties.
Hope it helps.
data t1;
do i=1 to 5;
weight=10*(mod(i,4)+1);
value=int(100*ranuni(123));
output;
end;
drop i;
run;
proc iml;
use t1;
read all var {weight} into weight(|colname=cols|);
read all var {value} into value(|colname=cols|);
w_value=weight#value;
rank=rank(w_value);
pos=int(nrow(value)*0.5);
if mod(pos,2) = 0 then do;
median=0.5*(w_value(|loc(rank=pos)|)+w_value(|loc(rank=(pos+1))|));
end;
else median=w_value(|loc(rank=(pos+1))|);
print median w_value weight value;
quit;
run;
Lynn Nicole Lethbridge wrote:
> Hi there
>
> Does anyone know if there's a quick way to get a weighted median using IML
> in SAS? My weights are integral and range from 1 to 3000 or so.
>
> Thanks very much for your time.
>
> Lynn
|