<?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: Search string variable in one table with string variable from another table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Search-string-variable-in-one-table-with-string-variable-from/m-p/530971#M145233</link>
    <description>&lt;P&gt;2 examples:&lt;/P&gt;
&lt;P&gt;1. using a hash map and iterator&lt;/P&gt;
&lt;P&gt;2. using the point option in a set statement, that one is probably easier to understand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data search;
  length search_term $10;
  input search_term;
cards;
botox
ibuprofen
paracetamol
;
data names;
  length name $30;
  infile cards truncover dlm=',';
  informat name $char30.;
  input name ;
cards;
botox b
ipren (ibuprofen)
pamol (paracetamol)
glp-1
insulin
;
run;

/* data step using a hash map and an iterator */
data match;
  if _N_ = 1 then do;
    length search_term $10;
    declare hash hmap (dataset: 'work.search');
    declare hiter search ('hmap');
    rc = hmap.definekey('search_term');
    rc = hmap.definedata('search_term');
    call missing(search_term);
    rc = hmap.definedone();
  end;
  set names;
  found = 0; /* Search indicator, 0=no hit, 1=one or more hits */
  rc = search.first();
  do while (rc = 0);
    if find(name,search_term,'it') &amp;gt; 0 then do;
      /* Found the search_term in the name column */
      found = 1;
      rc = -1; * Stop searching;
    end;
    else rc = search.next();
  end;
  if found = 0 then search_term = '';
  drop rc;
run;

/* data step using the point= option */
data match;
  set names;
  found = 0;
  do i=1 to searchobs;
    set search point=i nobs=searchobs;
    if find(name,search_term,'it') &amp;gt; 0 then do;
      found = 1;
      leave; * Stop searching;
    end;
  end;
  if found = 0 then search_term = '';
  drop i ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 29 Jan 2019 13:54:32 GMT</pubDate>
    <dc:creator>MichaelLarsen</dc:creator>
    <dc:date>2019-01-29T13:54:32Z</dc:date>
    <item>
      <title>Search string variable in one table with string variable from another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Search-string-variable-in-one-table-with-string-variable-from/m-p/530944#M145218</link>
      <description>&lt;P&gt;I have two datasets. One, called "search", has 10 observations with a variable called search_term containing different strings.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The other one, called "names", has 100 observations with a string variable called "name", each of which has a different drug name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to use the index function to search variable "name" for each of the 10 search terms in "search." To be specific, I don't want to merge name with search_term (which would require an exact match) - I merely want to create an indicator 1 or 0 if any of the 10 values of search_term can be found inside the string "name." For example, if "name" equals "BOTOX A" and if one of the 10 values of search_term is "BOTOX" , the indicator should be set to 1 (and similar if "name" equals "BOTOX B."&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd appreciate any help with this. Thanks.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 13:20:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Search-string-variable-in-one-table-with-string-variable-from/m-p/530944#M145218</guid>
      <dc:creator>chuakp</dc:creator>
      <dc:date>2019-01-29T13:20:21Z</dc:date>
    </item>
    <item>
      <title>Re: Search string variable in one table with string variable from another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Search-string-variable-in-one-table-with-string-variable-from/m-p/530955#M145225</link>
      <description>&lt;P&gt;Show test data, in the form of a datastep.&amp;nbsp; As such just example code here:&lt;/P&gt;
&lt;PRE&gt;data _null_;
  set smalldata end=last;
  if _n_=1 then call execute('data want; set largedata;');
  call execute(cats('if index(yourstring,',smallstring,') then result=1;'));
  if last then call execute('run;');
run;  
&lt;/PRE&gt;
&lt;P&gt;This will generate a datastep with an if for each row in small dataset.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 13:36:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Search-string-variable-in-one-table-with-string-variable-from/m-p/530955#M145225</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-01-29T13:36:36Z</dc:date>
    </item>
    <item>
      <title>Re: Search string variable in one table with string variable from another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Search-string-variable-in-one-table-with-string-variable-from/m-p/530971#M145233</link>
      <description>&lt;P&gt;2 examples:&lt;/P&gt;
