Hi,
How to create a new variable as a group indicator which equals 0 if the value in certain row is less than the median of the 13 numbers below and 1 if it is greater and equal the median?
Thanks.
46.20
55.60
53.30
44.80
55.40
56.00
48.90
51.30
52.40
54.60
52.20
64.30
55.00
data one;
input num;
dummy=1;
cards;
46.20
55.60
53.30
44.80
55.40
56.00
48.90
51.30
52.40
54.60
52.20
64.30
55.00
;
run;
proc summary data=one;
var num;
id dummy;
output out=med_num (drop=_type_ _freq_) median=med_num;
run;
data two;
merge med_num one;
by dummy;
drop dummy;
above=(num>med_num);
run;
proc print data=two;
var num med_num above;
run;
Obs num med_num above
1 46.2 53.3 0
2 55.6 53.3 1
3 53.3 53.3 0
4 44.8 53.3 0
5 55.4 53.3 1
6 56.0 53.3 1
7 48.9 53.3 0
8 51.3 53.3 0
9 52.4 53.3 0
10 54.6 53.3 1
11 52.2 53.3 0
12 64.3 53.3 1
13 55.0 53.3 1
I hope this helps,
Dorota
data one;
input num;
dummy=1;
cards;
46.20
55.60
53.30
44.80
55.40
56.00
48.90
51.30
52.40
54.60
52.20
64.30
55.00
;
run;
proc summary data=one;
var num;
id dummy;
output out=med_num (drop=_type_ _freq_) median=med_num;
run;
data two;
merge med_num one;
by dummy;
drop dummy;
above=(num>med_num);
run;
proc print data=two;
var num med_num above;
run;
Obs num med_num above
1 46.2 53.3 0
2 55.6 53.3 1
3 53.3 53.3 0
4 44.8 53.3 0
5 55.4 53.3 1
6 56.0 53.3 1
7 48.9 53.3 0
8 51.3 53.3 0
9 52.4 53.3 0
10 54.6 53.3 1
11 52.2 53.3 0
12 64.3 53.3 1
13 55.0 53.3 1
I hope this helps,
Dorota
Use proc rank :
data have;
input num;
cards;
46.20
55.60
53.30
44.80
55.40
56.00
48.90
51.30
52.40
54.60
52.20
64.30
55.00
;
proc rank data=have out=want groups=2;
var num;
ranks group;
run;
proc print; run;
PG
@PGStat: Cool :smileycool:, but not quite obvious for a novice (someone who asks this kind of questions).
How about some old school data step approach:
data have;
input num;
cards;
46.20
55.60
53.30
44.80
55.40
56.00
48.90
51.30
52.40
54.60
52.20
64.30
55.00
;
data _null_;
set have nobs=nobs;
n=nobs-1;
call symputx('n',n);
stop;
run;
data want;
do _n_=1 by 1 until (last);
set have nobs=nobs end=last;
array mdn (0:&n) _temporary_;
mdn(mod(_n_,nobs))=num;
if last then median=median(of mdn(*));
end;
do until (last1);
set have end=last1;
if num<median then group=0; else group=1;
output;
end;
run;
proc print;run;
Haikuo
I will learn it, thanks.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.