<?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: Insert a String after the  fourth &amp;quot;space&amp;quot; delimiter in  string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/891768#M352262</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  text = " I am happy  to  be part of this group";
  output;
  text = "This SAS Group is Amazing";
  output;
  text = " Thank you";
  output;
 run;

data want (drop=_:);
  set have;
  text=left(compbl(text));
  length newtext $40;
  if countw(text) &amp;gt; 4 then do;
    _four_word_length=length(catx(' ',scan(text,1),scan(text,2),scan(text,3),scan(text,4)))+1;
    newtext=cats(substr(text,1,_four_word_length),'[ESC]',substr(text,_four_word_length+1));
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 30 Aug 2023 18:00:33 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2023-08-30T18:00:33Z</dc:date>
    <item>
      <title>Insert a String after the  fourth "space" delimiter in  string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/891578#M352206</link>
      <description>&lt;P&gt;I have the following scenario:&lt;/P&gt;
&lt;P&gt;I have to find if the string "text" has more than 4 spaces then I want to replace the fourth "space" with the string "[ESC]". Otherwise, we don't insert any strings.&amp;nbsp; If replacing is complicated, I am okay with inserting a string after the fourth space. Thank you for your help.&lt;/P&gt;
&lt;P&gt;Example: "I am happy to&lt;STRONG&gt;[ESC]&lt;/STRONG&gt;be part of this group"&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data have;
  text = "I am happy to be part of this group";
  output;
  text = "This SAS Group is Amazing";
  output;
  text = " Thank you";
  output;

 run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Aug 2023 19:00:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/891578#M352206</guid>
      <dc:creator>SASuserlot</dc:creator>
      <dc:date>2023-08-29T19:00:12Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a String after the  fourth "space" delimiter in  string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/891582#M352207</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
    set have;
    fourth_word=scan(text,4,' ');
    location_of_fourth_word=findw(text,cats(' ',fourth_word));
    length_of_fourth_word=length(fourth_word);
    if not missing(fourth_word) then 
        replace_text=cats(substr(text,1,location_of_fourth_word+length_of_fourth_word-1),'[ESC]',
        substr(text,location_of_fourth_word+length_of_Fourth_word));
    else replace_text=text;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Aug 2023 19:29:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/891582#M352207</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-29T19:29:57Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a String after the  fourth "space" delimiter in  string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/891583#M352208</link>
      <description>&lt;P&gt;Can you show the code you have tried?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This blog from&amp;nbsp;leonid Batkhan might help:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/sgf/2019/06/26/finding-n-th-instance-of-a-substring-within-a-string/" target="_blank"&gt;https://blogs.sas.com/content/sgf/2019/06/26/finding-n-th-instance-of-a-substring-within-a-string/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another approach would be to use the CALL SCAN routine, which can return the position of the 5th word.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0ecxfx00bn8i4n1vhh8up24ha6x.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0ecxfx00bn8i4n1vhh8up24ha6x.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2023 19:25:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/891583#M352208</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-08-29T19:25:00Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a String after the  fourth "space" delimiter in  string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/891681#M352242</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data have;
  text = "I am happy to be part of this group";
  output;
  text = "This SAS Group is Amazing";
  output;
  text = " Thank you";
  output;
 run;

 data want;
  set have;
  want=prxchange('s/(^\S+\s+\S+\s+\S+\s+\S+)\s+/\1[ESC]/',1,left(text));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Aug 2023 11:53:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/891681#M352242</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-08-30T11:53:26Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a String after the  fourth "space" delimiter in  string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/891750#M352253</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/350312"&gt;@SASuserlot&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is using a prxchange similar to the one provided by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;, but with an added check on the number of words&amp;nbsp; in the string. This is to avoid insert of {ESC] at the end of the string, if there are exactly 4 words.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  text = "I am happy to be part of this group";
  output;
  text = "This SAS Group is      Amazing";
  output;
  text = "Thank you very much";
  output;
  text = "Thanks for everything";
  output;
  text = " Thank you";
  output;
run;

data want;
  length text $200;
  set have; 
  if countw(text,' ') &amp;gt; 4 then
    text = prxchange('s/(\S+\s+\S+\s+\S+\s+\S+)(\s*)(.*)/$1[ESC]$3/',1,catt(text));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Aug 2023 17:01:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/891750#M352253</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2023-08-30T17:01:57Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a String after the  fourth "space" delimiter in  string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/891768#M352262</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  text = " I am happy  to  be part of this group";
  output;
  text = "This SAS Group is Amazing";
  output;
  text = " Thank you";
  output;
 run;

data want (drop=_:);
  set have;
  text=left(compbl(text));
  length newtext $40;
  if countw(text) &amp;gt; 4 then do;
    _four_word_length=length(catx(' ',scan(text,1),scan(text,2),scan(text,3),scan(text,4)))+1;
    newtext=cats(substr(text,1,_four_word_length),'[ESC]',substr(text,_four_word_length+1));
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Aug 2023 18:00:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/891768#M352262</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-08-30T18:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a String after the  fourth "space" delimiter in  string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/892025#M352368</link>
      <description>&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2023 17:59:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/892025#M352368</guid>
      <dc:creator>SASuserlot</dc:creator>
      <dc:date>2023-08-31T17:59:06Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a String after the  fourth "space" delimiter in  string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/892027#M352369</link>
      <description>&lt;P&gt;Thank you&amp;nbsp; for your time&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;. That's awesome which can be done in one step &lt;span class="lia-unicode-emoji" title=":face_with_open_mouth:"&gt;😮&lt;/span&gt;. what's the 'S+' and 's+' signifies in the prxchange ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;. All of you guys are amazing.&amp;nbsp; unfortunately I have to chose one as Answer&lt;span class="lia-unicode-emoji" title=":expressionless_face:"&gt;😑&lt;/span&gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2023 18:03:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/892027#M352369</guid>
      <dc:creator>SASuserlot</dc:creator>
      <dc:date>2023-08-31T18:03:17Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a String after the  fourth "space" delimiter in  string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/892248#M352422</link>
      <description>'S+'  means one or more non-space characters .&lt;BR /&gt;and 's+'  mean one or more space characters .</description>
      <pubDate>Fri, 01 Sep 2023 11:44:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-String-after-the-fourth-quot-space-quot-delimiter-in/m-p/892248#M352422</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-09-01T11:44:32Z</dc:date>
    </item>
  </channel>
</rss>