&lt;P&gt;1. using a hash map and iterator&lt;/P&gt;
&lt;P&gt;2. using the point option in a set statement, that one is probably easier to understand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data search;
  length search_term $10;
  input search_term;
cards;
botox
ibuprofen
paracetamol
;
data names;
  length name $30;
  infile cards truncover dlm=',';
  informat name $char30.;
  input name ;
cards;
botox b
ipren (ibuprofen)
pamol (paracetamol)
glp-1
insulin
;
run;

/* data step using a hash map and an iterator */
data match;
  if _N_ = 1 then do;
    length search_term $10;
    declare hash hmap (dataset: 'work.search');
    declare hiter search ('hmap');
    rc = hmap.definekey('search_term');
    rc = hmap.definedata('search_term');
    call missing(search_term);
    rc = hmap.definedone();
  end;
  set names;
  found = 0; /* Search indicator, 0=no hit, 1=one or more hits */
  rc = search.first();
  do while (rc = 0);
    if find(name,search_term,'it') &amp;gt; 0 then do;
      /* Found the search_term in the name column */
      found = 1;
      rc = -1; * Stop searching;
    end;
    else rc = search.next();
  end;
  if found = 0 then search_term = '';
  drop rc;
run;

/* data step using the point= option */
data match;
  set names;
  found = 0;
  do i=1 to searchobs;
    set search point=i nobs=searchobs;
    if find(name,search_term,'it') &amp;gt; 0 then do;
      found = 1;
      leave; * Stop searching;
    end;
  end;
  if found = 0 then search_term = '';
  drop i ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 13:54:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Search-string-variable-in-one-table-with-string-variable-from/m-p/530971#M145233</guid>
      <dc:creator>MichaelLarsen</dc:creator>
      <dc:date>2019-01-29T13:54:32Z</dc:date>
    </item>
    <item>
      <title>Re: Search string variable in one table with string variable from another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Search-string-variable-in-one-table-with-string-variable-from/m-p/530986#M145242</link>
      <description>&lt;P&gt;Hi chuakp&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is another posibility. You can join the two tables in Proc SQL, as shown in the following example. It uses the data sets provided by MichaelLarsen, and gives the same output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I prefer the SQL way in this case, becaause it it is simple and easy to read compared to the data steps, especially the hash-step. But if the datasets are very large, i would expect data steps to be faster.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* proc sql join on search_term found in name */
proc sql;
	create table match as
		select 
			t2.search_term,
			t1.name,
			t2.search_term ne '' as found
		from names as t1 left join search as t2
		on find(t1.name,t2.search_term,'it') &amp;gt; 0;
quit;
			&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Jan 2019 14:37:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Search-string-variable-in-one-table-with-string-variable-from/m-p/530986#M145242</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-01-29T14:37:33Z</dc:date>
    </item>
    <item>
      <title>Re: Search string variable in one table with string variable from another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Search-string-variable-in-one-table-with-string-variable-from/m-p/531015#M145263</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/312"&gt;@chuakp&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have two datasets. One, called "search", has 10 observations with a variable called search_term containing different strings.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you mean by&amp;nbsp;"different strings" that you need to match anyone of multiple values contained in a single variable then I suspect this search_term is built incorrectly for a good search. Which is why we ask for actual examples of your data.&lt;/P&gt;
&lt;P&gt;Example: if your search_term="Apple Pear" then searching for search_term in a name&amp;nbsp;string like "Apple Company" that you expect to match would generally&amp;nbsp;fail because the entire search_term is not part of the name string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 15:31:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Search-string-variable-in-one-table-with-string-variable-from/m-p/531015#M145263</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-01-29T15:31:01Z</dc:date>
    </item>
    <item>
      <title>Re: Search string variable in one table with string variable from another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Search-string-variable-in-one-table-with-string-variable-from/m-p/531129#M145315</link>
      <description>&lt;P&gt;Thanks so much - this worked. I also appreciated the suggestion regarding the hash tag approach.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 19:15:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Search-string-variable-in-one-table-with-string-variable-from/m-p/531129#M145315</guid>
      <dc:creator>chuakp</dc:creator>
      <dc:date>2019-01-29T19:15:11Z</dc:date>
    </item>
  </channel>
</rss>

