<?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: Retaining blanks within a string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retaining-blanks-within-a-string/m-p/787147#M251445</link>
    <description>By using strip(string) in the line "string=strip(string)||letter;" you strip all leading and trailing blanks. That does not happen in the code from the reply of FreelanceReinhard. You could also use something like "string = substr(string, 1, _N_) || letter;" in stead of the line mentioned above..</description>
    <pubDate>Wed, 22 Dec 2021 17:54:42 GMT</pubDate>
    <dc:creator>JosvanderVelden</dc:creator>
    <dc:date>2021-12-22T17:54:42Z</dc:date>
    <item>
      <title>Retaining blanks within a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-blanks-within-a-string/m-p/787141#M251440</link>
      <description>&lt;P&gt;As an exercise, I am trying to disassemble a sting into individual characters, then reassemble the characters back to the original string into a new SAS dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can successfully disassemble the sting with this code:&lt;/P&gt;
&lt;P&gt;data looky (drop=string);&lt;BR /&gt;input string $ 1-50;&lt;BR /&gt;do i=1 to length(string);&lt;BR /&gt;letter = substr(string, i, 1);&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;cards;&lt;BR /&gt;The quick brown fox jumped over the lazy dogs&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, when I attempt to reassemble the string, the blanks are removed.&amp;nbsp; I've tried using TRIM, STRIP, CAT, CATT and CATS and they all remove blanks:&lt;/P&gt;
&lt;P&gt;data what;&lt;BR /&gt;length string $ 50;&lt;BR /&gt;retain string;&lt;BR /&gt;set work.looky;&lt;BR /&gt;string=strip(string)||letter;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;STRIP (and the other functions) returns&amp;nbsp;Thequickbrownfoxjumpedoverthelazydogs&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This simple put statement write the complete string to the log, blanks included:&lt;/P&gt;
&lt;P&gt;data _null_;&lt;BR /&gt;set work.looky;&lt;BR /&gt;put @i letter @@;&amp;nbsp;run;&lt;/P&gt;
&lt;P&gt;The quick brown fox jumped over the lazy dogs&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am obviously misapplying the TRIM, STRIP CATT functions, but I am truly baffled as to what to use that will retain blanks.&amp;nbsp; Simply using string||letter returns a completely blank column.&amp;nbsp; What am I doing wrong?&lt;/P&gt;</description>
      <pubDate>Wed, 22 Dec 2021 17:23:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-blanks-within-a-string/m-p/787141#M251440</guid>
      <dc:creator>MarkFisher</dc:creator>
      <dc:date>2021-12-22T17:23:57Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining blanks within a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-blanks-within-a-string/m-p/787145#M251443</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/27039"&gt;@MarkFisher&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/27039"&gt;@MarkFisher&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;letter = substr(string, i, 1);&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You could turn around the above assignment statement to reassemble the characters:&lt;/P&gt;
&lt;PRE&gt;data want;
length string $ 50;
retain string;
set work.looky;
&lt;STRONG&gt;substr(string, i, 1) = letter;&lt;/STRONG&gt;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Dec 2021 17:47:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-blanks-within-a-string/m-p/787145#M251443</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-12-22T17:47:50Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining blanks within a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-blanks-within-a-string/m-p/787146#M251444</link>
      <description>&lt;P&gt;SAS stores character variables as fixed length. So when you define STRING as length $50 it contains 50 spaces.&amp;nbsp;When you set it to 'T' it now contains the letter T and 49 spaces.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to know where in those 50 characters you want to place the letter.&lt;/P&gt;
&lt;P&gt;You could just write directly to the i'th position.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;substr(string,i,1)=letter;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or extract the first (i-1) values and append the new letter.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;string=substrn(string,1,i-1)||letter;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Dec 2021 17:54:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-blanks-within-a-string/m-p/787146#M251444</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-22T17:54:34Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining blanks within a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-blanks-within-a-string/m-p/787147#M251445</link>
      <description>By using strip(string) in the line "string=strip(string)||letter;" you strip all leading and trailing blanks. That does not happen in the code from the reply of FreelanceReinhard. You could also use something like "string = substr(string, 1, _N_) || letter;" in stead of the line mentioned above..</description>
      <pubDate>Wed, 22 Dec 2021 17:54:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-blanks-within-a-string/m-p/787147#M251445</guid>
      <dc:creator>JosvanderVelden</dc:creator>
      <dc:date>2021-12-22T17:54:42Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining blanks within a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-blanks-within-a-string/m-p/787151#M251449</link>
      <description>&lt;P&gt;Goodness, I didn't expect the solution to be so simple!&amp;nbsp; I was obviously mis-applying the TRIM, STRIP and CAT functions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you all for your timely and thoughtful replies!&lt;/P&gt;</description>
      <pubDate>Wed, 22 Dec 2021 18:27:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-blanks-within-a-string/m-p/787151#M251449</guid>
      <dc:creator>MarkFisher</dc:creator>
      <dc:date>2021-12-22T18:27:02Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining blanks within a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-blanks-within-a-string/m-p/787152#M251450</link>
      <description>Oddly enough, this statement writes the string in reverse order, with blanks included:&lt;BR /&gt;string=trim(left(letter))||string;</description>
      <pubDate>Wed, 22 Dec 2021 18:30:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-blanks-within-a-string/m-p/787152#M251450</guid>
      <dc:creator>MarkFisher</dc:creator>
      <dc:date>2021-12-22T18:30:24Z</dc:date>
    </item>
  </channel>
</rss>

