<?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 Need Help to find first instance of word in a SAS string and update it in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-Help-to-find-first-instance-of-word-in-a-SAS-string-and/m-p/847011#M334869</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;I need help on below scenario -&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt;a1= "dd|bb|aa|aa|cc|aa|dd";&lt;BR /&gt;/*I need to update only first instance of aa with " " */&lt;BR /&gt;/*need*/&lt;BR /&gt;a1= "dd|bb| |aa|cc|aa|dd";&lt;/P&gt;
&lt;P&gt;/*First I need to find out the first instance of aa and update it to " " in the same string.*/&lt;BR /&gt;/*i tried tranwrd but its removing all the instances of aa with " "&amp;nbsp; like&amp;nbsp;a1= "dd|bb| | |cc| |dd"; */&lt;BR /&gt;/*Also, I will be doing in iterations so every time I need to use updated value of a1*/&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 30 Nov 2022 11:11:14 GMT</pubDate>
    <dc:creator>nitink26</dc:creator>
    <dc:date>2022-11-30T11:11:14Z</dc:date>
    <item>
      <title>Need Help to find first instance of word in a SAS string and update it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-Help-to-find-first-instance-of-word-in-a-SAS-string-and/m-p/847011#M334869</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;I need help on below scenario -&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt;a1= "dd|bb|aa|aa|cc|aa|dd";&lt;BR /&gt;/*I need to update only first instance of aa with " " */&lt;BR /&gt;/*need*/&lt;BR /&gt;a1= "dd|bb| |aa|cc|aa|dd";&lt;/P&gt;
&lt;P&gt;/*First I need to find out the first instance of aa and update it to " " in the same string.*/&lt;BR /&gt;/*i tried tranwrd but its removing all the instances of aa with " "&amp;nbsp; like&amp;nbsp;a1= "dd|bb| | |cc| |dd"; */&lt;BR /&gt;/*Also, I will be doing in iterations so every time I need to use updated value of a1*/&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2022 11:11:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-Help-to-find-first-instance-of-word-in-a-SAS-string-and/m-p/847011#M334869</guid>
      <dc:creator>nitink26</dc:creator>
      <dc:date>2022-11-30T11:11:14Z</dc:date>
    </item>
    <item>
      <title>Re: Need Help to find first instance of word in a SAS string and update it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-Help-to-find-first-instance-of-word-in-a-SAS-string-and/m-p/847016#M334873</link>
      <description>&lt;P&gt;Try this. This replaces the first instance of |aa| with | | . (In the a2 string)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
   a1 = "dd|bb|aa|aa|cc|aa|dd";
   a2 = prxchange('s/\|aa\|/| |/', 1, a1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2022 11:22:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-Help-to-find-first-instance-of-word-in-a-SAS-string-and/m-p/847016#M334873</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-11-30T11:22:06Z</dc:date>
    </item>
    <item>
      <title>Re: Need Help to delete first instance of word in a SAS string and update it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-Help-to-find-first-instance-of-word-in-a-SAS-string-and/m-p/847020#M334875</link>
      <description>&lt;P&gt;You could use PRXCHANGE, which allows you to specify how many times the replacement should be done (2nd parameter) e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  a1="dd|bb|aa|aa|cc|aa|dd";
  a1=prxchange('s/aa/ /',1,a1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you need to replace only "aa" and not "aa1" or "aaa", it may be better to use FINDW and replace substrings:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  a1= "dd|bb|aa1|aa1|cc|aa";
  pos=findw(a1,'aa','|');
  if pos then do;
    substr(a1,pos,1)=' ';
    substr(a1,pos+1)=substr(a1,pos+2);
    end;
  drop pos;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Though a bit more tedious, the FINDW solution may also be faster, as the PRX functions generally require more overhead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit note: I put in the "if pos then do;" contdition, as you will otherwise get an error if no "aa" is found.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2022 11:43:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-Help-to-find-first-instance-of-word-in-a-SAS-string-and/m-p/847020#M334875</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-11-30T11:43:53Z</dc:date>
    </item>
    <item>
      <title>Re: Need Help to find first instance of word in a SAS string and update it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-Help-to-find-first-instance-of-word-in-a-SAS-string-and/m-p/847022#M334876</link>
      <description>&lt;P&gt;One possible problem with your solution: it will not replace the first occurrence in a string that starts with an "aa", like "aa|aa|bb".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried putting in look-ahead and look-behind operators in a PRX solution, but the "$|\|" necessary in the look-behind string (beginning of string or "\") is considered variable length and does not work.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2022 11:41:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-Help-to-find-first-instance-of-word-in-a-SAS-string-and/m-p/847022#M334876</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-11-30T11:41:07Z</dc:date>
    </item>
    <item>
      <title>Re: Need Help to find first instance of word in a SAS string and update it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-Help-to-find-first-instance-of-word-in-a-SAS-string-and/m-p/847041#M334889</link>
      <description>&lt;P&gt;This issue of identifying a string (including the possibility of a missing delimiter at the beginning or end) has been around for a while.&amp;nbsp; Since before parsing functions were added to the software in fact.&amp;nbsp; Here is an old school approach:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
   a1 = "dd|bb|aa|aa|cc|aa|dd";
   found_aa = index("|" || trim(a1) || "|", "|aa|");
   if found_aa &amp;gt; 0 then substr(a1, found_aa, 2) = "  ";
   drop found_aa;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The formula for FOUND_AA is actually trickier than it looks, since we are searching for "|aa|" but want to change the string starting with "aa" not starting with "|".&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2022 13:39:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-Help-to-find-first-instance-of-word-in-a-SAS-string-and/m-p/847041#M334889</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-11-30T13:39:04Z</dc:date>
    </item>
  </channel>
</rss>

