<?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 can I add/remove  comma(s) or blank(s) between words in a column value in SAS 9.4? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-add-remove-comma-s-or-blank-s-between-words-in-a/m-p/847682#M335132</link>
    <description>&lt;P&gt;Yes, that's true! But there are situations where we read data from various sources and they are not quite clean or formatted like this. The above is for example only to make the issue clearer.&lt;/P&gt;</description>
    <pubDate>Sun, 04 Dec 2022 22:48:14 GMT</pubDate>
    <dc:creator>inquistive</dc:creator>
    <dc:date>2022-12-04T22:48:14Z</dc:date>
    <item>
      <title>How can I add/remove  comma(s) or blank(s) between words in a column value in SAS 9.4?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-add-remove-comma-s-or-blank-s-between-words-in-a/m-p/847679#M335130</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data employees;
length emp_id $2. name $5.  emp_type $4.  emp_stat $8.;
   input emp_id name  emp_type emp_stat ;
datalines;
01 Sandy sea contrct
02 Dandy fte reg
03 Candy sea pttime
04 Mandy fte reg
;

data category;
length emp_cat1 $12  
       emp_cat2 $12;
       retain emp_id
              name;
set employees;
 emp_cat1= catx(",", emp_type, emp_stat);
 emp_cat2= catx(" ", emp_type, emp_stat);
 drop emp_type emp_stat;
 run;
 
 data emp_tmp;
 length emp_stat1 $12  
        emp_stat2 $12;
 retain emp_id
        name;
 set category;
 emp_stat1= compbl(tranwrd(emp_cat1, ",", ""));
 emp_stat2= compbl(tranwrd(emp_cat2, "", ","));
 run;
  
 data emp;
 length comma $12  
        blank $12;
        retain emp_id
               name
               ;
 set emp_tmp;
 comma= compbl(tranwrd(emp_stat1,"","," ));
 blank= compbl(tranwrd(emp_stat2, ",",""));
 drop emp_stat1 emp_stat2;
run;
 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The last step in the above code creates following table EMP:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="inquistive_2-1670192929118.png" style="width: 673px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78005i7F5A1F21693F40F6/image-dimensions/673x239?v=v2" width="673" height="239" role="button" title="inquistive_2-1670192929118.png" alt="inquistive_2-1670192929118.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The addition of blank (' ') between strings has returned a desired output.&amp;nbsp; But the addition of comma(,) between strings has returned so many undesired commas. Main question/concern is how to remove the&amp;nbsp; UNNECESSARY commas (especially at the end)in the data/records returned under the COMMA column? I can reduce the length of variable to 11 char to get rid of a comma (in the first row: sea, contrct,) but that won't solve the issues with the other rows that have shorter strings. There will still be unnecessary commas.&lt;/P&gt;
&lt;P&gt;Any help would be greatly appreciated.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 04 Dec 2022 22:45:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-add-remove-comma-s-or-blank-s-between-words-in-a/m-p/847679#M335130</guid>
      <dc:creator>inquistive</dc:creator>
      <dc:date>2022-12-04T22:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: How can I add/remove  comma(s) or blank(s) between words in a column value in SAS 9.4?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-add-remove-comma-s-or-blank-s-between-words-in-a/m-p/847681#M335131</link>
      <description>&lt;P&gt;Seems to me that when you create data set CATEGORY that you get what you want&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PaigeMiller_0-1670193865303.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78006i27F47BBFE60F858C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PaigeMiller_0-1670193865303.png" alt="PaigeMiller_0-1670193865303.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;so I don't understand why additional processing is necessary here, it seems to me that the solution is to stop the program at this point.&lt;/P&gt;</description>
      <pubDate>Sun, 04 Dec 2022 22:44:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-add-remove-comma-s-or-blank-s-between-words-in-a/m-p/847681#M335131</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-12-04T22:44:53Z</dc:date>
    </item>
    <item>
      <title>Re: How can I add/remove  comma(s) or blank(s) between words in a column value in SAS 9.4?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-add-remove-comma-s-or-blank-s-between-words-in-a/m-p/847682#M335132</link>
      <description>&lt;P&gt;Yes, that's true! But there are situations where we read data from various sources and they are not quite clean or formatted like this. The above is for example only to make the issue clearer.&lt;/P&gt;</description>
      <pubDate>Sun, 04 Dec 2022 22:48:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-add-remove-comma-s-or-blank-s-between-words-in-a/m-p/847682#M335132</guid>
      <dc:creator>inquistive</dc:creator>
      <dc:date>2022-12-04T22:48:14Z</dc:date>
    </item>
    <item>
      <title>Re: How can I add/remove  comma(s) or blank(s) between words in a column value in SAS 9.4?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-add-remove-comma-s-or-blank-s-between-words-in-a/m-p/847684#M335134</link>
      <description>&lt;P&gt;So this statements is confusing because of the way you left out the space inside of the string literal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;comma= compbl(tranwrd(emp_stat1,"",","));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You are running this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;comma= compbl(tranwrd(emp_stat1," ",","));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the TRANWRD() function will converting all of the spaces into commas (including any trailing spaces used to fill up the fixed length string EPM_STAT1).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So a value like &lt;FONT face="courier new,courier"&gt;"A B&amp;nbsp; &amp;nbsp;C&amp;nbsp; &amp;nbsp; "&lt;/FONT&gt; would become&amp;nbsp;&lt;FONT face="courier new,courier"&gt;"A,B,,,C,,,,"&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Then the COMPBL() function as nothing to operate on since there cannot be any spaces left.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The other statement makes more sense:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;blank= compbl(tranwrd(emp_stat2,","," "));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the TRANWRD() converts all of the commas into spaces.&amp;nbsp; Then the COMPBL() collapses multiple spaces into one.&lt;/P&gt;
