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

Have:

idvalue
10
11
12
20
20
22
20
30
30
30
30

 

want:

 

id value
11
22
30
  

 

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,

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

4 REPLIES 4
Reeza
Super User

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 783 views
  • 0 likes
  • 3 in conversation