<?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 Change value of a string variable while using retain statement? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Change-value-of-a-string-variable-while-using-retain-statement/m-p/848265#M335366</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I'm having trouble trying to assign a value to a string variable while using the retain statement.&lt;/P&gt;&lt;P&gt;I can retain a numeric variable while still being able to assign a new value to it, so that the value is continously evolving while going through each observation, as shown in the below simplified example with the variable "num".&lt;/P&gt;&lt;P&gt;But when I try to do the same with a characteristic variable, the variable seems to have been frozen to its initial value, which in this case, is "Test:".&lt;/P&gt;&lt;PRE&gt;Data test;
	Length str $ 100;
	retain num 0;	retain str "TEST:";
	set Sashelp.buy;
	if AMOUNT &amp;lt; -1000 then do;
		num = num + 1;
		str = str || "T";
	end;
	else str = str || "F";
run;&lt;/PRE&gt;&lt;P&gt;The final outcome I'm trying to get is something like: "Test:TFFTTTTTTTF", in the last observation. However this is what I get at the moment using the above code.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="retaintestscreenshot.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78150i6B40588644EE1DEB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="retaintestscreenshot.png" alt="retaintestscreenshot.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The length command in the code was to show that the issue is not caused by issue with length of the string variable.&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Wed, 07 Dec 2022 02:39:36 GMT</pubDate>
    <dc:creator>wetman</dc:creator>
    <dc:date>2022-12-07T02:39:36Z</dc:date>
    <item>
      <title>Change value of a string variable while using retain statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-value-of-a-string-variable-while-using-retain-statement/m-p/848265#M335366</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I'm having trouble trying to assign a value to a string variable while using the retain statement.&lt;/P&gt;&lt;P&gt;I can retain a numeric variable while still being able to assign a new value to it, so that the value is continously evolving while going through each observation, as shown in the below simplified example with the variable "num".&lt;/P&gt;&lt;P&gt;But when I try to do the same with a characteristic variable, the variable seems to have been frozen to its initial value, which in this case, is "Test:".&lt;/P&gt;&lt;PRE&gt;Data test;
	Length str $ 100;
	retain num 0;	retain str "TEST:";
	set Sashelp.buy;
	if AMOUNT &amp;lt; -1000 then do;
		num = num + 1;
		str = str || "T";
	end;
	else str = str || "F";
run;&lt;/PRE&gt;&lt;P&gt;The final outcome I'm trying to get is something like: "Test:TFFTTTTTTTF", in the last observation. However this is what I get at the moment using the above code.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="retaintestscreenshot.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78150i6B40588644EE1DEB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="retaintestscreenshot.png" alt="retaintestscreenshot.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The length command in the code was to show that the issue is not caused by issue with length of the string variable.&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2022 02:39:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-value-of-a-string-variable-while-using-retain-statement/m-p/848265#M335366</guid>
      <dc:creator>wetman</dc:creator>
      <dc:date>2022-12-07T02:39:36Z</dc:date>
    </item>
    <item>
      <title>Re: Change value of a string variable while using retain statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-value-of-a-string-variable-while-using-retain-statement/m-p/848266#M335367</link>
      <description>&lt;P&gt;The operator "||" does NOT pre-trim the contents of its arguments.&amp;nbsp; So the statement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;		str = str || "T";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;essentially constructs at string of 101 bytes length (100 for str, and 1 for "T").&amp;nbsp; &amp;nbsp;It then truncates all characters beyond number 100.&amp;nbsp; Consider using&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;str=trim(str) || "T";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;str=cats(str,"T");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2022 03:03:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-value-of-a-string-variable-while-using-retain-statement/m-p/848266#M335367</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-12-07T03:03:57Z</dc:date>
    </item>
    <item>
      <title>Re: Change value of a string variable while using retain statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-value-of-a-string-variable-while-using-retain-statement/m-p/848267#M335368</link>
      <description>Thank you so much! I tried both solutions and they all work for me!</description>
      <pubDate>Wed, 07 Dec 2022 03:08:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-value-of-a-string-variable-while-using-retain-statement/m-p/848267#M335368</guid>
      <dc:creator>wetman</dc:creator>
      <dc:date>2022-12-07T03:08:23Z</dc:date>
    </item>
  </channel>
</rss>

