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.
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
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?
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?
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.