<?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: sas add substring to an existing string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304233#M64749</link>
    <description>&lt;P&gt;You can do it directly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;string= trim(string) || '***';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're running into trouble because STRING is already defined with a fixed length. &amp;nbsp;If you just concatenate without adding the TRIM function, the trailing blanks remain in place and there isn't room to add any more characters.&lt;/P&gt;</description>
    <pubDate>Thu, 13 Oct 2016 03:16:58 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2016-10-13T03:16:58Z</dc:date>
    <item>
      <title>sas add substring to an existing string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304230#M64747</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppsose I have a variable string with value name1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to add to this string ***, like this the string becomes name1***.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried to do:&lt;/P&gt;
&lt;P&gt;string = string&amp;nbsp;||"***";&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but it didn't work, I had to create a dummy variable and do dummy = string&amp;nbsp;||"***"; and delete the original string and rename the dummy string. But is there a direct way to add a substring to an existing string without having recourse to dummy variables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 13 Oct 2016 03:02:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304230#M64747</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2016-10-13T03:02:51Z</dc:date>
    </item>
    <item>
      <title>Re: sas add substring to an existing string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304233#M64749</link>
      <description>&lt;P&gt;You can do it directly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;string= trim(string) || '***';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're running into trouble because STRING is already defined with a fixed length. &amp;nbsp;If you just concatenate without adding the TRIM function, the trailing blanks remain in place and there isn't room to add any more characters.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Oct 2016 03:16:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304233#M64749</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-10-13T03:16:58Z</dc:date>
    </item>
    <item>
      <title>Re: sas add substring to an existing string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304236#M64750</link>
      <description>&lt;P&gt;I am still getting errors, namely in the cases where I have strings which are made entirely of numbers, so if I have something like this: 14.793 and want to get 14.793*** I can't.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But when I do a = string||'***'; I get 14.793*** even without trimming.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Oct 2016 04:11:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304236#M64750</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2016-10-13T04:11:01Z</dc:date>
    </item>
    <item>
      <title>Re: sas add substring to an existing string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304245#M64755</link>
      <description>&lt;P&gt;Post your exact code and log. &amp;nbsp;Note that your code above doesn't use the TRIM function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Following the suggestions of assigning a long enough LENGTH&amp;nbsp;and use of the TRIM&amp;nbsp;function I can't replicate your issue. In general, I prefer the CAT family of functions since they trim automatically. I also find it easier to read.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	length a b $20.;
	input a $ b $;
	datalines;
14.793 15.697
;
run;

data test2;
	set test;
	a=trim(a)||"***";
	b=catt(b, "****");
run;

proc print data=test2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Oct 2016 05:09:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304245#M64755</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-13T05:09:41Z</dc:date>
    </item>
    <item>
      <title>Re: sas add substring to an existing string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304289#M64766</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12982"&gt;@ilikesas&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I am still getting errors, namely in the cases where I have strings which are made entirely of numbers, so if I have something like this: 14.793 and want to get 14.793*** I can't.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But when I do a = string||'***'; I get 14.793*** even without trimming.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If your original variable is defined as type character with a length of 6, and contains the value '14.793', then it cannot accept any further characters.&lt;/P&gt;
&lt;P&gt;Since your new variable a is automatically defined by SAS with sufficient length (because you concatenate with a string literal, so SAS knows exactly how many characters are needed), it can take the whole new string.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
string = '14.793';
run;

proc contents;run;

data want;
set have;
a = string !! '***';
run;

proc contents;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Partial output from the second proc contents:&lt;/P&gt;
&lt;PRE&gt;Alphabetic List of Variables and Attributes

       #    Variable    Type    Len

       2    a           Char      9
       1    string      Char      6
&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Oct 2016 09:11:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304289#M64766</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-10-13T09:11:43Z</dc:date>
    </item>
    <item>
      <title>Re: sas add substring to an existing string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304368#M64793</link>
      <description>&lt;P&gt;That result is misleading. &amp;nbsp;It would occur when STRING is actually a numeric variable, not a character variable. &amp;nbsp;There is no way to add "***" to the value fo a numeric variable (although a format might be able to get it to print with "***" at the end). &amp;nbsp;When your original variable is character, you won't get any errors.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Oct 2016 14:13:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304368#M64793</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-10-13T14:13:07Z</dc:date>
    </item>
    <item>
      <title>Re: sas add substring to an existing string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304830#M64919</link>
      <description>&lt;P&gt;That is my case - I get errors that my variable is numeric, although I did try to convert it to character in the following way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;estimate = put(estimate,12.3.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 15 Oct 2016 02:10:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304830#M64919</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2016-10-15T02:10:09Z</dc:date>
    </item>
    <item>
      <title>Re: sas add substring to an existing string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304833#M64921</link>
      <description>&lt;P&gt;Once ESTIMATE is defined as numeric, it remains numeric. &amp;nbsp;Forever. &amp;nbsp;Yes, the PUT function generates a character string, but SAS then has to store those characters in a numeric variable, so SAS converts the character string to numeric.&lt;/P&gt;</description>
      <pubDate>Sat, 15 Oct 2016 02:36:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304833#M64921</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-10-15T02:36:00Z</dc:date>
    </item>
    <item>
      <title>Re: sas add substring to an existing string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304834#M64922</link>
      <description>&lt;P&gt;So yes, the only way to make ESTIMATE character is to create a new character DUMMY variable, make it equal ESTIMATE, delete the original ESTIMATE and then rename DUMMY to ESTIMATE. That is what I did in the very beginning, but I just thought if there was another way to make it directly. I accepeted as a solution the post where the string and catt functions are shown, making it more useful for future readers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Sat, 15 Oct 2016 02:43:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-add-substring-to-an-existing-string/m-p/304834#M64922</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2016-10-15T02:43:22Z</dc:date>
    </item>
  </channel>
</rss>

