<?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 position of each letter a in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-position-of-each-letter-a/m-p/547479#M151723</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input string :$50.;
datalines;
098ab-c1za3-8a1-8a-1a01212
;run;


data want;
 set have;
 do i=1 to lengthn(string);
  if char(string,i)='a' then do;position=i;output;end;
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 31 Mar 2019 12:57:48 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2019-03-31T12:57:48Z</dc:date>
    <item>
      <title>Find position of each letter a</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-position-of-each-letter-a/m-p/547461#M151709</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I want to find position for each letter "a" in a series.&lt;/P&gt;
&lt;P&gt;Is there any simple way like find(string,'a'2)?&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;HHCFX&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input string :$50.;
datalines;
098ab-c1za3-8a1-8a-1a01212
;run;



data want;set have;

first_a=find(string,'a');
second_a=find(string,'a',2);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Mar 2019 02:53:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-position-of-each-letter-a/m-p/547461#M151709</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2019-03-31T02:53:24Z</dc:date>
    </item>
    <item>
      <title>Re: Find position of each letter a</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-position-of-each-letter-a/m-p/547463#M151711</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input string :$50.;
datalines;
098ab-c1za3-8a1-8a-1a01212
;run;

data want;
set have;&lt;BR /&gt;a=0;
do pos=findc(string,'a') by 0 while (pos);
a+1;
output;
pos= findc(string,'a',pos+1) ;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 31 Mar 2019 03:06:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-position-of-each-letter-a/m-p/547463#M151711</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-03-31T03:06:21Z</dc:date>
    </item>
    <item>
      <title>Re: Find position of each letter a</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-position-of-each-letter-a/m-p/547470#M151716</link>
      <description>&lt;P&gt;&lt;SPAN&gt;novinosrin has provided the idea and I build further on his.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;hhchenfx is in looking for a SAS function to provide the&amp;nbsp; position and order of occurrence of 'a' in a string. I don't find a readymade SAS function for his desire. However, we can build a function using the SAS function compiler (FCMP). Here is a function:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;proc fcmp outlib = work.cmput.str;
   function findpatnext(str $, pat $);
      file log;
      static lastpos 1;
      pos = findc(str, pat, 'it', lastpos); /* edit: added semicolon */
      lastpos = pos + 1;
      return(pos);
   endsub;
quit;
&lt;/PRE&gt;
&lt;P&gt;In a Data Step we can call the function as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;options cmplib = work.cmput;

data want;
   length str $26;
   str = '098ab-c1za3-8a1-8a-1a01212';
   loc = 1;
   do order = 1 by 1 while(loc);
      loc = findpatnext(str, 'a');
      if loc then output;
   end;
run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;
&lt;P&gt;The output Data Set, WANT:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE class="table" aria-label="Data Set WORK.WANT"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;Obs&lt;/TH&gt;
&lt;TH class="header" scope="col"&gt;str&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;loc&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;order&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;1&lt;/TH&gt;
&lt;TD class="data"&gt;098ab-c1za3-8a1-8a-1a01212&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;2&lt;/TH&gt;
&lt;TD class="data"&gt;098ab-c1za3-8a1-8a-1a01212&lt;/TD&gt;
&lt;TD class="r data"&gt;10&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;3&lt;/TH&gt;
&lt;TD class="data"&gt;098ab-c1za3-8a1-8a-1a01212&lt;/TD&gt;
&lt;TD class="r data"&gt;14&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;4&lt;/TH&gt;
&lt;TD class="data"&gt;098ab-c1za3-8a1-8a-1a01212&lt;/TD&gt;
&lt;TD class="r data"&gt;18&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;5&lt;/TH&gt;
&lt;TD class="data"&gt;098ab-c1za3-8a1-8a-1a01212&lt;/TD&gt;
&lt;TD class="r data"&gt;21&lt;/TD&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We repeat the Data Step using NAME in SASHELP.CLASS as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
   set sashelp.class(keep = name);
   loc = 1;
   do order = 1 by 1 while(loc);
      loc = findpatnext(name, 'a');
      if loc then output;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;The output Data Set is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Obs 	Name 	loc 	order
1 	Alfred 	1 	1
2 	Alice 	1 	1
3 	Barbara 	2 	1
4 	Barbara 	5 	2
5 	Barbara 	7 	3
6 	Carol 	2 	1
7 	James 	2 	1
8 	Jane 	2 	1
9 	Janet 	2 	1
10 	Mary 	2 	1
11 	Ronald 	4 	1
12 	Thomas 	5 	1
13 	William 	6 	1&lt;/PRE&gt;
&lt;P&gt;Edited:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;In case, the n-th occurrence of 'a' is needed, the do-loop in the Data Step can be modified. Suppose the 3-rd occurrence is needed then the following Data Step can be used:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
   length str $26;
   str = '098ab-c1za3-8a1-8a-1a01212';
   loc = 1;
   do order = 1 by 1 while(loc);
      loc = findpatnext(str, 'a');
      if order = 3 then do; output; leave; end;
   end;
run;&lt;/PRE&gt;</description>
      <pubDate>Sun, 31 Mar 2019 08:17:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-position-of-each-letter-a/m-p/547470#M151716</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-03-31T08:17:09Z</dc:date>
    </item>
    <item>
      <title>Re: Find position of each letter a</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-position-of-each-letter-a/m-p/547473#M151718</link>
      <description>&lt;P&gt;A classic use of the Call PRXNEXT Routine&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input string :$50.;
datalines;
098ab-c1za3-8a1-8a-1a01212
;run;

data want(keep=string pos);
   set have;
   RegExID = prxparse('/a/');
   start=1;
   call prxnext(RegExID, start, length(string), string, pos, length);
      do while (pos &amp;gt; 0);
         output;
         call prxnext(RegExID, start, length(string), string, pos, length);
      end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 31 Mar 2019 07:56:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-position-of-each-letter-a/m-p/547473#M151718</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-03-31T07:56:50Z</dc:date>
    </item>
    <item>
      <title>Re: Find position of each letter a</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-position-of-each-letter-a/m-p/547479#M151723</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input string :$50.;
datalines;
098ab-c1za3-8a1-8a-1a01212
;run;


data want;
 set have;
 do i=1 to lengthn(string);
  if char(string,i)='a' then do;position=i;output;end;
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 31 Mar 2019 12:57:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-position-of-each-letter-a/m-p/547479#M151723</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-03-31T12:57:48Z</dc:date>
    </item>
    <item>
      <title>Re: Find position of each letter a</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-position-of-each-letter-a/m-p/547502#M151731</link>
      <description>&lt;P&gt;Thanks a lot.&lt;/P&gt;
&lt;P&gt;Very easy to follow code.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Sun, 31 Mar 2019 17:49:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-position-of-each-letter-a/m-p/547502#M151731</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2019-03-31T17:49:16Z</dc:date>
    </item>
  </channel>
</rss>

