BookmarkSubscribeRSS Feed
Riteshdell
Quartz | Level 8

Hi All,

 

I have 3 values corresponsing to each ID.

I am looking for output as lowest V column corresponding ID.

In the output of Final column, Value should be picked always on V3 , V2, V1 sequence priority.

 

If all three is not null then V3

if V1 and V2 is not null  and V3 is nullthen V2

if v2 and v3 is null and V1 is not null then v1

 

note : ID is duplicated in rows.

 

Source :

 

data test;
infile datalines dlm=' ' dsd;
input id $ V1 $ V2 $ V3 $;
datalines;
1 x y z
2 x y
3 x
1 x y
3 x y z
;
run;

expecting output :

 

ID Final

1    z

2    y

3    x

1    y

3    z

3 REPLIES 3
PaigeMiller
Diamond | Level 26

@Riteshdell wrote:

Hi All,

 

I have 3 values corresponsing to each ID.

I am looking for output as lowest V column corresponding ID.

In the output of Final column, Value should be picked always on V3 , V2, V1 sequence priority.

 

If all three is not null then V3

 


if not missing(v1) and not missing(v2) and not missing(v3) then output=v3;

if V1 and V2 is not null  and V3 is nullthen V2

if v2 and v3 is null and V1 is not null then v1

 

These are obvious modifications of the code above.

--
Paige Miller
Astounding
PROC Star

An easy-to-understand approach:

 

final = v3;

if final = " " then final = v2;

if final = " " then final = v1;

gamotte
Rhodochrosite | Level 12

Hello,

 

Another solution using an array and coalescec function to retrieve the first non missing element :

 

data test;
	infile datalines dlm=' ' dsd;
	input id $ V1 $ V2 $ V3 $;
	length final $1.;
	array v(*) v3-v1;
	final=coalescec(of v(*));
	datalines;
1 x y z
2 x y
3 x
1 x y
3 x y z
;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 669 views
  • 3 likes
  • 4 in conversation