BookmarkSubscribeRSS Feed
timj22
Calcite | Level 5

Hi everyone,

I have a dataset where one column is filled with column headers, and I was to return the value from that header back to that row.

For example

 

SAS DAta Set:

 

Name   Lookup     1     2     3     Return

John        2            a     d      v        d       

Tim          2            a     c      v        c

Kat          3             c     d      v        v

Jim          1             c     c      d        c

So as you can see from above

if lookup = 2 then return the value from column 2.

Now my dataset is much larger so I cant just use if/then to bring back every combination..

Any ideas?

 

Cheers

2 REPLIES 2
Astounding
PROC Star

This should be a straightforward application of arrays:

 

data want;

set have;

array vars {3} v1 v2 v3;

return = vars{lookup};

run;

 

Note that I illustrated using variable names of V1, V2, and V3, since 1, 2, and 3 are not valid variable names in SAS.  Also note that you may need to assign a length to RETURN before assigning a value.  If the variables in the array have different lengths, you may need to ensure that RETURN is assigned a long enough length to hold the longest value it might encounter.

Reeza
Super User

Look at the VVALUE/VVALUEX function.

 

 

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
  • 2 replies
  • 1518 views
  • 0 likes
  • 3 in conversation