<?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 How to use SUBSTR to create another variable? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-SUBSTR-to-create-another-variable/m-p/985317#M379822</link>
    <description>&lt;P&gt;hi all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If run the following code, I can create a new column "new_val" that is the sub of the original value, which returns 98.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data news;
val='06MAY98';
new_val=substr(val, 6, 2);

proc print data=news; run;&lt;/PRE&gt;
&lt;DIV id="tinyMceEditorNietzsche_0" class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV id="tinyMceEditorNietzsche_1" class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 200px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/113806iD692355E46D28028/image-size/small?v=v2&amp;amp;px=200" role="button" title="image.png" alt="image.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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This time I want to replace the year 98 with 99, so I wrote this following code, but instead of getting '06MAY99', I got a 0.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data news;
val='06MAY98';
new_val=substr(val, 6, 2) = 99;

proc print data=news; run;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 200px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/113807i530AB866A957ACD5/image-size/small?v=v2&amp;amp;px=200" role="button" title="image.png" alt="image.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;is there any way to fix this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 25 Mar 2026 07:23:02 GMT</pubDate>
    <dc:creator>Nietzsche</dc:creator>
    <dc:date>2026-03-25T07:23:02Z</dc:date>
    <item>
      <title>How to use SUBSTR to create another variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-SUBSTR-to-create-another-variable/m-p/985317#M379822</link>
      <description>&lt;P&gt;hi all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If run the following code, I can create a new column "new_val" that is the sub of the original value, which returns 98.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data news;
val='06MAY98';
new_val=substr(val, 6, 2);

proc print data=news; run;&lt;/PRE&gt;
&lt;DIV id="tinyMceEditorNietzsche_0" class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV id="tinyMceEditorNietzsche_1" class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 200px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/113806iD692355E46D28028/image-size/small?v=v2&amp;amp;px=200" role="button" title="image.png" alt="image.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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This time I want to replace the year 98 with 99, so I wrote this following code, but instead of getting '06MAY99', I got a 0.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data news;
val='06MAY98';
new_val=substr(val, 6, 2) = 99;

proc print data=news; run;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 200px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/113807i530AB866A957ACD5/image-size/small?v=v2&amp;amp;px=200" role="button" title="image.png" alt="image.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;is there any way to fix this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Mar 2026 07:23:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-SUBSTR-to-create-another-variable/m-p/985317#M379822</guid>
      <dc:creator>Nietzsche</dc:creator>
      <dc:date>2026-03-25T07:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to use SUBSTR to create another variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-SUBSTR-to-create-another-variable/m-p/985318#M379823</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/431484"&gt;@Nietzsche&lt;/a&gt;&amp;nbsp;, in your example you can fix it with&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data news;
val='06MAY98';
new_val=cat(substr(val, 0, 5),99);

proc print data=news; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;two remarks:&lt;/P&gt;
&lt;P&gt;1. "substr" is a function to get a substring not to change a substring.&lt;/P&gt;
&lt;P&gt;2. A general better approach might be to work with dates instead of character value resembling dates.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Mar 2026 07:44:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-SUBSTR-to-create-another-variable/m-p/985318#M379823</guid>
      <dc:creator>MarkusWeick</dc:creator>
      <dc:date>2026-03-25T07:44:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to use SUBSTR to create another variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-SUBSTR-to-create-another-variable/m-p/985319#M379824</link>
      <description>&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/401244"&gt;@MarkusWeick&lt;/a&gt;&amp;nbsp;suggest, use a numerical date formatted column for this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data news;
   val = '06May1998'd;
   new_val = intnx('YEAR',val, 1, 'SAME');
   format val new_val date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Mar 2026 07:56:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-SUBSTR-to-create-another-variable/m-p/985319#M379824</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2026-03-25T07:56:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to use SUBSTR to create another variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-SUBSTR-to-create-another-variable/m-p/985320#M379825</link>
      <description>&lt;P&gt;Use left-side SUBSTR() function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data news;
val='06MAY98';
&lt;STRONG&gt;substr(val, 6, 2)&lt;/STRONG&gt; = 99;

proc print data=news; run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Mar 2026 07:58:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-SUBSTR-to-create-another-variable/m-p/985320#M379825</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2026-03-25T07:58:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to use SUBSTR to create another variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-SUBSTR-to-create-another-variable/m-p/985321#M379826</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/431484"&gt;@Nietzsche&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your result 0 is the Boolean value saying that the equality 98 = 99 is false. (The character string&amp;nbsp;substr(val, 6, 2)=&lt;STRONG&gt;'98'&lt;/STRONG&gt; was automatically converted to the number &lt;STRONG&gt;98&lt;/STRONG&gt;, see the corresponding note in the log.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0uev77ebdwy90n1rsd7hwjd2qc3.htm" target="_blank" rel="noopener"&gt;SUBSTR (left of =&lt;FONT face="arial,helvetica,sans-serif"&gt;)&lt;/FONT&gt; function&lt;/A&gt; modifies the value of the variable in the first argument, so you should create NEW_VAL first with the old value and then modify it:&lt;/P&gt;
&lt;PRE&gt;data news;
val='06MAY98';
new_val=val;
substr(new_val, 6)='99';
run;&lt;/PRE&gt;
&lt;P&gt;Alternatively, you could define NEW_VAL like this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;new_val=substr(val, 1, 5)||'99';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;using the&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0n08xougp40i5n1xw7njpcy0a2b.htm" target="_blank" rel="noopener"&gt;SUBSTR (right of =&lt;FONT face="arial,helvetica,sans-serif"&gt;)&lt;/FONT&gt; function&lt;/A&gt;. But then the length of NEW_VAL would be 9 (=&lt;FONT face="courier new,courier"&gt;length(val)+length('99')&lt;/FONT&gt;) by default rather than 7 with the previous method. The preliminary assignment statement &lt;FONT face="courier new,courier"&gt;new_val=val&lt;/FONT&gt; or a LENGTH statement could avoid that.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Mar 2026 08:02:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-SUBSTR-to-create-another-variable/m-p/985321#M379826</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2026-03-25T08:02:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to use SUBSTR to create another variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-SUBSTR-to-create-another-variable/m-p/985322#M379827</link>
      <description>Thanks for the left-side SUBSTR() function</description>
      <pubDate>Wed, 25 Mar 2026 08:20:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-SUBSTR-to-create-another-variable/m-p/985322#M379827</guid>
      <dc:creator>MarkusWeick</dc:creator>
      <dc:date>2026-03-25T08:20:57Z</dc:date>
    </item>
  </channel>
</rss>

