[prev in list] [next in list] [prev in thread] [next in thread] 

List:       sas-l
Subject:    Re: how to find the multiple substring location from a long string
From:       Richard DeVenezia <rdevenezia () GMAIL ! COM>
Date:       2023-06-10 6:34:39
Message-ID: CACT-pRxNAyQcC7z2QGFzEQvDTu5ih1VMUtfNC02ck4i+7szVrQ () mail ! gmail ! com
[Download RAW message or body]

You can iterate repeated uses of the FIND() function

data have;
string = 'cdeABCefgABCjklmnABC';
run;

data want;
  set have;

  length needle $50;
  needle = 'ABC';

  p = -length(needle) + 1;

  if length(needle) <= length(string) then
    do until (p = 0);
      p = find(string,trim(needle), p+3);
      put p=;
    end;
run;

On Mon, May 15, 2023 at 6:40 PM Kai Yang <
00000f1278aa5087-dmarc-request@listserv.uga.edu> wrote:

> Hello SAS-l,
> I have a long string field (str_long, $5000.). I need to find a specific
> substring location in the long string. But I found the substring may shows
> multiple times in the long string. So, I need to know how many time the
> specific substring display in the long string and want to know the location
> for each of the substring.
> For example, if a substring " ABC" display 3 time in a long string, I need
> to have 4 new variables, first one will tell me: 3 times of " ABC" string
> are found in the long string, the rest of 3 variables will tell me the
> location of the substring.
> I can use index function to identify the location of first substring, but
> I don't know how to find the location of the rest of substrings.
>
> Thanks,
> Kai
>

[Attachment #3 (text/html)]

<div dir="ltr"><div class="gmail_default" style="font-family:arial,sans-serif">You \
can iterate repeated uses of the FIND() function</div><div class="gmail_default" \
style="font-family:arial,sans-serif"><br></div><div class="gmail_default" \
style="font-family:arial,sans-serif">data have;<br>string = \
&#39;cdeABCefgABCjklmnABC&#39;;<br>run;<br><br>data want;<br>   set have;<br><br>   \
length needle $50;<br>   needle = &#39;ABC&#39;;<br><br>   p = -length(needle) + \
1;<br><br>   if length(needle) &lt;= length(string) then<br>      do until (p = \
0);<br>         p = find(string,trim(needle), p+3);<br>         put p=;<br>      \
end;<br>run;<br></div></div><br><div class="gmail_quote"><div dir="ltr" \
class="gmail_attr">On Mon, May 15, 2023 at 6:40 PM Kai Yang &lt;<a \
href="mailto:00000f1278aa5087-dmarc-request@listserv.uga.edu">00000f1278aa5087-dmarc-request@listserv.uga.edu</a>&gt; \
wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div \
style="font-family:&quot;Helvetica \
Neue&quot;,Helvetica,Arial,sans-serif;font-size:13px"><div dir="ltr">Hello \
SAS-l,</div><div dir="ltr">I have a long string field (str_long, $5000.). I need to \
find a specific substring location in the long string. But I found the substring may \
shows multiple times in the long string. So, I need to know how many time the \
specific substring display in the long string and want to know the location for each \
of the substring.</div><div dir="ltr">For example, if a substring &quot; ABC&quot; \
display 3 time in a long string, I need to have 4 new variables, first one will tell \
me: 3 times of &quot; ABC&quot; string are found in the long string, the rest of 3 \
variables will tell me the location of the substring.</div><div dir="ltr">I can use \
index function to identify the location of first substring, but I don&#39;t know how \
to find the location of the rest of substrings.</div><div dir="ltr"><br></div><div \
dir="ltr">Thanks,</div><div dir="ltr">Kai</div></div></div></blockquote></div>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic