## Is it solved? variable names as values when you have multiple variables

Solved
Occasional Contributor
Posts: 7

# Is it solved? variable names as values when you have multiple variables

I have 5 flags each having 1 and 0 (obviously). I have to write a code to get the column name when 1 is encountered. I came across a solution for using max or sum but I need to know if there is a solution when you come across two 1s, is there a way to get both the column names as the value? eg; A B C D E Column(obtained using vname) 1 0 0 0 0 A 0 1 1 0 0 B C 0 0 0 0 1 E 0 0 1 0 1 C E

Accepted Solutions
Solution
‎10-17-2017 12:45 AM
Super User
Posts: 6,933

## Re: Is it solved? variable names as values when you have multiple variables

It looks like the variable names aren't necessarily A, B, C, D, and E anymore.

You will need to decide whether you want a single variable to hold the flagged names (the approach I will use) vs. multiple variables.  here's one approach:

data want;

set have;

length flagged_names \$ 29;

array flags {5} pat_1 - pat_5;

do _n_=1 to 5;

if flags{_n_}=1 then flagged_names = catx(' ', flagged_names, vname(flags{_n_}) ) ;

end;

run;

Clearly if you have longer variable names, you may need to use a longer length for FLAGGED_NAMES.

All Replies
Super User
Posts: 24,001

## Re: Is it solved? variable names as values when you have multiple variables

If you have more than one you'll need to use a loop.

Ramakanthkrovi wrote:
I have 5 flags each having 1 and 0 (obviously). I have to write a code to get the column name when 1 is encountered. I came across a solution for using max or sum but I need to know if there is a solution when you come across two 1s, is there a way to get both the column names as the value? eg; A B C D E Column(obtained using vname) 1 0 0 0 0 A 0 1 1 0 0 B C 0 0 0 0 1 E 0 0 1 0 1 C E

Occasional Contributor
Posts: 7

## Re: Is it solved? variable names as values when you have multiple variables

[ Edited ]

array pat_(5) pat_:;
sum_value=sum(of pat_(*));
if sum_value = 1 then var_sum = vname(pat_(whichn(sum_value, of pat_(*))));
else

Part of the code which I am using and it is a random fix which does not give me the values of two columns. Can you help me with the part of code to change please?

Super User
Posts: 24,001

## Re: Is it solved? variable names as values when you have multiple variables

Sure, If you want to post sample data that I can work with.

Otherwise, feel free to search for do loop examples on here. Or if you only care about two ones, then call WHICHN twice.

If you can have, three, four, or five, how are you planning to deal with that?

Super User
Posts: 24,001

## Re: Is it solved? variable names as values when you have multiple variables

Solution
‎10-17-2017 12:45 AM
Super User
Posts: 6,933

## Re: Is it solved? variable names as values when you have multiple variables

It looks like the variable names aren't necessarily A, B, C, D, and E anymore.

You will need to decide whether you want a single variable to hold the flagged names (the approach I will use) vs. multiple variables.  here's one approach:

data want;

set have;

length flagged_names \$ 29;

array flags {5} pat_1 - pat_5;

do _n_=1 to 5;

if flags{_n_}=1 then flagged_names = catx(' ', flagged_names, vname(flags{_n_}) ) ;

end;

run;

Clearly if you have longer variable names, you may need to use a longer length for FLAGGED_NAMES.

Occasional Contributor
Posts: 7