<?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 for a keyword contained within text and output other rows that have the same ID variable in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903452#M43963</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
length x $ 20;
x = "~keyword"; output;
x = "keyword"; output;
x = "~~keyword"; output;
run;

data _null_;
 set test;
 y=index(x, 'keyword');
 put (_all_) (=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;returns:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;x=~keyword y=2
x=keyword y=1
x=~~keyword y=3
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How does it looks with your data?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 16 Nov 2023 10:31:57 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2023-11-16T10:31:57Z</dc:date>
    <item>
      <title>Search for a keyword contained within text and output other rows that have the same ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903147#M43955</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I have a large dataset and I have used a where statement to search for a keyword that is contained in a character variable. The keyword is in less than 5% of the rows. When I find a keyword I'd like to also copy other rows that have the same ID. The ID is not unique and each ID is duplicated in around six rows.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've thought of using an IF THEN statement but I've not been able to make anything work that would simply output the other rows that contain the same ID. I'd really appreciate any advice.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using SAS Enterprise Guide (Ver 8.3.8.206).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code used&lt;/P&gt;&lt;PRE&gt;data want (compress=yes) ;
infile "C:\file_location.txt" dlm="|" dsd firstobs=2 truncover;
length
  ID $20.
  Num_1 4.
  Num_2 5.
  CHAR_1 $5.
  CHAR_2 $32767.
  CHAR_3 $32767.
  CHAR_4 $32767.
  CHAR_5 $32767.
 ;
input
  ID
  Num_1
  Num_2
  Num_1
  CHAR_1
  CHAR_2
  CHAR_3 
  CHAR_4
  CHAR_5
;
run;

DATA output1;
SET want;
   where CHAR_2 contains 'keyword'  ;
run;&lt;/PRE&gt;&lt;P&gt;Example of the dataset&lt;/P&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;&lt;LI-CODE lang="sas"&gt;Example dataset							
EVENT_ID	Num_1	Num_2	Char_1	Char_2	Char_3	Char_4	Char_5
145896555	19	25	A	Text	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896555	19	25	A	Text	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896556	19	25	A	Text	Text	Text	Text
145896556	19	25	B	Text	Text	Text	Text
145896556	19	25	B	Text Keyword	Text	Text	Text
145896556	19	25	B	Text	Text	Text	Text
145896556	19	25	B	Text	Text	Text	Text
145896556	19	25	B	Text	Text	Text	Text
145896557	19	25	B	Text	Text	Text	Text
145896557	19	25	B	Text	Text	Text	Text
145896557	19	25	B	Text	Text	Text	Text
145896557	19	25	B	Text	Text	Text	Text
							
							
I'd like the output dataset to read:							
145896556	19	25	A	Text	Text	Text	Text
145896556	19	25	B	Text	Text	Text	Text
145896556	19	25	B	Text Keyword	Text	Text	Text
145896556	19	25	B	Text	Text	Text	Text
145896556	19	25	B	Text	Text	Text	Text
145896556	19	25	B	Text	Text	Text	Text&lt;/LI-CODE&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>Wed, 15 Nov 2023 09:26:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903147#M43955</guid>
      <dc:creator>river1</dc:creator>
      <dc:date>2023-11-15T09:26:43Z</dc:date>
    </item>
    <item>
      <title>Re: Search for a keyword contained within text and output other rows that have the same ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903153#M43956</link>
      <description>&lt;P&gt;How about like that:&lt;/P&gt;
&lt;P&gt;1) read the data in loop one&lt;/P&gt;
&lt;P&gt;2) check every CHAR_* variable for a keyword (inner loop)&lt;/P&gt;
&lt;P&gt;3) if keyword is found store ID in a hash table&lt;/P&gt;
&lt;P&gt;4) read data second time (the second loop)&lt;/P&gt;
&lt;P&gt;5) check if ID in in hash table, if "yes" do output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (compress=yes) ;
infile cards4 dlm="|" dsd truncover;
length
  ID $20.
  Num_1 8
  Num_2 8
  CHAR_1 $5.
  CHAR_2 $32.
  CHAR_3 $32.
  CHAR_4 $32.
  CHAR_5 $32.
 ;
input
  ID
  Num_1
  Num_2
  CHAR_1
  CHAR_2
  CHAR_3 
  CHAR_4
  CHAR_5
