<?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: Susbstrn of mixed characters and numbers in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Susbstrn-of-mixed-characters-and-numbers/m-p/497828#M132099</link>
    <description>&lt;P&gt;Thank you RW9 for the quick response. All of the variables are coded as a character. However, the above code would not retain the observations needed. Would it be possible to provide an alternative coding?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 21 Sep 2018 14:16:46 GMT</pubDate>
    <dc:creator>lillymaginta</dc:creator>
    <dc:date>2018-09-21T14:16:46Z</dc:date>
    <item>
      <title>Susbstrn of mixed characters and numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Susbstrn-of-mixed-characters-and-numbers/m-p/497823#M132096</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input $ dx1 dx2 dx3;
cards;
ft123 rg12 st12
st1243 rt1 rt2
st1gt dr1 123; 
Run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I have the following data where dx variables are characters&amp;nbsp;but include both numbers/letters. I am trying to use the following substrn&amp;nbsp;statement to retain dx's starting with 'st1' but getting zero results back.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&amp;nbsp;want(drop=i);set have;; array x{*} dx:; st=&lt;STRONG&gt;1&lt;/STRONG&gt;=&lt;STRONG&gt;0&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;do i=&lt;STRONG&gt;1&lt;/STRONG&gt; to dim(x); if SUBSTRN(x{i},&lt;STRONG&gt;1&lt;/STRONG&gt;,&lt;STRONG&gt;3&lt;/STRONG&gt;)&lt;/P&gt;&lt;P&gt;in ('st1' )&lt;/P&gt;&lt;P&gt;&amp;nbsp;then do;st=&lt;STRONG&gt;1&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; leave;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Sep 2018 14:06:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Susbstrn-of-mixed-characters-and-numbers/m-p/497823#M132096</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2018-09-21T14:06:20Z</dc:date>
    </item>
    <item>
      <title>Re: Susbstrn of mixed characters and numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Susbstrn-of-mixed-characters-and-numbers/m-p/497825#M132097</link>
      <description>&lt;P&gt;An array can only reference data of one type, either character or numeric, but not both.&amp;nbsp; All your data there is character, as at least on cell contains character data, so can't be numeric.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Sep 2018 14:13:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Susbstrn-of-mixed-characters-and-numbers/m-p/497825#M132097</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-09-21T14:13:36Z</dc:date>
    </item>
    <item>
      <title>Re: Susbstrn of mixed characters and numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Susbstrn-of-mixed-characters-and-numbers/m-p/497826#M132098</link>
      <description>&lt;P&gt;An array can only reference data of one type, either character or numeric, but not both.&amp;nbsp; All your data there is character, as at least on cell contains character data, so can't be numeric.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Sep 2018 14:13:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Susbstrn-of-mixed-characters-and-numbers/m-p/497826#M132098</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-09-21T14:13:37Z</dc:date>
    </item>
    <item>
      <title>Re: Susbstrn of mixed characters and numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Susbstrn-of-mixed-characters-and-numbers/m-p/497828#M132099</link>
      <description>&lt;P&gt;Thank you RW9 for the quick response. All of the variables are coded as a character. However, the above code would not retain the observations needed. Would it be possible to provide an alternative coding?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Sep 2018 14:16:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Susbstrn-of-mixed-characters-and-numbers/m-p/497828#M132099</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2018-09-21T14:16:46Z</dc:date>
    </item>
    <item>
      <title>Re: Susbstrn of mixed characters and numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Susbstrn-of-mixed-characters-and-numbers/m-p/497847#M132112</link>
      <description>&lt;P&gt;You should show what the desired output would be from the given input, best is also as data step. Providing code that does not perform does not really provide a good description of the actual desired output.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Sep 2018 14:45:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Susbstrn-of-mixed-characters-and-numbers/m-p/497847#M132112</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-09-21T14:45:30Z</dc:date>
    </item>
    <item>
      <title>Re: Susbstrn of mixed characters and numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Susbstrn-of-mixed-characters-and-numbers/m-p/497858#M132120</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input $ id dx1 dx2 dx3;
cards;
1  ft123   rg12   st12
2  st1243  rt1    rt2
3  st1gt   dr1    123
4  rt1     rt1    rt1
5  gt1     gt1    gt1; 
Run; 

Retain observations that have 'st1':

Output 
1  ft123   rg12   st12
2  st1243  rt1    rt2
3  st1gt   dr1    123&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Sep 2018 15:05:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Susbstrn-of-mixed-characters-and-numbers/m-p/497858#M132120</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2018-09-21T15:05:50Z</dc:date>
    </item>
    <item>
      <title>Re: Susbstrn of mixed characters and numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Susbstrn-of-mixed-characters-and-numbers/m-p/497875#M132129</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/72105"&gt;@lillymaginta&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input $ id dx1 dx2 dx3;
cards;
1  ft123   rg12   st12
2  st1243  rt1    rt2
3  st1gt   dr1    123
4  rt1     rt1    rt1
5  gt1     gt1    gt1; 
Run; 

Retain observations that have 'st1':

Output 
1  ft123   rg12   st12
2  st1243  rt1    rt2
3  st1gt   dr1    123&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;First, test any data step code to ensure it works. Yours has several errors.&lt;/P&gt;
&lt;P&gt;This looks like one way of what you ask for:&lt;/P&gt;
&lt;PRE&gt;data have;
input  id  dx1 $ dx2 $ dx3 $;
cards;
1  ft123   rg12   st12
2  st1243  rt1    rt2
3  st1gt   dr1    123
4  rt1     rt1    rt1
5  gt1     gt1    gt1
; 
Run; 

data want;
   set have;
   array c _character_;
   do i= 1 to dim(c);
      if index(c[i],'st1') &amp;gt; 0 then do;
         output;
         leave;
      end;
   end;
   drop i;
run;
&lt;/PRE&gt;
&lt;P&gt;If there are only certain variables you want to search for the value then place the names of the variables in place of _character_ which will search all character variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second, this is case sensitive. If you also want to match "sT1" "St1" and "ST1" then use&lt;/P&gt;
&lt;PRE&gt;if index(upcase(c[i]),'ST1') &amp;gt; 0 then do;
&lt;/PRE&gt;
&lt;P&gt;The LEAVE instruction exits the loop when executed, in effect when the first match is found in this case, so only one row is output in the case of multiple variables containing the search string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Sep 2018 15:40:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Susbstrn-of-mixed-characters-and-numbers/m-p/497875#M132129</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-09-21T15:40:09Z</dc:date>
    </item>
    <item>
      <title>Re: Susbstrn of mixed characters and numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Susbstrn-of-mixed-characters-and-numbers/m-p/497878#M132132</link>
      <description>&lt;P&gt;You might want to use one of the CAT...() functions to allow you to search all of the strings in one command.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  if index('^'||catx('^',of dx1-dx3),'^st1');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Sep 2018 15:48:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Susbstrn-of-mixed-characters-and-numbers/m-p/497878#M132132</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-09-21T15:48:24Z</dc:date>
    </item>
  </channel>
</rss>

