SAS conditionally grouping

Viewed 27

I have a question about the conditional group dataset:

Here is my dataset:

data temp;
input id x1 x2 $;
cards;
1 25 A
1 35 C
1 20 B
3 33 D
;
run;

I want to group them by id and create a new variable Y. the output should like:

id x1 x2 y
1  25 A  C
1  35 C  C 
1  20 B  C
3  33 D  D

For example, if id is the same, I need to compare x2 C trumps B, and B trumps A, etc.

how should I code? Thank you very much!

1 Answers

Not sure if this will generalize but a SQL solution, where the max of X2 becomes your Y value for each ID.

proc sql;
create table want as
select *, max(x2) as y
from have
group by ID;
quit;
Related