<?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: Find content of variable in another variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716636#M221514</link>
    <description>&lt;P&gt;i don't know why it doesn't work on my dataset, when i do some dummy dataset it's ok.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;index finds only the last number of sequence, for example 1;2;3;4;5 it gives Y flag to 5 but when it's for example 3 it does not.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe it's something to do with delimiters?&lt;/P&gt;</description>
    <pubDate>Wed, 03 Feb 2021 20:10:34 GMT</pubDate>
    <dc:creator>Jedrzej</dc:creator>
    <dc:date>2021-02-03T20:10:34Z</dc:date>
    <item>
      <title>Find content of variable in another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716620#M221504</link>
      <description>&lt;P&gt;Hi, I want to create variable flag which will give Y if number from variable A will be in variable B&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;Flag&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1;2;3;4;5&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried index, find but nothing works...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you in advance&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 19:33:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716620#M221504</guid>
      <dc:creator>Jedrzej</dc:creator>
      <dc:date>2021-02-03T19:33:00Z</dc:date>
    </item>
    <item>
      <title>Re: Find content of variable in another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716622#M221506</link>
      <description>&lt;P&gt;Is A numeric or character?&lt;/P&gt;
&lt;P&gt;If it is numeric then how did you create a character version to use the Index function with?&lt;/P&gt;
&lt;P&gt;If you try to use a numeric variable in the Index function then the conversion to character will use a default format and usually results in leading blanks. So the value almost never is found.&lt;/P&gt;
&lt;P&gt;You need to control the conversion to character as in:&lt;/P&gt;
&lt;PRE&gt;data junk;
   a=2;
   b="1;2;3;4;5";
   flag = index(b,a);
   flagb = index(b,strip(put(a,best.)));
run;&lt;/PRE&gt;
&lt;P&gt;Flag is 0, i.e. a not found because of the conversion issue.&lt;/P&gt;
&lt;P&gt;Flagb is 3, i.e. the 2 was found in the 3rd position.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Personally I would use&lt;/P&gt;
&lt;PRE&gt;data junk;
   a=2;
   b="1;2;3;4;5";
   flag = ( index(b,strip(put(a,best.))) &amp;gt; 0);
run;&lt;/PRE&gt;
&lt;P&gt;Which creates a numeric value of 1, for true, and 0 for false on the find. A custom format could be used if you really need to see "Y". The coding I propose is more flexible for a number of calculations: Sum of flag is the number of 1, mean is the percent of 1s and you can write expressions like "if flag then &amp;lt;do something&amp;gt;" as SAS will treat 1 as true.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 19:41:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716622#M221506</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-02-03T19:41:48Z</dc:date>
    </item>
    <item>
      <title>Re: Find content of variable in another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716624#M221507</link>
      <description>&lt;P&gt;A and B are char not numeric&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 19:44:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716624#M221507</guid>
      <dc:creator>Jedrzej</dc:creator>
      <dc:date>2021-02-03T19:44:32Z</dc:date>
    </item>
    <item>
      <title>Re: Find content of variable in another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716628#M221509</link>
      <description>&lt;P&gt;Is variable A numeric or character ?&lt;/P&gt;
&lt;P&gt;I suppose variable B is char type.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If A is numeric transform it into char by &lt;STRONG&gt;strip&lt;/STRONG&gt;(&lt;STRONG&gt;put(a,best8.));&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;then you can use any of the next functions:&lt;/P&gt;
&lt;P&gt;1) if index(b,a) then flag='Y';&lt;/P&gt;
&lt;P&gt;2) if findw(b,a) then flag='Y';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 19:47:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716628#M221509</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-02-03T19:47:43Z</dc:date>
    </item>
    <item>
      <title>Re: Find content of variable in another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716636#M221514</link>
      <description>&lt;P&gt;i don't know why it doesn't work on my dataset, when i do some dummy dataset it's ok.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;index finds only the last number of sequence, for example 1;2;3;4;5 it gives Y flag to 5 but when it's for example 3 it does not.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe it's something to do with delimiters?&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 20:10:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716636#M221514</guid>
      <dc:creator>Jedrzej</dc:creator>
      <dc:date>2021-02-03T20:10:34Z</dc:date>
    </item>
    <item>
      <title>Re: Find content of variable in another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716644#M221518</link>
      <description>&lt;P&gt;Example data&lt;/P&gt;
&lt;P&gt;Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the &amp;lt;&amp;gt; icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And the actual code you are attempting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: if you have LEADING spaces in a value like "&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2", then you won't find the value in "1;2;3" because the string you search does not have the leading space. If you have trailing non-printable characters other than spaces you will have the same problem.&lt;/P&gt;
&lt;PRE&gt;data example;
   a='2';
   a2 = ' 2';
   a3=cats('2',"09"x);
   b='1;2;3;4;5';
   first= index(b,a);
   second= index(b,a2);
   third= index(b,a3);
run;&lt;/PRE&gt;
&lt;P&gt;The A3 value has a TAB character after the 2. When you print or examine the data in a SAS table viewer you won't see the tab and might think that is all there is to the value. But it does not match the '2' in the searched string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So specific data is needed to diagnose what is going on. Since you say it finds "5" I suspect something along the lines of intermittent values with leading spaces.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 20:29:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716644#M221518</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-02-03T20:29:04Z</dc:date>
    </item>
    <item>
      <title>Re: Find content of variable in another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716655#M221525</link>
      <description>&lt;P&gt;i had trailing blanks in var A, thank you&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 20:50:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716655#M221525</guid>
      <dc:creator>Jedrzej</dc:creator>
      <dc:date>2021-02-03T20:50:48Z</dc:date>
    </item>
    <item>
      <title>Re: Find content of variable in another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716660#M221528</link>
      <description>&lt;P&gt;what is the length of variable A ?&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 20:54:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716660#M221528</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-02-03T20:54:00Z</dc:date>
    </item>
    <item>
      <title>Re: Find content of variable in another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716665#M221529</link>
      <description>Yes&lt;BR /&gt;</description>
      <pubDate>Wed, 03 Feb 2021 21:10:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-content-of-variable-in-another-variable/m-p/716665#M221529</guid>
      <dc:creator>Jedrzej</dc:creator>
      <dc:date>2021-02-03T21:10:13Z</dc:date>
    </item>
  </channel>
</rss>

