DATA Step, Macro, Functions and more

How to indirectly refer to a column header

Reply
New User
Posts: 1

How to indirectly refer to a column header

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

Super User
Posts: 5,081

Re: How to indirectly refer to a column header

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.

Super User
Posts: 17,784

Re: How to indirectly refer to a column header

Look at the VVALUE/VVALUEX function.

 

 

Ask a Question
Discussion stats
  • 2 replies
  • 211 views
  • 0 likes
  • 3 in conversation