;
cards4;
145896555|19|25|A|Text|Text|Text|Text
145896555|19|25|B|Text|Text|Text|Text
145896555|19|25|B|Text|Text|Text|Text
145896555|19|25|B|Text|Text|Text|Text
145896555|19|25|B|Text|Text|Text|Text
145896555|19|25|B|Text|Text|Text|Text
145896555|19|25|A|Text|Text|Text|Text
145896555|19|25|B|Text|Text|Text|Text
145896555|19|25|B|Text|Text|Text|Text
145896555|19|25|B|Text|Text|Text|Text
145896555|19|25|B|Text|Text|Text|Text
145896555|19|25|B|Text|Text|Text|Text
145896555|19|25|B|Text|Text|Text|Text
145896556|19|25|A|Text|Text|Text|Text
145896556|19|25|B|Text|Text|Text|Text
145896556|19|25|B|Text Keyword|Text|Text|Text
145896556|19|25|B|Text|Text|Text|Text
145896556|19|25|B|Text|Text|Text|Text
145896556|19|25|B|Text|Text|Text|Text
145896557|19|25|B|Text|Text|Text|Text
145896557|19|25|B|Text|Text|Text|Text
145896557|19|25|B|Text|Text|Text|Text
145896557|19|25|B|Text|Text|Text|Text
145891111|19|25|B|Text|Text|Text Keyword|Text
;;;;
run;

DATA output1;

declare hash H();
H.defineKey("ID");
H.defineDone();

do until(end1);
  SET want end=end1;
  
  array c[*] CHAR_:;

  do i=lbound(c) to hbound(c); drop i;
    if index(c[i], 'Keyword') then
      do;
        H.replace();
        put id=;
        leave;
      end; 
  end;
end;


do until(end2);
   SET want end=end2;

   if 0=H.check() then output;
end;


stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Nov 2023 10:22:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903153#M43956</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-11-15T10:22:59Z</dc:date>
    </item>
    <item>
      <title>Re: Search for a keyword contained within text and output other rows that have the same ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903158#M43957</link>
      <description>&lt;P&gt;Alternatively, if you assume:&lt;/P&gt;
&lt;P&gt;1) you know names of char variables in advance&lt;/P&gt;
&lt;P&gt;2) sum of lengths of that variables is less than 32767&lt;/P&gt;
&lt;P&gt;you can go with SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table output2 as
  select *
  from want
  where ID in
    (
      select ID 
      from want
      where index(catx("|",CHAR_1,CHAR_2,CHAR_3,CHAR_4,CHAR_5), 'Keyword')
    )
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;without assumption 2) it would be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table output3 as
  select *
  from want
  where ID in
    (
      select ID 
      from want
      where 
           index(CHAR_1, 'Keyword')
        or index(CHAR_2, 'Keyword')
        or index(CHAR_3, 'Keyword')
        or index(CHAR_4, 'Keyword')
        or index(CHAR_5, 'Keyword')
    )
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 15 Nov 2023 10:34:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903158#M43957</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-11-15T10:34:52Z</dc:date>
    </item>
    <item>
      <title>Re: Search for a keyword contained within text and output other rows that have the same ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903448#M43960</link>
      <description>&lt;P&gt;Thanks, that works perfectly however I have one problem. Some of the cells start with a tilde ~. I thought I'd solved the problem but the final output is still missing entries that started with the tilde ie if my keyword was 'elephantine' then the output would not include the IDs containing '~elephantine..'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was only searching for part of the word to scoop up any variations ie I was searching for 'phant' instead of 'elephantine' .&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can just search for '~' on its own and it displays all instances where the tilde ~ is included. There are no error messages.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you have any suggestions for why this is happening?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2023 09:59:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903448#M43960</guid>
      <dc:creator>river1</dc:creator>
      <dc:date>2023-11-16T09:59:39Z</dc:date>
    </item>
    <item>
      <title>Re: Search for a keyword contained within text and output other rows that have the same ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903450#M43961</link>
      <description>&lt;P&gt;Thanks, this code was great. But I still had the same problem as above, when there was a tilde ~ character at the start of the cell it did not output&lt;/P&gt;&lt;P&gt;ie '~keyword' would not cause the row to not output.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The tilde ~ characters are present in the want dataset but they are not outputting in the output3 dataset.&lt;/P&gt;&lt;P&gt;I've tried to search for '~keyword' and I've tried to remove the tilde character before running the SQL code but this did not work either.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Data want2 ;
set want;
Code=compress(Code,'~');
run;&lt;/PRE&gt;&lt;P&gt;Thanks for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2023 10:26:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903450#M43961</guid>
      <dc:creator>river1</dc:creator>
      <dc:date>2023-11-16T10:26:54Z</dc:date>
    </item>
    <item>
      <title>Re: Search for a keyword contained within text and output other rows that have the same ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903452#M43963</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
