<?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: How to compress around a comma inside a string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-compress-around-a-comma-inside-a-string/m-p/887886#M350787</link>
    <description>&lt;P&gt;The example data works fine with just TRANWRD() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;want = tranwrd(have,' , ',',');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the real data has multiple adjacent spaces you can try using COMPBL() first to collapse all places with multiple spaces into a single space.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the real data has some commas with only leading or only trailing spaces then you might need to work a little harder.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input string $50.;
cards;
Mary Had , A Little , Lamb
Mary Had  ,  A Little  ,  Lamb
Mary Had, A Little ,Lamb
Mary Had  ,A Little,  Lamb
; 

data want;
  set have;
  length newstr $50;
  newstr=tranwrd(tranwrd(compbl(string),', ',','),' ,',',');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs                string                        newstr

 1     Mary Had , A Little , Lamb        Mary Had,A Little,Lamb
 2     Mary Had  ,  A Little  ,  Lamb    Mary Had,A Little,Lamb
 3     Mary Had, A Little ,Lamb          Mary Had,A Little,Lamb
 4     Mary Had  ,A Little,  Lamb        Mary Had,A Little,Lamb
&lt;/PRE&gt;</description>
    <pubDate>Fri, 04 Aug 2023 14:32:00 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-08-04T14:32:00Z</dc:date>
    <item>
      <title>How to compress around a comma inside a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compress-around-a-comma-inside-a-string/m-p/887851#M350766</link>
      <description>&lt;P&gt;Hi.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to deleted spaces around commas inside a singe character string:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have&lt;/P&gt;
&lt;TABLE width="200px"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="199px"&gt;Mary Had , A Little , Lamb&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Want&lt;/P&gt;
&lt;TABLE width="193px"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="192px"&gt;Mary Had,A Little,Lamb&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help is truly appreciated!&lt;/P&gt;</description>
      <pubDate>Fri, 04 Aug 2023 12:41:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compress-around-a-comma-inside-a-string/m-p/887851#M350766</guid>
      <dc:creator>arde</dc:creator>
      <dc:date>2023-08-04T12:41:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to compress around a comma inside a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compress-around-a-comma-inside-a-string/m-p/887861#M350774</link>
      <description>&lt;P&gt;I would use PRXCHANGE, but I'm sure there's a different function that could do it as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
var = "Mary Had, A Little,Lamb";
var_want = prxchange("s/(\s\,)|(\,\s)/,/", -1, var);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Basically saying, substitute a space followed by a comma OR a comma followed by a space, replace it with a single comma. -1 tells SAS to do the replacement for the entire field.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not sure if this will apply to all cases, though - especially if you have multiple spaces. Then you will need to modify the \s.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Aug 2023 13:14:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compress-around-a-comma-inside-a-string/m-p/887861#M350774</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2023-08-04T13:14:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to compress around a comma inside a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compress-around-a-comma-inside-a-string/m-p/887882#M350784</link>
      <description>Thank you!</description>
      <pubDate>Fri, 04 Aug 2023 14:25:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compress-around-a-comma-inside-a-string/m-p/887882#M350784</guid>
      <dc:creator>arde</dc:creator>
      <dc:date>2023-08-04T14:25:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to compress around a comma inside a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compress-around-a-comma-inside-a-string/m-p/887886#M350787</link>
      <description>&lt;P&gt;The example data works fine with just TRANWRD() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;want = tranwrd(have,' , ',',');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the real data has multiple adjacent spaces you can try using COMPBL() first to collapse all places with multiple spaces into a single space.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the real data has some commas with only leading or only trailing spaces then you might need to work a little harder.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input string $50.;
cards;
Mary Had , A Little , Lamb
Mary Had  ,  A Little  ,  Lamb
Mary Had, A Little ,Lamb
Mary Had  ,A Little,  Lamb
; 

data want;
  set have;
  length newstr $50;
  newstr=tranwrd(tranwrd(compbl(string),', ',','),' ,',',');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs                string                        newstr

 1     Mary Had , A Little , Lamb        Mary Had,A Little,Lamb
 2     Mary Had  ,  A Little  ,  Lamb    Mary Had,A Little,Lamb
 3     Mary Had, A Little ,Lamb          Mary Had,A Little,Lamb
 4     Mary Had  ,A Little,  Lamb        Mary Had,A Little,Lamb
&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Aug 2023 14:32:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compress-around-a-comma-inside-a-string/m-p/887886#M350787</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-04T14:32:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to compress around a comma inside a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compress-around-a-comma-inside-a-string/m-p/887898#M350795</link>
      <description>that's good to know, thank you</description>
      <pubDate>Fri, 04 Aug 2023 15:17:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compress-around-a-comma-inside-a-string/m-p/887898#M350795</guid>
      <dc:creator>arde</dc:creator>
      <dc:date>2023-08-04T15:17:28Z</dc:date>
    </item>
  </channel>
</rss>

