Have:
| id | value |
| 1 | 0 |
| 1 | 1 |
| 1 | 2 |
| 2 | 0 |
| 2 | 0 |
| 2 | 2 |
| 2 | 0 |
| 3 | 0 |
| 3 | 0 |
| 3 | 0 |
| 3 | 0 |
want:
| id | value |
| 1 | 1 |
| 2 | 2 |
| 3 | 0 |
I need to select one observation per subject. if the value is 1 then retain that observation. if not 1 then look for value 2 and retain it. if both 1 and 2 are not there then look for 0 and retian it.
I appreciate any help to get the output.
Thanks,
You can add in a custom sort variable, sort the variable and then take the first/last depending on your logic. Here's a way using proc format.
data have;
input id value;
value_order=put(value, order_fmt. -l);
cards;
1 0
1 1
1 2
2 0
2 0
2 2
2 0
3 0
3 0
3 0
3 0
;
proc format ;
value order_fmt 1=1 2=2 0=3;
run;
proc sort data=have;
by id value_order;
run;
data want;
set have;
by id;
if first.id;
run;
You can add in a custom sort variable, sort the variable and then take the first/last depending on your logic. Here's a way using proc format.
data have;
input id value;
value_order=put(value, order_fmt. -l);
cards;
1 0
1 1
1 2
2 0
2 0
2 2
2 0
3 0
3 0
3 0
3 0
;
proc format ;
value order_fmt 1=1 2=2 0=3;
run;
proc sort data=have;
by id value_order;
run;
data want;
set have;
by id;
if first.id;
run;
Thanks Reeza
Hi,
proc sql;
create table WANT as
select distinct A.ID,
case when exists(select distinct ID from HAVE where ID=A.ID and VALUE=1) then 1
when exists(select distinct ID from HAVE where ID=A.ID and VALUE=2) then 2
else 0 end as VALUE
from HAVE A;
quit;
Thanks RW9
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.