<?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: finding multiple positions of the same phrase in a long string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/finding-multiple-positions-of-the-same-phrase-in-a-long-string/m-p/713413#M220093</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/272939"&gt;@aiannone&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;(...) I have a variable that contains a long string of test results: "blood-neg, blood-neg, blood-pos, serum-neg, blood-pos, serum-neg, blood-pos, blood-neg, blood-pos"&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;in this string I am specifically looking to find the position of the phrase "serum-neg, blood-pos"&lt;/P&gt;
&lt;P&gt;I have been using this code to find the position:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;result=findw(newvar_test, 'serum-neg, blood-pos', ' ' , 'E');&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;in the example above, the result I get is 4- which is correct! However, I would also like to get a result that says 6 since this is the position of the next occurrence of this phrase. (...)&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/272939"&gt;@aiannone&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think even to get &lt;FONT face="courier new,courier"&gt;result=4&lt;/FONT&gt; you would need to add the comma to the list of word delimiters:&lt;/P&gt;
&lt;PRE&gt;result=findw(newvar_test, 'serum-neg, blood-pos', ' &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;,&lt;/FONT&gt;&lt;/STRONG&gt;' , 'E');&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I would rather switch from FINDW to &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n1obc9u7z3225mn1npwnassehff0.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;CALL PRXNEXT&lt;/A&gt;&amp;nbsp;because that CALL routine gives you more flexibility in defining the search phrase. For example, there might be blanks or other white-space characters around the comma between "serum-neg" and "blood-pos" -- which you likely want to ignore -- or perhaps &lt;EM&gt;no&lt;/EM&gt; blanks. The Perl regular expression used below is also case-insensitive (cf. the "&lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt;" modifier of FINDW).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample data */

data have;
do id=1 to 2;
  newvar_test="blood-neg, blood-neg, blood-pos, Serum-NEG,blood-Pos, serum-neg ,   blood-pos, blood-neg, blood-pos";
  output;
end;
run;

/* Find positions of phrase */

data want(drop=_:);
set have;
if _n_=1 then _rid+prxparse('/serum-neg\s*,\s*blood-pos/i');
_s=1;
call prxnext(_rid, _s, -1, newvar_test, _p, _l);
do while(_p);
  result=countc(substr(newvar_test,1,_p-1),',')+1;
  output;
  call prxnext(_rid, _s, -1, newvar_test, _p, _l);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Ideally, however, you would receive those blood test data in the form&lt;/P&gt;
&lt;PRE&gt;Obs    ID            dt              test

 1      1    20JAN2021:07:00:00    blood-neg
 2      1    21JAN2021:07:00:00    blood-neg
 3      1    22JAN2021:07:00:00    blood-pos
 ...   ...   ...                   ...
&lt;/PRE&gt;
&lt;P&gt;so that you wouldn't need to rely on counting commas in long strings etc. to determine sequence numbers of measurements.&lt;/P&gt;</description>
    <pubDate>Fri, 22 Jan 2021 16:59:00 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2021-01-22T16:59:00Z</dc:date>
    <item>
      <title>finding multiple positions of the same phrase in a long string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-multiple-positions-of-the-same-phrase-in-a-long-string/m-p/713357#M220064</link>
      <description>&lt;P&gt;Hello! I have been stuck on this issue for a long time and would be super grateful if anyone could help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a variable that contains a long string of test results: "blood-neg, blood-neg, blood-pos, serum-neg, blood-pos, serum-neg, blood-pos, blood-neg, blood-pos"&lt;/P&gt;&lt;DIV class="branch"&gt;&lt;DIV&gt;&lt;DIV align="center"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;in this string I am specifically looking to find the position of the phrase "serum-neg, blood-pos"&lt;/P&gt;&lt;P&gt;I have been using this code to find the position:&amp;nbsp;&lt;/P&gt;&lt;P&gt;result=findw(newvar_test, 'serum-neg, blood-pos', ' ' , 'E');&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in the example above, the result I get is 4- which is correct! However, I would also like to get a result that says 6 since this is the position of the next occurrence of this phrase.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;No matter what I do, I am unable to figure this out. It is also tricky because the dataset contains thousands of observations and all of them have different test result variable lengths and orders.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way that I can get SAS to return all positions of the same phrase among this large string of test results?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2021 14:28:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-multiple-positions-of-the-same-phrase-in-a-long-string/m-p/713357#M220064</guid>
      <dc:creator>aiannone</dc:creator>
      <dc:date>2021-01-22T14:28:13Z</dc:date>
    </item>
    <item>
      <title>Re: finding multiple positions of the same phrase in a long string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-multiple-positions-of-the-same-phrase-in-a-long-string/m-p/713398#M220084</link>
      <description>&lt;DIV class="xis-refDictEntry"&gt;
