BookmarkSubscribeRSS Feed
raddad34
Fluorite | Level 6

I have a large dataset that looks like this:

 

 

data have;
input finalVal value1 value2 value3;
datalines;
5 1 2 5
6 26 6 10
29 4 9 1
7 35 1 7
;

I want to find which of the 3 columns match and output that to a new column

 

like this:

data want;
input finalVal value1 value2 value3 matching_column $;
datalines;
5 1 2 5 value3
6 26 6 10 value2
29 4 9 1 none
7 35 1 7 value3
;

is this doable within a single proc sql or data step?

I was thinking I could make a series of if statements for everything, but I'm not sure if there is a more efficient way of doing this.

 

 

I would appreciate any input, thanks!

 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

I don't understand the logic here. Please explain further. I also don't see where any "matching columns" comes into play.

--
Paige Miller
Reeza
Super User

Do you want the variable name or just the index?

 

data want;
set have;

array _search(3) value1-value3;

index = whichn(finalVal, of _search(*));
if index ne 0 then variableName = vname(_search(index));
else variableName = 'none';

run;

 


@raddad34 wrote:

I have a large dataset that looks like this:

 

 

data have;
input finalVal value1 value2 value3;
datalines;
5 1 2 5
6 26 6 10
29 4 9 1
7 35 1 7
;

I want to find which of the 3 columns match and output that to a new column

 

like this:

data want;
input finalVal value1 value2 value3 matching_column $;
datalines;
5 1 2 5 value3
6 26 6 10 value2
29 4 9 1 none
7 35 1 7 value3
;

is this doable within a single proc sql or data step?

I was thinking I could make a series of if statements for everything, but I'm not sure if there is a more efficient way of doing this.

 

 

I would appreciate any input, thanks!

 


 

pink_poodle
Barite | Level 11
It is doable. There is a varname function that returns variable name. Please look at the example here:
https://documentation.sas.com/doc/en/sclref/9.4/p16g1sss185es1n1k1dhbxhsdqi2.htm

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1011 views
  • 2 likes
  • 4 in conversation