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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 509 views
  • 3 likes
  • 4 in conversation