<?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 a variable for multiple strings in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705568#M216485</link>
    <description>&lt;P&gt;To clarify, it does not have to be an exact match. I just want to subset to observations where one of the 200 strings appears somewhere within the variable's value.&lt;/P&gt;</description>
    <pubDate>Mon, 14 Dec 2020 01:08:36 GMT</pubDate>
    <dc:creator>chuakp</dc:creator>
    <dc:date>2020-12-14T01:08:36Z</dc:date>
    <item>
      <title>Searching a variable for multiple strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705565#M216483</link>
      <description>&lt;P&gt;I have a dataset with variable called drug_name. I'm want to subset to observations in which drug_name contains one of 200 strings corresponding to individual molecules. The brute force method is to use index repeatedly. For example, if there were only 3 strings of interest, I could do something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want; set have; where&lt;/P&gt;
&lt;P&gt;index(drug_name, "ACETAMINOPHEN") &amp;gt; 0 or&lt;/P&gt;
&lt;P&gt;index(drug_name, "IBUPROFEN") &amp;gt; 0 or&lt;/P&gt;
&lt;P&gt;index(drug_name, "DIPHENHYDRAMINE") &amp;gt; 0;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the more efficient way to do this? Thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 00:51:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705565#M216483</guid>
      <dc:creator>chuakp</dc:creator>
      <dc:date>2020-12-14T00:51:18Z</dc:date>
    </item>
    <item>
      <title>Re: Searching a variable for multiple strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705567#M216484</link>
      <description>&lt;P&gt;It depends on what you mean by "contains".&amp;nbsp; Doe sit mean the variable's value is an exact match to one of the 200 strings, or does it mean that the one of the 200 strings appears somewhere within the variable's value (possibly with other characters before or after).&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 00:58:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705567#M216484</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-12-14T00:58:18Z</dc:date>
    </item>
    <item>
      <title>Re: Searching a variable for multiple strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705568#M216485</link>
      <description>&lt;P&gt;To clarify, it does not have to be an exact match. I just want to subset to observations where one of the 200 strings appears somewhere within the variable's value.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 01:08:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705568#M216485</guid>
      <dc:creator>chuakp</dc:creator>
      <dc:date>2020-12-14T01:08:36Z</dc:date>
    </item>
    <item>
      <title>Re: Searching a variable for multiple strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705577#M216486</link>
      <description>&lt;P&gt;Then I think you are stuck.&amp;nbsp; You could remove all the comparison operators.&amp;nbsp; This would give you a valid comparison for one of the strings:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;where index(var, 'ACETAMINOPHEN') or .....;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also some functions have additional parameters that could ignore upper vs. lower case which might be a help.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 04:06:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705577#M216486</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-12-14T04:06:20Z</dc:date>
    </item>
    <item>
      <title>Re: Searching a variable for multiple strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705592#M216492</link>
      <description>&lt;P&gt;Do you have the list of strings as dataset? Could you post some observations, so that we have something to play with?&lt;/P&gt;
&lt;P&gt;SAS can create such a where-expression for you, the following code is hardly tested:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DrugList;
   input name $upcase20.;
   datalines;
ACETAMiNOPhEN
IBUProFEN
DIPHENHyDRAMiNE
;


data _null_;
   set DrugList end=jobDone;
   file "drug_filter.sas";
   length Buffer $ 100;
   
   if _n_ = 1 then do;
      put 'where';
   end;
   
   Buffer = cats('index(drug_name, ', quote(trim(name)), ')');
   put '  ' Buffer @;
   
   if jobDone then do;
      put;
      put ';';
   end;
   else do;
      put ' or ';
   end;
run;

data want;
   set have;
   %include "drug_filter.sas";
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Dec 2020 06:42:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705592#M216492</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-12-14T06:42:37Z</dc:date>
    </item>
    <item>
      <title>Re: Searching a variable for multiple strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705610#M216499</link>
      <description>&lt;P&gt;You can speed things up and simplify your program by using a format or an informat, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format lib=work;
  invalue drug (upcase)
    "ACETAMINOPHEN",
    "IBUPROFEN",
    "DIPHENHYDRAMINE" =1
    other=0;
run;
    
    
data have;
infile cards truncover;
input text $200.;
cards;
 erew ACETAmINOPHEN werwerrewwerrwe
sdfælø klgdsklæ klgsdf
rewqv IBUPROFEN 3432 4
sgsfdsgfdsgfdgs
werreerw DIPHENHYDRAMINE 3422323442
;run; 

data want;
  set have;
  do _N_=1 to countw(text) until(found);
    found=input(scan(text,_N_),drug.);
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You may need to put some options on the SCAN function to get the words right.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 08:30:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705610#M216499</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-12-14T08:30:22Z</dc:date>
    </item>
    <item>
      <title>Re: Searching a variable for multiple strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705646#M216520</link>
      <description>&lt;P&gt;This code works, thanks very much!&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 12:34:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705646#M216520</guid>
      <dc:creator>chuakp</dc:creator>
      <dc:date>2020-12-14T12:34:36Z</dc:date>
    </item>
    <item>
      <title>Re: Searching a variable for multiple strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705649#M216523</link>
      <description>&lt;P&gt;Thanks. I think this would work, but the challenge would be that I would have to create a format with 200 values since I'm looking for 200 strings.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 12:36:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705649#M216523</guid>
      <dc:creator>chuakp</dc:creator>
      <dc:date>2020-12-14T12:36:55Z</dc:date>
    </item>
    <item>
      <title>Re: Searching a variable for multiple strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705652#M216525</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; 
set have; 
if prxmatch('/\b(ACETAMINOPHEN|IBUPROFEN|DIPHENHYDRAMINE)\b/',drug_name );
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Dec 2020 12:39:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705652#M216525</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-12-14T12:39:20Z</dc:date>
    </item>
    <item>
      <title>Re: Searching a variable for multiple strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705938#M216628</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;Thanks. I think this would work, but the challenge would be that I would have to create a format with 200 values since I'm looking for 200 strings.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This is not a problem at all. Proc Format accepts a dataset when using the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=proc&amp;amp;docsetTarget=n1c16dxnndwfzyn14o1kb8a4312m.htm#n1l9h98t2b6lomn1ur1653iklmi9" target="_self"&gt;cntlin&lt;/A&gt;-option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 06:06:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Searching-a-variable-for-multiple-strings/m-p/705938#M216628</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-12-15T06:06:15Z</dc:date>
    </item>
  </channel>
</rss>

