BookmarkSubscribeRSS Feed
jrevinzon
Calcite | Level 5

Lets say I have 100 columns with various character strings. I want to search all of the columns to see if they contain the word "Apple". I then want to create another column that creates a list of all the columns that contained the word "Apple". 

Example:

Col1     Col2     Col3       Results 

Apple    Berry   Pear       Col1 

Berry     Apple   Apple     Col1, Col 2

Pear     Pear     Pear       None 

 

Any help with this would be appreciated. 

2 REPLIES 2
novinosrin
Tourmaline | Level 20

Hi @jrevinzon  A quick clue for you. Look at WHICHC/WHICHN group of functions. I am lazy to write the code. But there are gazillion examples online. All the best!

Reeza
Super User
  1. Use WHICHC() to see fi the term is present

  2. If present, loop through and concatenate the names of the variables. 
  3. data want;
    set have;
    
    array _cols (*) col1-col3;
    
    length results $300.;
    call missing(Results);
    
    if whichc("Apple", of _cols(*)) then do i=1 to dim(_cols);
        if _cols(i) = "Apple" then results = catx(", ", results, vname(_cols(i)));
    end;
    
    run;

@jrevinzon wrote:

Lets say I have 100 columns with various character strings. I want to search all of the columns to see if they contain the word "Apple". I then want to create another column that creates a list of all the columns that contained the word "Apple". 

Example:

Col1     Col2     Col3       Results 

Apple    Berry   Pear       Col1 

Berry     Apple   Apple     Col1, Col 2

Pear     Pear     Pear       None 

 

Any help with this would be appreciated. 


 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 924 views
  • 3 likes
  • 3 in conversation