Solved
Contributor
Posts: 38

# How to create a group variable based on median

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

Accepted Solutions
Solution
‎07-20-2012 09:42 PM
Contributor
Posts: 66

## Re: How to create a group variable based on median

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

All Replies
Solution
‎07-20-2012 09:42 PM
Contributor
Posts: 66

## Re: How to create a group variable based on median

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

Posts: 5,541

## Re: How to create a group variable based on median

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

PG
Contributor
Posts: 66

## Re: How to create a group variable based on median

@PGStat: Cool :smileycool:, but not quite obvious for a novice (someone who asks this kind of questions).

Posts: 3,167

## Re: How to create a group variable based on median

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

Contributor
Posts: 38

## Re: How to create a group variable based on median

I will learn it, thanks.

🔒 This topic is solved and locked.