<?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: Replace last word in a string conditionally in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Replace-last-word-in-a-string-conditionally/m-p/715243#M220917</link>
    <description>&lt;P&gt;Just for some fun .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example;
   length x $ 100;
   x="ABC INVESTMENT TR CO TR";
   y=prxchange('s/\btr$/TRUST/i',1,strip(x));
   output;

      x="ABC WORLD CO TRANSPORTATION";
   y=prxchange('s/\btr$/TRUST/i',1,strip(x));
   output;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 29 Jan 2021 12:40:36 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2021-01-29T12:40:36Z</dc:date>
    <item>
      <title>Replace last word in a string conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-last-word-in-a-string-conditionally/m-p/715047#M220827</link>
      <description>&lt;P&gt;Hi Everyone:&lt;/P&gt;
&lt;P&gt;I have a data set that looks like this:&lt;/P&gt;
&lt;P&gt;ABC INVESTMENT TR CO TR&lt;/P&gt;
&lt;P&gt;ABC WORLD CO TRANSPORTATION&lt;/P&gt;
&lt;P&gt;ABC SKYE INVESTMENT TR&lt;/P&gt;
&lt;P&gt;ABC CLOUD PROPERTY TR&lt;/P&gt;
&lt;P&gt;ABC MOVIES PRODUCTION TRUSTME&lt;/P&gt;
&lt;P&gt;TRUST YOURSELF TR TRUE TRACTOR&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Strings vary in length. My goal is to replace "TR" at the end of the string with "TRUST". I want to do this if and only if the last word is TR. Ideally, the data will look like this:&lt;/P&gt;
&lt;P&gt;ABC INVESTMENT TR CO TR&lt;STRONG&gt;UST&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;ABC WORLD CO TRANSPORTATION&lt;/P&gt;
&lt;P&gt;ABC SKYE INVESTMENT TR&lt;STRONG&gt;UST&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;ABC CLOUD PROPERTY TR&lt;STRONG&gt;UST&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;ABC MOVIES PRODUCTION TRUSTME&lt;/P&gt;
&lt;P&gt;TRUST YOURSELF TR TRUE TRACTOR&lt;/P&gt;
&lt;P&gt;I used tranwrd successfully to replace other parts of the strings, but got stuck on the last one. Could you help me with this? I am using SAS 9.4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jan 2021 16:16:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-last-word-in-a-string-conditionally/m-p/715047#M220827</guid>
      <dc:creator>finans_sas</dc:creator>
      <dc:date>2021-01-28T16:16:22Z</dc:date>
    </item>
    <item>
      <title>Re: Replace last word in a string conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-last-word-in-a-string-conditionally/m-p/715050#M220830</link>
      <description>&lt;P&gt;One way that uses the fact this is the LAST "word".&lt;/P&gt;
&lt;PRE&gt;data example;
   length x $ 100;
   x="ABC INVESTMENT TR CO TR";
   if scan(x,countw(x))='TR' then x=cats(x,'UST');
run;&lt;/PRE&gt;
&lt;P&gt;Potential issue. If your current variable length is such that adding 3 characters would exceed the length of the variable then the value would be truncated.&lt;/P&gt;
&lt;PRE&gt;data example;
   length x $ 25;
   x="ABC INVESTMENT TR CO TR";
   if scan(x,countw(x))='TR' then x=cats(x,'UST');
run;&lt;/PRE&gt;
&lt;P&gt;So you may have to create a new variable with a longer defined length.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jan 2021 16:28:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-last-word-in-a-string-conditionally/m-p/715050#M220830</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-01-28T16:28:05Z</dc:date>
    </item>
    <item>
      <title>Re: Replace last word in a string conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-last-word-in-a-string-conditionally/m-p/715052#M220831</link>
      <description>&lt;P&gt;Just changing it at the end of the lines make it easy.&amp;nbsp; You can just append something to the end of the string and to the end of the string you are asking TRANWRD() to change.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  if scan(string,-1,' ')='TR' then do;
    string=tranwrd(cats(string,'^%|'),'TR^%|','TRUST');
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Otherwise learn how to use the regular expression functions.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jan 2021 16:31:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-last-word-in-a-string-conditionally/m-p/715052#M220831</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-28T16:31:52Z</dc:date>
    </item>
    <item>
      <title>Re: Replace last word in a string conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-last-word-in-a-string-conditionally/m-p/715066#M220839</link>
      <description>Thank you so much, Tom!</description>
      <pubDate>Thu, 28 Jan 2021 17:15:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-last-word-in-a-string-conditionally/m-p/715066#M220839</guid>
      <dc:creator>finans_sas</dc:creator>
      <dc:date>2021-01-28T17:15:35Z</dc:date>
    </item>
    <item>
      <title>Re: Replace last word in a string conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-last-word-in-a-string-conditionally/m-p/715067#M220840</link>
      <description>Thank you so much, ballardw!</description>
      <pubDate>Thu, 28 Jan 2021 17:16:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-last-word-in-a-string-conditionally/m-p/715067#M220840</guid>
      <dc:creator>finans_sas</dc:creator>
      <dc:date>2021-01-28T17:16:17Z</dc:date>
    </item>
    <item>
      <title>Re: Replace last word in a string conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-last-word-in-a-string-conditionally/m-p/715243#M220917</link>
      <description>&lt;P&gt;Just for some fun .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example;
   length x $ 100;
   x="ABC INVESTMENT TR CO TR";
   y=prxchange('s/\btr$/TRUST/i',1,strip(x));
   output;

      x="ABC WORLD CO TRANSPORTATION";
   y=prxchange('s/\btr$/TRUST/i',1,strip(x));
   output;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Jan 2021 12:40:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-last-word-in-a-string-conditionally/m-p/715243#M220917</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-01-29T12:40:36Z</dc:date>
    </item>
  </channel>
</rss>

