<?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: Remove extra spaces and combine letters in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Remove-extra-spaces-and-combine-letters/m-p/751227#M236453</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 infile datalines truncover;
 input str $40.;
 datalines;
A b c d e  
A B Supply Inc
A B C Machines Inc
A B C D Operations Inc
A B C D E US Inc
;

data want;
  set have;
  length want_str $ 200;
  do i=1 to countw(str,' ');
    temp=scan(str,i,' ');
    if length(temp)=1 then want_str=cats(want_str,temp);
	 else want_str=catx(' ',want_str,temp);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 30 Jun 2021 13:24:40 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2021-06-30T13:24:40Z</dc:date>
    <item>
      <title>Remove extra spaces and combine letters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-extra-spaces-and-combine-letters/m-p/751065#M236364</link>
      <description>&lt;P&gt;Hello Everyone:&lt;/P&gt;
&lt;P&gt;I have a column with names, but there are extra spaces in the names. I provided a sample dataset below. My goal is to remove the space between single letters and combine them into one word. However, I do not want to merge the single-letters with multi-letter words. Is it possible to write a script such that if it will search in a string and stop where a multi-letter word first appears (for example, the word &lt;EM&gt;Supply&lt;/EM&gt; in the first case) and it will merge all the letters to the left of it? I would really appreciate your help with step.&lt;/P&gt;
&lt;P&gt;Thank you so much&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="290"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="145"&gt;&lt;STRONG&gt;Actual&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="145"&gt;&lt;STRONG&gt;Desired&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A B Supply Inc&lt;/TD&gt;
&lt;TD&gt;AB Supply Inc&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A B C Machines Inc&lt;/TD&gt;
&lt;TD&gt;ABC Machines Inc&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A B C D Operations Inc&lt;/TD&gt;
&lt;TD&gt;ABCD Operations Inc&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A B C D E US Inc&lt;/TD&gt;
&lt;TD&gt;ABCDE US Inc&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jun 2021 20:56:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-extra-spaces-and-combine-letters/m-p/751065#M236364</guid>
      <dc:creator>finans_sas</dc:creator>
      <dc:date>2021-06-29T20:56:41Z</dc:date>
    </item>
    <item>
      <title>Re: Remove extra spaces and combine letters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-extra-spaces-and-combine-letters/m-p/751085#M236376</link>
      <description>&lt;P&gt;This assumes all the singles letters come first as per your data.&lt;/P&gt;
&lt;P&gt;We find the first set of 2 consecutive letters and compress what comes before:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;POS    = prxmatch('/[a-z]{2}/i',STR); * The final i makes this match case-insensitive;
NEWSTR = compress(substr(STR,1,POS-2)) || ' ' || substr(STR,POS) ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Jun 2021 22:30:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-extra-spaces-and-combine-letters/m-p/751085#M236376</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-06-29T22:30:22Z</dc:date>
    </item>
    <item>
      <title>Re: Remove extra spaces and combine letters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-extra-spaces-and-combine-letters/m-p/751135#M236413</link>
      <description>&lt;P&gt;It should be possible to use a single regex with prxchange to get the required result, unfortunately those expressions need the "g" switch which is not supported. See &lt;A href="https://stackoverflow.com/questions/4228750/removing-spaces-between-single-letters" target="_blank"&gt;https://stackoverflow.com/questions/4228750/removing-spaces-between-single-letters&lt;/A&gt;. Maybe someone else, with more regex experience can fix the problem. Using -1 in prxchange doesn't help because on subsequent iterations the first conditions is not fulfilled any more due to concatenations that took place.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jun 2021 06:24:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-extra-spaces-and-combine-letters/m-p/751135#M236413</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-06-30T06:24:38Z</dc:date>
    </item>
    <item>
      <title>Re: Remove extra spaces and combine letters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-extra-spaces-and-combine-letters/m-p/751194#M236438</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp; Not that I believe the data the OP posted are sufficiently representative but for what you mention and the sample data provided a look-ahead could do the job.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 infile datalines truncover;
 input str $40.;
 datalines;
A b c d e  
A B Supply Inc
A B C Machines Inc
A B C D Operations Inc
A B C D E US Inc
;

data want;
  set have;
  str=prxchange('s/(\w)\s+(?=\w(\s|$))/$1/oi',-1,strip(str));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jul 2021 10:10:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-extra-spaces-and-combine-letters/m-p/751194#M236438</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-07-01T10:10:05Z</dc:date>
    </item>
    <item>
      <title>Re: Remove extra spaces and combine letters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-extra-spaces-and-combine-letters/m-p/751227#M236453</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 infile datalines truncover;
 input str $40.;
 datalines;
A b c d e  
A B Supply Inc
A B C Machines Inc
A B C D Operations Inc
A B C D E US Inc
;

data want;
  set have;
  length want_str $ 200;
  do i=1 to countw(str,' ');
    temp=scan(str,i,' ');
    if length(temp)=1 then want_str=cats(want_str,temp);
	 else want_str=catx(' ',want_str,temp);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Jun 2021 13:24:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-extra-spaces-and-combine-letters/m-p/751227#M236453</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-06-30T13:24:40Z</dc:date>
    </item>
    <item>
      <title>Re: Remove extra spaces and combine letters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-extra-spaces-and-combine-letters/m-p/751338#M236503</link>
      <description>&lt;P&gt;Thank you so much, ChrisNZ for this code! I really appreciate it. When I used it, it was able to combine the letters the way I wanted, but it produces errors when I have names that do not need to be modified. For example, it converts "ABC Supply" to "ABCSupply ABC Supply" whereas the desired outcome here is the same as the actual outcome (ABC Supply). Is there a way to remedy this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;On a separate note, sometimes I have single letters in different parts of the names such as ABC U S Supply, which should be changed to ABC US Supply. However, there are not many of those.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much!&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jun 2021 18:45:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-extra-spaces-and-combine-letters/m-p/751338#M236503</guid>
      <dc:creator>finans_sas</dc:creator>
      <dc:date>2021-06-30T18:45:31Z</dc:date>
    </item>
    <item>
      <title>Re: Remove extra spaces and combine letters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-extra-spaces-and-combine-letters/m-p/751341#M236506</link>
      <description>&lt;P&gt;Thank you so much for your help, Patrick! I will try your suggestion.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jun 2021 18:52:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-extra-spaces-and-combine-letters/m-p/751341#M236506</guid>
      <dc:creator>finans_sas</dc:creator>
      <dc:date>2021-06-30T18:52:21Z</dc:date>
    </item>
    <item>
      <title>Re: Remove extra spaces and combine letters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-extra-spaces-and-combine-letters/m-p/751342#M236507</link>
      <description>&lt;P&gt;Thank you so much for your help, Ksharp! I will try your suggestion.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jun 2021 18:53:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-extra-spaces-and-combine-letters/m-p/751342#M236507</guid>
      <dc:creator>finans_sas</dc:creator>
      <dc:date>2021-06-30T18:53:22Z</dc:date>
    </item>
  </channel>
</rss>