&lt;P&gt;So a value like&amp;nbsp; &lt;FONT face="courier new,courier"&gt;"A,B,,,C,,,,"&amp;nbsp;&lt;/FONT&gt;would become&amp;nbsp;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;"A B C"&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not exactly sure what your goal is.&amp;nbsp; Do you want to convert a value like &lt;FONT face="courier new,courier"&gt;"a,,,b,,,,c"&lt;/FONT&gt; into &lt;FONT face="courier new,courier"&gt;"a,b,c"&lt;/FONT&gt; ?&lt;/P&gt;
&lt;P&gt;If so then use something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;comma= translate(compbl(translate(strip(emp_stat1),' ,',', ')),' ,',', ');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So remove the leading and trailing spaces.&amp;nbsp; Swap the commas and spaces,&amp;nbsp; compress the multiple spaces (which were commas), and the swap the commas and spaces again.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 04 Dec 2022 23:05:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-add-remove-comma-s-or-blank-s-between-words-in-a/m-p/847684#M335134</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-12-04T23:05:40Z</dc:date>
    </item>
    <item>
      <title>Re: How can I add/remove  comma(s) or blank(s) between words in a column value in SAS 9.4?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-add-remove-comma-s-or-blank-s-between-words-in-a/m-p/847686#M335136</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83652"&gt;@inquistive&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Yes, that's true! But there are situations where we read data from various sources and they are not quite clean or formatted like this. The above is for example only to make the issue clearer.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I'm not following. You can fix poorly formatted data, and then your placement of commas is simple.&lt;/P&gt;</description>
      <pubDate>Sun, 04 Dec 2022 23:23:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-add-remove-comma-s-or-blank-s-between-words-in-a/m-p/847686#M335136</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-12-04T23:23:17Z</dc:date>
    </item>
    <item>
      <title>Re: How can I add/remove  comma(s) or blank(s) between words in a column value in SAS 9.4?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-add-remove-comma-s-or-blank-s-between-words-in-a/m-p/847694#M335141</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;. You provided me with what I was looking for.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, I messed up (unintentionally&lt;span class="lia-unicode-emoji" title=":pensive_face:"&gt;😔&lt;/span&gt;) with the extra blank space:&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;comma= compbl(tranwrd(emp_stat1," ",","));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Dec 2022 00:15:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-add-remove-comma-s-or-blank-s-between-words-in-a/m-p/847694#M335141</guid>
      <dc:creator>inquistive</dc:creator>
      <dc:date>2022-12-05T00:15:09Z</dc:date>
    </item>
    <item>
      <title>Re: How can I add/remove  comma(s) or blank(s) between words in a column value in SAS 9.4?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-add-remove-comma-s-or-blank-s-between-words-in-a/m-p/847695#M335142</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83652"&gt;@inquistive&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;. You provided me with what I was looking for.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, I messed up (unintentionally&lt;span class="lia-unicode-emoji" title=":pensive_face:"&gt;😔&lt;/span&gt;) with the extra blank space:&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;comma= compbl(tranwrd(emp_stat1," ",","));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That looks wrong.&amp;nbsp; That is what you had before.&lt;/P&gt;
&lt;P&gt;Did you mean to call the functions in the opposite order?&amp;nbsp; So compress multiple blanks to one and then convert them to commas?&lt;/P&gt;</description>
      <pubDate>Mon, 05 Dec 2022 00:17:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-add-remove-comma-s-or-blank-s-between-words-in-a/m-p/847695#M335142</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-12-05T00:17:19Z</dc:date>
    </item>
    <item>
      <title>Re: How can I add/remove  comma(s) or blank(s) between words in a column value in SAS 9.4?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-add-remove-comma-s-or-blank-s-between-words-in-a/m-p/847696#M335143</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;Yes, I copied and pasted the code where I made a mistake (that you pointed out in your response). I was referring to the place where the problem lies in my code.&lt;/P&gt;
&lt;P&gt;The code you provided is the perfect solution&lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;.&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Mon, 05 Dec 2022 00:21:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-add-remove-comma-s-or-blank-s-between-words-in-a/m-p/847696#M335143</guid>
      <dc:creator>inquistive</dc:creator>
      <dc:date>2022-12-05T00:21:09Z</dc:date>
    </item>
  </channel>
</rss>

