<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Searching for keywords in a column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888702#M351100</link>
    <description>&lt;P&gt;What is the desired result? Just identify if your source data matches to at least one word in your list or some table that gives your per row of your source data all the matching words?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 10 Aug 2023 00:00:04 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2023-08-10T00:00:04Z</dc:date>
    <item>
      <title>Searching for keywords in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888620#M351079</link>
      <description>&lt;P&gt;I have a list of keywords (175) in column A that I would like to search through column B (paragraph form sentences.) I've tried several different ways and keep getting errors.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;column_B_lc = lowcase(column_B);&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;do i = 1 to countw(column_A);&lt;BR /&gt;word = scan(column_A, i);&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;if indexc(column_B_lc, ' ' || word || ' ') &amp;gt; 0 then do;&lt;BR /&gt;output;&lt;BR /&gt;leave;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;drop i word column_B_lc; /* Drop temporary variables used in the loop */&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;any tips/suggestions are welcome!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Aug 2023 17:25:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888620#M351079</guid>
      <dc:creator>achapman</dc:creator>
      <dc:date>2023-08-09T17:25:44Z</dc:date>
    </item>
    <item>
      <title>Re: Searching for keywords in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888621#M351080</link>
      <description>&lt;P&gt;Make a simpler example with DATA.&amp;nbsp; You are also using the wrong function to search for words use INDEXW or FINDW.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Aug 2023 17:36:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888621#M351080</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2023-08-09T17:36:18Z</dc:date>
    </item>
    <item>
      <title>Re: Searching for keywords in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888653#M351086</link>
      <description>&lt;P&gt;Do you really have 175 different words in a single variable in one observation?&amp;nbsp; That is what your code would be expecting.&lt;/P&gt;
&lt;P&gt;The data step does not "search through a column" as I think you are hoping. It operates on mostly one observation at a time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any time you have an error you should include the LOG with all of the messages and the code. Copy the text from the log and on the forum open a text box using the &amp;lt;/&amp;gt; icon above the message window. Then paste the text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Aug 2023 19:50:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888653#M351086</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-08-09T19:50:03Z</dc:date>
    </item>
    <item>
      <title>Re: Searching for keywords in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888656#M351087</link>
      <description>&lt;P&gt;If you had something like this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile cards truncover ;
  length a $20 b $80;
  input a b $80.;
cards;
x a b c 
y x y z
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then to test if the WORD in A is in the string in B you could do something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  found = 0 &amp;lt; findw(b,a,,'sit') ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    a      b      found

 1     x    a b c      0
 2     y    x y z      1

&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Aug 2023 20:01:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888656#M351087</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-09T20:01:38Z</dc:date>
    </item>
    <item>
      <title>Re: Searching for keywords in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888684#M351093</link>
      <description>&lt;P&gt;Are you saying you have 175 distinct values for column_a (i.e. 175 rows, each with a single word)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or does each observation have a very long character variable (column_a) that has 175 words in it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And will you be using the same 175 words throughout all values of column_b?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Aug 2023 22:14:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888684#M351093</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-08-09T22:14:07Z</dc:date>
    </item>
    <item>
      <title>Re: Searching for keywords in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888688#M351095</link>
      <description>&lt;P&gt;&amp;nbsp;Sorry I didn't explain this very well. Yes, I have 175 distinct values (each a single word) that needs to be throughout all values of column_b.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Aug 2023 22:37:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888688#M351095</guid>
      <dc:creator>achapman</dc:creator>
      <dc:date>2023-08-09T22:37:01Z</dc:date>
    </item>
    <item>
      <title>Re: Searching for keywords in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888689#M351096</link>
      <description>&lt;P&gt;Here's a fictional example of how I do this.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Create words in a data set&amp;nbsp; (terms)&lt;/LI&gt;