length x $ 20;
x = "~keyword"; output;
x = "keyword"; output;
x = "~~keyword"; output;
run;

data _null_;
 set test;
 y=index(x, 'keyword');
 put (_all_) (=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;returns:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;x=~keyword y=2
x=keyword y=1
x=~~keyword y=3
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How does it looks with your data?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2023 10:31:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903452#M43963</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-11-16T10:31:57Z</dc:date>
    </item>
    <item>
      <title>Re: Search for a keyword contained within text and output other rows that have the same ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903469#M43964</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/420704"&gt;@river1&lt;/a&gt;&amp;nbsp;If you just want to search for a substring in a string then the find() function will do this job for you. Using your data the code could look as simple as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.want;
  if _n_=1 then 
    do;
      dcl hash h1(dataset:'work.have(where=(find(cats(char_1,char_2,char_3,char_4,char_5),"keyword","i")))');
      h1.defineKey('event_id');
      h1.defineDone();
    end;
  set work.have;
  if h1.check()=0;
run;

proc print data=work.want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1700133098550.png" style="width: 538px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89923iD22320A9946C6B1C/image-dimensions/538x168?v=v2" width="538" height="168" role="button" title="Patrick_0-1700133098550.png" alt="Patrick_0-1700133098550.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2023 11:11:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903469#M43964</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-16T11:11:52Z</dc:date>
    </item>
    <item>
      <title>Re: Search for a keyword contained within text and output other rows that have the same ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903470#M43965</link>
      <description>&lt;P&gt;With the assumption that&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;total length of char_1, char_2, char_3, char_4, and char_5&lt;/LI-CODE&gt;
&lt;P&gt;does not exceeds 32767.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2023 11:15:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903470#M43965</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-11-16T11:15:47Z</dc:date>
    </item>
    <item>
      <title>Re: Search for a keyword contained within text and output other rows that have the same ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903479#M43967</link>
      <description>&lt;P&gt;Yes, that's the assumption I've made. Else we would need OR conditions ...which potentially would also perform a bit better.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.want;
  if _n_=1 then 
    do;
      dcl hash h1(dataset:'work.have(where=(
                                              find(char_1,"keyword","i") or
                                              find(char_2,"keyword","i") or
                                              find(char_3,"keyword","i") or
                                              find(char_4,"keyword","i") or
                                              find(char_5,"keyword","i")
                                            ))');
      h1.defineKey('event_id');
      h1.defineDone();
    end;
  set work.have;
  if h1.check()=0;
run;

proc print data=work.want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2023 12:30:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903479#M43967</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-16T12:30:55Z</dc:date>
    </item>
    <item>
      <title>Re: Search for a keyword contained within text and output other rows that have the same ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903631#M43976</link>
      <description>&lt;P&gt;Thanks that works perfectly! It outputs the rows and includes the tilde characters.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been trying to add a line to exclude keywords to reduce the dataset. I've tried the following but this is not working, do you have suggestions for an alternative?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data work.new;
  if _n_=1 then 
    do;
      dcl hash h1(dataset:'work.have(where=(find(cats(var_1),"keyword","i") and not find(cats(var_1), "keyword_2", "i")))'); ))');
      h1.defineKey('event_id');
      h1.defineDone();
    end;
  set work.have;
  if h1.check()=0;
run;&lt;/PRE&gt;&lt;PRE&gt;Example dataset							
ID	Num_1	Num_2	Char_1	Char_2	Char_3	Char_4	Char_5
145896555	19	25	A	Text	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896555	19	25	B	Keyword_1	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896556	19	25	A	Text	Text	Text	Text
145896556	19	25	B	Text	Text	Text	Text
145896556	19	25	B	Text	Text	Text	Text
145896556	19	25	B	Text	Text	Text	Text
145896557	19	25	B	Text	Text	Text	Text
145896557	19	25	B	Text	Text	Text	Text
145896557	19	25	B	Text	Text	Text	Text
145896557	19	25	A	Text	Text	Text	Text
145896557	19	25	B	Text	Text	Text	Text
145896558	19	25	B	Keyword_1	Text	Text	Text
145896558	19	25	B	Keyword_2	Text	Text	Text
145896558	19	25	B	Text	Text	Text	Text
145896558	19	25	B	Text	Text	Text	Text
145896559	19	25	B	Text	Text	Text	Text
145896559	19	25	B	Text	Text	Text	Text
145896559	19	25	B	Text	Text	Text	Text
145896559	19	25	B	Text	Text	Text	Text
							
							
I'd like the output dataset to read:							
145896555	19	25	A	Text	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text
145896555	19	25	B	Keyword_1	Text	Text	Text
145896555	19	25	B	Text	Text	Text	Text&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Nov 2023 12:02:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903631#M43976</guid>
      <dc:creator>river1</dc:creator>
      <dc:date>2023-11-17T12:02:43Z</dc:date>
    </item>
    <item>
      <title>Re: Search for a keyword contained within text and output other rows that have the same ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903765#M43977</link>
      <description>&lt;P&gt;Please provide sample data via fully working SAS datastep code as done below.&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover dlm='|' dsd;
  input ID  Num_1 Num_2 (Char_1  Char_2  Char_3  Char_4  Char_5) (:$10.);
