data one;
input a b c d;
cards;
1 34 34 2
2 33 33 4
2 33 44 5
3 43 44 3
4 23 23 2
5 34 33 1
5 33 23 2
6 22 33 4
4 34 45 3
;
run;
data one_1;
set one;
if d=2 then turn=1;
if d=5 then turn=2;
if d=1 then turn=3;
if d=4 then turn=4;
if d=3 then turn=5;
run;
proc sort data=one_1;
by a turn;
run;
data one_2;
set one_1;
by a turn;
if first.a;
run;
data one;
input a b c d;
cards;
1 34 34 2
2 33 33 4
2 33 44 5
3 43 44 3
4 23 23 2
5 34 33 1
5 33 23 2
6 22 33 4
4 34 45 3
;
run;
data one_1;
set one;
if d=2 then turn=1;
if d=5 then turn=2;
if d=1 then turn=3;
if d=4 then turn=4;
if d=3 then turn=5;
run;
proc sort data=one_1;
by a turn;
run;
data one_2;
set one_1;
by a turn;
if first.a;
run;