<?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: Index any value in the list in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Index-any-value-in-the-list/m-p/647577#M193826</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input ID Field $30.;
cards;
1 VAL1, VAL40, VAL60
2 VAL0, VAL40, VAL61
3 VAL1, VAL40, VAL2
;


data want;
 set have;
 length want $50;
 do _n_=1 to countw(field,',');
  temp=strip(scan(field,_n_,','));
  if temp in ('VAL1','VAL2') then
  want=catx('-',want,temp);
 end;
 drop temp;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 13 May 2020 18:43:23 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-05-13T18:43:23Z</dc:date>
    <item>
      <title>Index any value in the list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-any-value-in-the-list/m-p/647552#M193817</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I want to search multiple values in a string field. e.g. currently i am using this code to look for either Val1 or Val2 :&lt;/P&gt;&lt;P&gt;Newfield = Index (Field, in "Val1", "Val2"))&lt;/P&gt;&lt;P&gt;The original field has values separted by comma like this Val1,Val2, ....,Val50&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SAS error says too many arguments.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas how can search more than 1 value in the same field?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 May 2020 17:42:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-any-value-in-the-list/m-p/647552#M193817</guid>
      <dc:creator>AZIQ1</dc:creator>
      <dc:date>2020-05-13T17:42:57Z</dc:date>
    </item>
    <item>
      <title>Re: Index any value in the list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-any-value-in-the-list/m-p/647556#M193819</link>
      <description>&lt;P&gt;why not just&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where&amp;nbsp;Field in ("Val1", "Val2");&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 May 2020 17:42:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-any-value-in-the-list/m-p/647556#M193819</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-13T17:42:12Z</dc:date>
    </item>
    <item>
      <title>Re: Index any value in the list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-any-value-in-the-list/m-p/647570#M193821</link>
      <description>&lt;P&gt;data looks like this:&lt;/P&gt;&lt;P&gt;Looking for VAL1 or VAL2 in "Field"&lt;BR /&gt;Have&lt;BR /&gt;ID Field&lt;BR /&gt;1 VAL1, VAL40, VAL60&lt;BR /&gt;2 VAL0, VAL40, VAL61&lt;BR /&gt;3 VAL1, VAL40, VAL2&lt;BR /&gt;&lt;BR /&gt;want&lt;BR /&gt;ID Field WANT Comments&lt;BR /&gt;1 VAL1, VAL40, VAL60 1 has VAL1&lt;BR /&gt;2 VAL0, VAL40, VAL61 0 does not have VAL1 OR VAL2&lt;BR /&gt;3 VAL1, VAL40, VAL2 2 has both VAL1 OR VAL2&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 May 2020 18:31:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-any-value-in-the-list/m-p/647570#M193821</guid>
      <dc:creator>AZIQ1</dc:creator>
      <dc:date>2020-05-13T18:31:10Z</dc:date>
    </item>
    <item>
      <title>Re: Index any value in the list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-any-value-in-the-list/m-p/647572#M193823</link>
      <description>&lt;P&gt;If you have a list of match terms&amp;nbsp;&lt;U&gt;and&lt;/U&gt; you need to know the index position of a match in the list do the following&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Place the terms in an array and use&amp;nbsp;the WHICHC function to find the matching index:&lt;/P&gt;
&lt;PRE&gt;data have;
  input field $; datalines;
xyzzy
foo 
bar 
123
walter
run;

data want;
  set have;
  length termIndex 8;
  array terms(15) $20 (
  "ralph",  "crimson", "zork",   "xyzzy", "romeo",
  "xyzzy",  "herald",  "beluga", "foo",   "snafu",
  "egbert", "herd",    "scope",  "bar",   "123"
  );

  termIndex = whichC (field, of terms(*));
run;          
&lt;/PRE&gt;
&lt;P&gt;A non-match will return 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you only need to test for&amp;nbsp;&lt;EM&gt;any match&lt;/EM&gt;&amp;nbsp;just use the IN operator&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;flag = field in (
  "ralph",  "crimson", "zork",   "xyzzy", "romeo",
  "xyzzy",  "herald",  "beluga", "foo",   "snafu",
  "egbert", "herd",    "scope",  "bar",   "123"
  );
&lt;/PRE&gt;
&lt;P&gt;For a list of 50 terms, you might be better off putting the values in a separate data set and querying.&amp;nbsp; The control of the terms is easier when in a data set, and source code does not need to be changed if the terms change.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;data terms;
infile datalines dsd dlm=',';
input term $ @@; 
if not missing(term);
datalines;
  "ralph",  "crimson", "zork",   "xyzzy", "romeo",
  "xyzzy",  "herald",  "beluga", "foo",   "snafu",
  "egbert", "herd",    "scope",  "bar",   "123"
;

proc sql;
  create table want as
  select 
    have.field, 
    exists (select * from terms where terms.term=have.field) as flag
  from
    have
  ;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 May 2020 18:32:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-any-value-in-the-list/m-p/647572#M193823</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-05-13T18:32:34Z</dc:date>
    </item>
    <item>
      <title>Re: Index any value in the list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-any-value-in-the-list/m-p/647577#M193826</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input ID Field $30.;
cards;
1 VAL1, VAL40, VAL60
2 VAL0, VAL40, VAL61
3 VAL1, VAL40, VAL2
;


data want;
 set have;
 length want $50;
 do _n_=1 to countw(field,',');
  temp=strip(scan(field,_n_,','));
  if temp in ('VAL1','VAL2') then
  want=catx('-',want,temp);
 end;
 drop temp;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 May 2020 18:43:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-any-value-in-the-list/m-p/647577#M193826</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-13T18:43:23Z</dc:date>
    </item>
    <item>
      <title>Re: Index any value in the list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Index-any-value-in-the-list/m-p/647583#M193829</link>
      <description>&lt;P&gt;When the data value itself is string that is a comma separated value list you can use the INDEXW function to locate the position of a word in the value.&amp;nbsp; If you increase the number of flag cases the number of comments possibilities increase in a combinatoric fashion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;data have;
input ID &amp;amp; Field $40.;
datalines;
1  VAL1, VAL40, VAL60
2  VAL0, VAL40, VAL61
3  VAL1, VAL40, VAL2
;

data want;
  set have;

  flag1 = indexw(field, 'VAL1', ' ,') &amp;gt; 0;
  flag2 = indexw(field, 'VAL2', ' ,') &amp;gt; 0;

  want = flag1 + flag2;

  length comments $50;
  select (cats(flag1,flag2));
    when ('00') comments = 'Has neither VAL1 nor VAL2';
    when ('10') comments = 'Has only VAL1';
    when ('01') comments = 'Has only VAL2';
    otherwise comments = 'Has both VAL1 and VAL2';
  end;    
run;&lt;/PRE&gt;
&lt;P&gt;For more flagging based on more complex patterns you can use PRXMATCH function to search using a PERL regular expression.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 May 2020 18:52:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Index-any-value-in-the-list/m-p/647583#M193829</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-05-13T18:52:42Z</dc:date>
    </item>
  </channel>
</rss>

