BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ramakanthkrovi
Obsidian | Level 7
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
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

6 REPLIES 6
Reeza
Super User

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

 

Ramakanthkrovi
Obsidian | Level 7

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?

Reeza
Super User

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?

 

Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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