<?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: only replacing exact string among slight variations? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/only-replacing-exact-string-among-slight-variations/m-p/935390#M367769</link>
    <description>&lt;P&gt;If you want to use tranwrd() the include the spaces.&amp;nbsp; You will have to ensure there is both a leading and trailing space in the string to make sure you catch words at the start or end of the source string.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  y='ic icc 1icc 1ic';
  length y2 $40 ;
  y2=strip(tranwrd(cat(' ',y, ' '), ' ic ', ' 1icc '));
  y2=strip(tranwrd(cat(' ',y2,' '), ' icc ', ' 1icc '));
  y2=strip(tranwrd(cat(' ',y2,' '), ' 1ic ', ' 1icc '));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs           y                   y2

 1     ic icc 1icc 1ic    1icc 1icc 1icc 1icc&lt;/PRE&gt;</description>
    <pubDate>Wed, 10 Jul 2024 19:03:49 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-07-10T19:03:49Z</dc:date>
    <item>
      <title>only replacing exact string among slight variations?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/only-replacing-exact-string-among-slight-variations/m-p/935355#M367751</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample2;
 y='icc 1icc ic'; run;

 data sample3; set sample2;
 y2=tranwrd(compress(y, ' '), 'ic', '1icc');
y3=tranwrd(compress(y2, ' '), '1ic', '1icc');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I need to correct the icc -&amp;gt; 1icc&lt;/P&gt;
&lt;P&gt;and ic -&amp;gt; 1icc.&amp;nbsp; The 1icc is correct as is, but if I try to use multiple steps of tranwrd to correct the slight variations, I get&amp;nbsp;1icccc11icccc1iccc as y3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way to identify and correct the specific iterations only?&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2024 15:59:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/only-replacing-exact-string-among-slight-variations/m-p/935355#M367751</guid>
      <dc:creator>appleorange</dc:creator>
      <dc:date>2024-07-10T15:59:58Z</dc:date>
    </item>
    <item>
      <title>Re: only replacing exact string among slight variations?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/only-replacing-exact-string-among-slight-variations/m-p/935360#M367755</link>
      <description>&lt;P&gt;What do you want your final output y3 to look like?&lt;/P&gt;
&lt;P&gt;Do you want it to be '1icc 1icc 1icc'?&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2024 16:30:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/only-replacing-exact-string-among-slight-variations/m-p/935360#M367755</guid>
      <dc:creator>procphysics</dc:creator>
      <dc:date>2024-07-10T16:30:41Z</dc:date>
    </item>
    <item>
      <title>Re: only replacing exact string among slight variations?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/only-replacing-exact-string-among-slight-variations/m-p/935361#M367756</link>
      <description>Yes, correct.</description>
      <pubDate>Wed, 10 Jul 2024 16:32:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/only-replacing-exact-string-among-slight-variations/m-p/935361#M367756</guid>
      <dc:creator>appleorange</dc:creator>
      <dc:date>2024-07-10T16:32:41Z</dc:date>
    </item>
    <item>
      <title>Re: only replacing exact string among slight variations?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/only-replacing-exact-string-among-slight-variations/m-p/935367#M367760</link>
      <description>&lt;P&gt;The tranwrd function replaces all instances of a character pattern, so when you do&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;y2=tranwrd(compress(y, ' '), 'ic', '1icc');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;all instances of 'ic' in the column are replaced with '1icc'.&lt;/P&gt;
&lt;P&gt;Also, you are removing the spaces from y so SAS does the tranwrd function on 'icc1iccic'.&lt;/P&gt;
&lt;P&gt;y='&lt;STRONG&gt;ic&lt;/STRONG&gt;c 1&lt;STRONG&gt;ic&lt;/STRONG&gt;c &lt;STRONG&gt;ic&lt;/STRONG&gt;' becomes y2='&lt;STRONG&gt;1icc&lt;/STRONG&gt;c1&lt;STRONG&gt;1icc&lt;/STRONG&gt;c&lt;STRONG&gt;1icc&lt;/STRONG&gt;'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want your final string to be '1icc 1icc 1icc' then you can do that by breaking up y into each word, then replacing the first and third words.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample2;
    y='icc 1icc ic';

    firstspace = find(y,' ');
    secondspace = find(y,' ',firstspace+1);

    firstword=substr(y,1,firstspace-1);
    secondword=substr(y,firstspace+1,secondspace-firstspace-1);
    thirdword=substr(y,secondspace+1);

    if firstword='icc' then firstword='1icc';
    if thirdword='ic' then thirdword='1icc';

    y2 = catx(' ',firstword,secondword,thirdword);

 run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First use the find function to find the location of the first and second spaces in y. The secondspace looks for the next space after the firstspace.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; firstspace = find(y,' ');
 secondspace = find(y,' ',firstspace+1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then use the substr function to extract each word using spaces as delimiters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    firstword=substr(y,1,firstspace-1);
    secondword=substr(y,firstspace+1,secondspace-firstspace-1);
    thirdword=substr(y,secondspace+1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;firstword starts at position 1 and ends right before the first space.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;secondword is between the first and second spaces.&lt;/P&gt;
&lt;P&gt;thirdword starts right after the second space.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now replace the words 'icc' and 'ic' with '1icc'.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   if firstword='icc' then firstword='1icc';
   if thirdword='ic' then thirdword='1icc';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Your new string looks like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;y2 = catx(' ',firstword,secondword,thirdword);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2024 16:53:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/only-replacing-exact-string-among-slight-variations/m-p/935367#M367760</guid>
      <dc:creator>procphysics</dc:creator>
      <dc:date>2024-07-10T16:53:23Z</dc:date>
    </item>
    <item>
      <title>Re: only replacing exact string among slight variations?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/only-replacing-exact-string-among-slight-variations/m-p/935390#M367769</link>
      <description>&lt;P&gt;If you want to use tranwrd() the include the spaces.&amp;nbsp; You will have to ensure there is both a leading and trailing space in the string to make sure you catch words at the start or end of the source string.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  y='ic icc 1icc 1ic';
  length y2 $40 ;
  y2=strip(tranwrd(cat(' ',y, ' '), ' ic ', ' 1icc '));
  y2=strip(tranwrd(cat(' ',y2,' '), ' icc ', ' 1icc '));
  y2=strip(tranwrd(cat(' ',y2,' '), ' 1ic ', ' 1icc '));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs           y                   y2

 1     ic icc 1icc 1ic    1icc 1icc 1icc 1icc&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Jul 2024 19:03:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/only-replacing-exact-string-among-slight-variations/m-p/935390#M367769</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-07-10T19:03:49Z</dc:date>
    </item>
    <item>
      <title>Re: only replacing exact string among slight variations?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/only-replacing-exact-string-among-slight-variations/m-p/935405#M367772</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  y='ic icc 1icc 1ic';
  length y2 $40 ;
  y2=prxchange('s/\b(ic|icc)\b/1icc/i',-1,y);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Jul 2024 01:23:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/only-replacing-exact-string-among-slight-variations/m-p/935405#M367772</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-07-11T01:23:07Z</dc:date>
    </item>
  </channel>
</rss>

