BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
comeon2012
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
Dorota_Jarosz
Obsidian | Level 7

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

View solution in original post

5 REPLIES 5
Dorota_Jarosz
Obsidian | Level 7

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

PGStats
Opal | Level 21

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
Dorota_Jarosz
Obsidian | Level 7

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

Haikuo
Onyx | Level 15

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

comeon2012
Fluorite | Level 6

I will learn it, thanks.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 5081 views
  • 6 likes
  • 4 in conversation