&lt;LI&gt;Determine number of words in list (required to declare temporary array) (num_search_terms)&lt;/LI&gt;
&lt;LI&gt;In data step, load words from terms into a temporary array which can then be looped over in the data step in each row. Temporary arrays are loaded to memory so this is fairly efficient.&lt;/LI&gt;
&lt;LI&gt;Search through and flag for each row. This example just flags a 1 or 0 but you could flag which term was found or if multiple terms are found.&amp;nbsp;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/2f733d27820f43fa37d6ba92c30f22cf" target="_blank"&gt;https://gist.github.com/statgeek/2f733d27820f43fa37d6ba92c30f22cf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Aug 2023 22:46:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888689#M351096</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-08-09T22:46:39Z</dc:date>
    </item>
    <item>
      <title>Re: Searching for keywords in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888702#M351100</link>
      <description>&lt;P&gt;What is the desired result? Just identify if your source data matches to at least one word in your list or some table that gives your per row of your source data all the matching words?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Aug 2023 00:00:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888702#M351100</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-08-10T00:00:04Z</dc:date>
    </item>
    <item>
      <title>Re: Searching for keywords in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888711#M351105</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/446578"&gt;@achapman&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;Sorry I didn't explain this very well. Yes, I have 175 distinct values (each a single word) that needs to be throughout all values of column_b.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Sounds like you have two separate datasets.&amp;nbsp; The first one has 175 observations, each of which as a single word.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data terms;
  input term $20.;
cards;
The
Black
Cat
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And a second dataset that has some other number of observations.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sentences;
   input sentence $80;
cards;
Our car is black.
She wears a hat.
The cat is black.
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Perhaps by some strange coincidence that dataset also has 175 observations and that is what caused you to think they formed a single dataset?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if you want to match every term with every sentence then perhaps SQL is the way to go? Perhaps you only want to keep the terms that were found?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select a.term
     , findw(b.sentence,a.term,' .!,','site') as wordnum
     , b.sentence
from terms a 
   , sentences b
where calculated wordnum
order by 3,2,1
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    term     wordnum        sentence

 1     Black       4       Our car is black.
 2     The         1       The cat is black.
 3     Cat         2       The cat is black.
 4     Black       4       The cat is black.
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Aug 2023 03:49:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888711#M351105</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-10T03:49:43Z</dc:date>
    </item>
    <item>
      <title>Re: Searching for keywords in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888822#M351130</link>
      <description>&lt;P&gt;This is what I originally tried, but I keep getting 0 for found throughout the entire dataset, which I know is incorrect. Thanks for your help!&lt;/P&gt;</description>
      <pubDate>Thu, 10 Aug 2023 17:21:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888822#M351130</guid>
      <dc:creator>achapman</dc:creator>
      <dc:date>2023-08-10T17:21:40Z</dc:date>
    </item>
    <item>
      <title>Re: Searching for keywords in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888829#M351134</link>
      <description>&lt;P&gt;Make sure you are using the right delimiters for the FINDW() function.&amp;nbsp; Make sure to use the i modifier to ignore case and the t modifier to trim the trailing spaces.&lt;/P&gt;
&lt;P&gt;Pick just a couple of observations from each to test with so you can be sure about what is happening.&lt;/P&gt;
&lt;P&gt;Make sure to look at those values to be sure that there are not any strange invisible characters,&amp;nbsp; TAB, CR, LF, FF, Non-breaking space, null byte, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try using upcase(),strip() and perhaps compress() or translate() to eliminate bogus characters.&amp;nbsp; Add spaces before and after both the search term and the longer string so you use INDEX() to look for space delimited terms and still match at the start and the end of the string.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;index
(cat(' ',upcase(strip(translate(sentence,' ','090A0C0DA000FF'x))),' ')
,cat(' ',upcase(strip(translate(term,' ','090A0C0DA000FF'x))),' ')
)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Aug 2023 17:42:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888829#M351134</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-10T17:42:52Z</dc:date>
    </item>
    <item>
      <title>Re: Searching for keywords in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888830#M351135</link>
      <description>&lt;P&gt;&amp;nbsp;If none of the posted solutions work, please post a small example of your terms and your data. Feel free to make fake data and show the expected output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Aug 2023 17:50:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888830#M351135</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-08-10T17:50:15Z</dc:date>
    </item>
    <item>
      <title>Re: Searching for keywords in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888837#M351140</link>
      <description>&lt;P&gt;This worked PERFECTLY.&amp;nbsp; Thank you so much, I had tried an array before but I must have had an error somewhere because it was not giving me the right output.&amp;nbsp; Thanks again!&lt;/P&gt;</description>
      <pubDate>Thu, 10 Aug 2023 18:29:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-for-keywords-in-a-column/m-p/888837#M351140</guid>
      <dc:creator>achapman</dc:creator>
      <dc:date>2023-08-10T18:29:49Z</dc:date>
    </item>
  </channel>
</rss>

