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: Call for Content

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!

Submit your idea!

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
  • 2 replies
  • 894 views
  • 3 likes
  • 3 in conversation