&lt;DIV class="xis-syntax"&gt;
&lt;DIV class="xis-syntaxDescription"&gt;
&lt;DIV class="xis-requiredArgGroup"&gt;
&lt;DIV id="n0574160pvvviwn1vqs8dhqs4b1q" class="xis-argDescriptionPair"&gt;
&lt;DIV class="xis-argumentDescription"&gt;
&lt;DIV id="p1n0h9k1zxe3aen13o12nfr42d0n" class="xis-argDescriptionPair"&gt;
&lt;H4 class="xis-argument"&gt;e or E&lt;/H4&gt;
&lt;DIV class="xis-argumentDescription"&gt;
&lt;P class="xis-paraSimpleFirst"&gt;counts the words that are scanned until the specified word is found, &lt;STRONG&gt;instead of determining the character position&lt;/STRONG&gt; of the specified word in the string. Fragments of a word are not counted.&lt;/P&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Fri, 22 Jan 2021 16:07:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-multiple-positions-of-the-same-phrase-in-a-long-string/m-p/713398#M220084</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2021-01-22T16:07:52Z</dc:date>
    </item>
    <item>
      <title>Re: finding multiple positions of the same phrase in a long string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-multiple-positions-of-the-same-phrase-in-a-long-string/m-p/713413#M220093</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/272939"&gt;@aiannone&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;(...) I have a variable that contains a long string of test results: "blood-neg, blood-neg, blood-pos, serum-neg, blood-pos, serum-neg, blood-pos, blood-neg, blood-pos"&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;in this string I am specifically looking to find the position of the phrase "serum-neg, blood-pos"&lt;/P&gt;
&lt;P&gt;I have been using this code to find the position:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;result=findw(newvar_test, 'serum-neg, blood-pos', ' ' , 'E');&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;in the example above, the result I get is 4- which is correct! However, I would also like to get a result that says 6 since this is the position of the next occurrence of this phrase. (...)&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/272939"&gt;@aiannone&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think even to get &lt;FONT face="courier new,courier"&gt;result=4&lt;/FONT&gt; you would need to add the comma to the list of word delimiters:&lt;/P&gt;
&lt;PRE&gt;result=findw(newvar_test, 'serum-neg, blood-pos', ' &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;,&lt;/FONT&gt;&lt;/STRONG&gt;' , 'E');&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I would rather switch from FINDW to &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n1obc9u7z3225mn1npwnassehff0.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;CALL PRXNEXT&lt;/A&gt;&amp;nbsp;because that CALL routine gives you more flexibility in defining the search phrase. For example, there might be blanks or other white-space characters around the comma between "serum-neg" and "blood-pos" -- which you likely want to ignore -- or perhaps &lt;EM&gt;no&lt;/EM&gt; blanks. The Perl regular expression used below is also case-insensitive (cf. the "&lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt;" modifier of FINDW).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample data */

data have;
do id=1 to 2;
  newvar_test="blood-neg, blood-neg, blood-pos, Serum-NEG,blood-Pos, serum-neg ,   blood-pos, blood-neg, blood-pos";
  output;
end;
run;

/* Find positions of phrase */

data want(drop=_:);
set have;
if _n_=1 then _rid+prxparse('/serum-neg\s*,\s*blood-pos/i');
_s=1;
call prxnext(_rid, _s, -1, newvar_test, _p, _l);
do while(_p);
  result=countc(substr(newvar_test,1,_p-1),',')+1;
  output;
  call prxnext(_rid, _s, -1, newvar_test, _p, _l);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Ideally, however, you would receive those blood test data in the form&lt;/P&gt;
&lt;PRE&gt;Obs    ID            dt              test

 1      1    20JAN2021:07:00:00    blood-neg
 2      1    21JAN2021:07:00:00    blood-neg
 3      1    22JAN2021:07:00:00    blood-pos
 ...   ...   ...                   ...
&lt;/PRE&gt;
&lt;P&gt;so that you wouldn't need to rely on counting commas in long strings etc. to determine sequence numbers of measurements.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2021 16:59:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-multiple-positions-of-the-same-phrase-in-a-long-string/m-p/713413#M220093</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-01-22T16:59:00Z</dc:date>
    </item>
    <item>
      <title>Re: finding multiple positions of the same phrase in a long string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-multiple-positions-of-the-same-phrase-in-a-long-string/m-p/714038#M220377</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;THANK YOU SO MUCH! That worked! I am VERY appreciative!&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jan 2021 19:36:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-multiple-positions-of-the-same-phrase-in-a-long-string/m-p/714038#M220377</guid>
      <dc:creator>aiannone</dc:creator>
      <dc:date>2021-01-25T19:36:45Z</dc:date>
    </item>
  </channel>
</rss>

