<?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 the variable values in a list of words in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883043#M348917</link>
    <description>&lt;P&gt;Using | works fine for me.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input raw_name $80. ;
cards;
John Smith 
Liz Wood 
Eric Little
;

%let list=wood ERIC ;
%let list2=%sysfunc(translate(&amp;amp;list,|,%str( )));

data want;
  set have;
  found = 0&amp;lt;prxmatch("/\b(&amp;amp;list2)\b/i",raw_name);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1688095118233.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85480i8466511F1369AE9D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1688095118233.png" alt="Tom_0-1688095118233.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 30 Jun 2023 03:20:20 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-06-30T03:20:20Z</dc:date>
    <item>
      <title>search for the variable values in a list of words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883026#M348908</link>
      <description>&lt;P&gt;I have a dataset with one variable in a really messy format. I'd like to keep the rows with this variable value containing a list of words in both lower or upper case. There are around 20 words for this list.&lt;/P&gt;&lt;P&gt;My question is like this post:&lt;/P&gt;&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/Search-in-a-dataset-for-units-only-containing-words-in-a-list/m-p/842613#M82208" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/Search-in-a-dataset-for-units-only-containing-words-in-a-list/m-p/842613#M82208&lt;/A&gt;&lt;/P&gt;&lt;P&gt;But my word list is longer and I'd like to ignore the influence of lower or upper case.&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;How should I write the SAS code? Thank you.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;this is my code, but after running it, I get 0 observations.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%let med_list = a b c d ...;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;data data_filter;&lt;BR /&gt;set data;&lt;/P&gt;&lt;P&gt;if find(RAW_NAME,"&amp;amp;med_list", 'i') &amp;gt; 0;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Additionally, I also would like to change the RAW_Name to the matched word in med_list. How can I do it?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jun 2023 23:58:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883026#M348908</guid>
      <dc:creator>L777</dc:creator>
      <dc:date>2023-06-29T23:58:26Z</dc:date>
    </item>
    <item>
      <title>Re: search for the variable values in a list of words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883028#M348910</link>
      <description>&lt;P&gt;We can't test this for you until you add some test data to search through. Add a DATALINES statement to your DATA step and add a few rows of data we can test your program with.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jun 2023 00:58:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883028#M348910</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2023-06-30T00:58:35Z</dc:date>
    </item>
    <item>
      <title>Re: search for the variable values in a list of words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883030#M348911</link>
      <description>&lt;P&gt;You either have to add a LOOP.&amp;nbsp; You could loop over the list of words you want to find.&amp;nbsp; Or you could loop over the list of words in the string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or you could try to convert your string into a regular expression by using | as the delimiter in the list.&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jun 2023 01:33:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883030#M348911</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-30T01:33:14Z</dc:date>
    </item>
    <item>
      <title>Re: search for the variable values in a list of words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883037#M348914</link>
      <description>&lt;P&gt;I tried to use |&amp;nbsp;&lt;SPAN&gt;as the delimiter in the list, but it still does not work. I wonder how to write it in a loop?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jun 2023 02:50:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883037#M348914</guid>
      <dc:creator>L777</dc:creator>
      <dc:date>2023-06-30T02:50:17Z</dc:date>
    </item>
    <item>
      <title>Re: search for the variable values in a list of words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883041#M348916</link>
      <description>&lt;P&gt;Here how to test if any of a list of many words exist in a single (long) character variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data_filter;
   set data;
   array list [4] $5 _temporary_ ('a' 'b' 'c' 'd');
   found=0;
   do index=1 to dim(list) while (not found);
      found = 0&amp;lt;findw(RAW_NAME,list[index],' ','ir');
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is using spaces as the delimiter between the words in the long string.&amp;nbsp; The I modifier means to ignore case of letters.&amp;nbsp; The R modifier strips the delimiters from the word being searched for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To make the list of words to find flexible you just need to have the list of words as quoted words.&amp;nbsp; (You can get that from your example macro variable by using the &lt;A href="https://github.com/sasutils/macros/blob/master/qlist.sas" target="_self"&gt;%QLIST() macro&lt;/A&gt;).&amp;nbsp; The number of words in the list. (You can get that using the the COUNTW() function.)&amp;nbsp; And a maximum length for each word.&amp;nbsp; (Just pick something that it longer than the longest word you are looking for.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list=a b c d;
%let n=%sysfunc(countw(&amp;amp;list,%str( )));
data data_filter;
   set data;
   array list [&amp;amp;n] $5 _temporary_ %qlist(&amp;amp;list);
   found=0;
   do index=1 to dim(list) while (not found);
      found = 0&amp;lt;findw(RAW_NAME,list[index],' ','ir');
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or build them from from a dataset.&amp;nbsp; So if they are in a variable named WORD in a dataset named WORD_LIST you can use PROC SQL to make macros variables LIST, MAXLEN and N like this and then use them to define the temporary array.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select
   quote(trim(word),"'") 
 , max(length(word))
into :list separated by ' '
   , :maxlen trimmed     
from word_list
;
%let n=&amp;amp;sqlobs;

data date_filter;
   set data;
   array list [&amp;amp;n] $&amp;amp;maxlen (&amp;amp;list) ;
....&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Jun 2023 03:08:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883041#M348916</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-30T03:08:18Z</dc:date>
    </item>
    <item>
      <title>Re: search for the variable values in a list of words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883043#M348917</link>
      <description>&lt;P&gt;Using | works fine for me.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input raw_name $80. ;
cards;
John Smith 
Liz Wood 
Eric Little
;

%let list=wood ERIC ;
%let list2=%sysfunc(translate(&amp;amp;list,|,%str( )));

data want;
  set have;
  found = 0&amp;lt;prxmatch("/\b(&amp;amp;list2)\b/i",raw_name);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1688095118233.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85480i8466511F1369AE9D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1688095118233.png" alt="Tom_0-1688095118233.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jun 2023 03:20:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883043#M348917</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-30T03:20:20Z</dc:date>
    </item>
    <item>
      <title>Re: search for the variable values in a list of words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883045#M348918</link>
      <description>&lt;P&gt;Thank you so much! It works. I have one more question. How to replace the original value with the searched value directly if found=1. Let Liz Wood=Wood, and Eric Little= Eric, and delete the rows with found=0.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jun 2023 03:52:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883045#M348918</guid>
      <dc:creator>L777</dc:creator>
      <dc:date>2023-06-30T03:52:17Z</dc:date>
    </item>
    <item>
      <title>Re: search for the variable values in a list of words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883046#M348919</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/444323"&gt;@L777&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you so much! It works. I have one more question. How to replace the original value with the searched value directly if found=1. Let Liz Wood=Wood, and Eric Little= Eric, and delete the rows with found=0.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Probably easiest in that case to remember the actual location where the "word" was found rather than converting it to a binary result.&amp;nbsp; &amp;nbsp;You can take the first word from that location using SUBSTR() and SCAN().&amp;nbsp; (You can probably also do it directly with CALL SCAN() but that is more complicated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if FOUND has the location where the first match was made you could do what you want by using:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if found = 0 then delete;
raw_name=scan(substr(RAW_NAME,found),1,' ');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jun 2023 03:57:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883046#M348919</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-30T03:57:59Z</dc:date>
    </item>
    <item>
      <title>Re: search for the variable values in a list of words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883052#M348923</link>
      <description>&lt;P&gt;If I understand your question right then below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input raw_name $80. ;
cards;
John Smith 
Liz Wood 
Eric Little
;

%let list=wood ERIC ;
%let list2=%sysfunc(translate(&amp;amp;list,|,%str( )));

data want;
  set have;
  _prxid=prxparse("/\b(&amp;amp;list2)\b/i");
  call prxsubstr(_prxid,trim(raw_name),_pos,_len);
  if _pos&amp;gt;0 then 
    do;
      raw_name=substr(raw_name,_pos,_len);
      output;
    end;
  drop _:;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Jun 2023 06:44:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/search-for-the-variable-values-in-a-list-of-words/m-p/883052#M348923</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-06-30T06:44:12Z</dc:date>
    </item>
  </channel>
</rss>

