options linesize = 80 ;
footnote "~john-c/5421/mediansum.sas &sysdate &systime" ;
data xdata ;
infile 'x.data' end = eof ;
input x ;
if x eq . then delete ;
endvar = 0 ;
if eof = 1 then endvar = 1 ;
run ;
proc print data = xdata ;
proc sort data = xdata ; by x ;
data xdata ;
retain count 0 ;
set xdata end = endmark ;
count = count + 1 ;
endata = 0 ;
if endmark eq 1 then endata = 1 ;
if endmark eq 1 then do ;
call symput('xcount', left(put(count, 3.0))) ;
end ;
run ;
proc print data = xdata ;
data xdata ;
set xdata ;
retain median ;
ihalf = int(&xcount / 2) ;
half = &xcount / 2 ;
if half = ihalf then do ;
if count eq half then median = x ;
if count eq (half + 1) then median = .5 * median + .5 * x ;
end ;
if half ne ihalf then do ;
if count eq ihalf + 1 then median = x ;
end ;
run ;
proc print data = xdata ;
var count ihalf half x median ;
title1 "How to compute the median ... use of symput ... count = &xcount" ;
The SAS System 1
18:53 Monday, October 2, 2006
Obs x endvar
1 0.1678 0
2 0.0909 0
3 0.2145 0
4 0.7073 0
5 0.8860 0
6 0.4544 0
7 0.3173 0
8 0.0100 0
9 0.9999 0
10 0.2134 0
11 0.3124 0
12 0.4132 0
13 0.4123 0
14 0.4213 0
15 0.4231 0
16 0.4312 0
17 0.4321 0
18 0.5555 0
19 0.8978 0
20 0.9135 1
~john-c/5421/mediansum.sas 02OCT06 18:53
The SAS System 2
18:53 Monday, October 2, 2006
Obs count x endvar endata
1 1 0.0100 0 0
2 2 0.0909 0 0
3 3 0.1678 0 0
4 4 0.2134 0 0
5 5 0.2145 0 0
6 6 0.3124 0 0
7 7 0.3173 0 0
8 8 0.4123 0 0
9 9 0.4132 0 0
10 10 0.4213 0 0
11 11 0.4231 0 0
12 12 0.4312 0 0
13 13 0.4321 0 0
14 14 0.4544 0 0
15 15 0.5555 0 0
16 16 0.7073 0 0
17 17 0.8860 0 0
18 18 0.8978 0 0
19 19 0.9135 1 0
20 20 0.9999 0 1
~john-c/5421/mediansum.sas 02OCT06 18:53
How to compute the median ... use of symput ... count = 20 3
18:53 Monday, October 2, 2006
Obs count ihalf half x median
1 1 10 10 0.0100 .
2 2 10 10 0.0909 .
3 3 10 10 0.1678 .
4 4 10 10 0.2134 .
5 5 10 10 0.2145 .
6 6 10 10 0.3124 .
7 7 10 10 0.3173 .
8 8 10 10 0.4123 .
9 9 10 10 0.4132 .
10 10 10 10 0.4213 0.4213
11 11 10 10 0.4231 0.4222
12 12 10 10 0.4312 0.4222
13 13 10 10 0.4321 0.4222
14 14 10 10 0.4544 0.4222
15 15 10 10 0.5555 0.4222
16 16 10 10 0.7073 0.4222
17 17 10 10 0.8860 0.4222
18 18 10 10 0.8978 0.4222
19 19 10 10 0.9135 0.4222
20 20 10 10 0.9999 0.4222
~john-c/5421/mediansum.sas 02OCT06 18:53
~john-c/5421/mediansum.examp October 2, 2006.