datalines;
145896555|19|25|A|Text|Text|Text|Text
145896555|19|25|B|Text|Text|Text|Text
145896555|19|25|B|Text|Text|Text|Text
145896555|19|25|B|Text|Text|Text|Text
145896555|19|25|B|Keyword_1|Text|Text|Text
145896555|19|25|B|Text|Text|Text|Text
145896556|19|25|A|Text|Text|Text|Text
145896556|19|25|B|Text|Text|Text|Text
145896556|19|25|B|Text|Text|Text|Text
145896556|19|25|B|Text|Text|Text|Text
145896557|19|25|B|Text|Text|Text|Text
145896557|19|25|B|Text|Text|Text|Text
145896557|19|25|B|Text|Text|Text|Text
145896557|19|25|A|Text|Text|Text|Text
145896557|19|25|B|Text|Text|Text|Text
145896558|19|25|B|Keyword_1|Text|Text|Text
145896558|19|25|B|Keyword_2|Text|Text|Text
145896558|19|25|B|Text|Text|Text|Text
145896559|19|25|B|Text|Text|Text|Text
145896559|19|25|B|Text|Text|Text|Text
145896559|19|25|B|Text|Text|Text|Text
145896559|19|25|B|Text|Text|Text|Text
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;Your initial requirement was to select all rows with the same ID if at least one row matches your selection criteria. What gets loaded into the hash are all IDs that match this criteria with a single entry per selected ID.&lt;/P&gt;
&lt;P&gt;The extension to this requirement is now to exclude specific rows from the selected ones based on additional criteria.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you can do is further filter the selected rows from your base table with a matching ID in the hash table. Code like below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.want;
  if _n_=1 then 
    do;
      dcl hash h1(dataset:'work.have(where=(
                                              find(char_1,"keyword","i") or
                                              find(char_2,"keyword","i") or
                                              find(char_3,"keyword","i") or
                                              find(char_4,"keyword","i") or
                                              find(char_5,"keyword","i")
                                            ))');
      h1.defineKey('ID');
      h1.defineDone();
    end;
  set work.have;
  if  h1.check()=0 and
      find(char_1,"keyword_2","i")=0 and
      find(char_2,"keyword_2","i")=0 and
      find(char_3,"keyword_2","i")=0 and
      find(char_4,"keyword_2","i")=0 and
      find(char_5,"keyword_2","i")=0
      ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1700304492530.png" style="width: 528px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89981i058B950A5EFDA452/image-dimensions/528x214?v=v2" width="528" height="214" role="button" title="Patrick_0-1700304492530.png" alt="Patrick_0-1700304492530.png" /&gt;&lt;/span&gt;&lt;/P&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Nov 2023 10:49:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/903765#M43977</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-18T10:49:54Z</dc:date>
    </item>
    <item>
      <title>Re: Search for a keyword contained within text and output other rows that have the same ID variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/904029#M43979</link>
      <description>&lt;P&gt;Thank you. This looks much neater than what I ended up with. I ran the code three times. First to select the keyword of interest then I ran it twice to exclude the keywords I did not want ie&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; work.WANT;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if _n_ = &lt;STRONG&gt;1&lt;/STRONG&gt; then&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dcl hash h1(dataset: WORK.HAVE(where=(find(cats(VAR_1), "KEYWORD_2", "i") &amp;gt; 0))');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; h1.defineKey(VAR_2);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; h1.defineDone();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set WORK.HAVE;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if h1.check()ne &lt;STRONG&gt;0&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2023 23:39:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Search-for-a-keyword-contained-within-text-and-output-other-rows/m-p/904029#M43979</guid>
      <dc:creator>river1</dc:creator>
      <dc:date>2023-11-20T23:39:36Z</dc:date>
    </item>
  </channel>
</rss>

