<?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: substr function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/substr-function/m-p/715512#M221012</link>
    <description>I really appreciate, Tom.</description>
    <pubDate>Sat, 30 Jan 2021 04:30:13 GMT</pubDate>
    <dc:creator>runrunbunny</dc:creator>
    <dc:date>2021-01-30T04:30:13Z</dc:date>
    <item>
      <title>substr function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-function/m-p/715488#M221001</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The normal&amp;nbsp;substr function is straight forward. For example , substr(var,2,6)&amp;nbsp; means substract from the 2nd place to the 6th place of var. Today I saw someone write a length function nested in substr function, do any of you know what does it mean? Substract from which place to which place?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="4"&gt;SUBSTR(VAR,LENGTH(VAR)-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="4" color="#008080"&gt;4&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="4"&gt;,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="4" color="#008080"&gt;5&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="4"&gt;)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;Runrunbunny&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2021 23:57:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-function/m-p/715488#M221001</guid>
      <dc:creator>runrunbunny</dc:creator>
      <dc:date>2021-01-29T23:57:00Z</dc:date>
    </item>
    <item>
      <title>Re: substr function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-function/m-p/715496#M221004</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; It's always useful to review the syntax of the SUBSTR function when used for extraction:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1611966664733.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/54140i341B53AE3CF8DD6C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1611966664733.png" alt="Cynthia_sas_0-1611966664733.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Based on understanding what the 3 arguments here represent, then you can write a little test program to validate that you get the same response. Let's look at a LONG text string of 28 characters:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_1-1611966736271.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/54141i9E6C0CAD8E3A7589/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_1-1611966736271.png" alt="Cynthia_sas_1-1611966736271.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;If I want to extract (for whatever silly reason, the characters starting in position 24 of the string (in this case, "uppet", then I would need this:&lt;/P&gt;
&lt;P&gt;newvar = substr(var,24,5);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; I can also write a test program and use PUTLOG to verify that SUBSTR is working as designed:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_2-1611966986468.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/54142i4EE027D4CE713072/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_2-1611966986468.png" alt="Cynthia_sas_2-1611966986468.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;With a longer string, I'd have to adjust my amount to subtract or if I wanted to get a different piece from the longer string (such as the word Muppet or the word Kermit).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Sat, 30 Jan 2021 00:48:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-function/m-p/715496#M221004</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2021-01-30T00:48:40Z</dc:date>
    </item>
    <item>
      <title>Re: substr function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-function/m-p/715497#M221005</link>
      <description>&lt;P&gt;No.&amp;nbsp; The third argument is the number of bytes you want to extract, NOT the position of the last character.&lt;/P&gt;
&lt;P&gt;So&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;SUBSTR(VAR,2,6)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;means from the 2nd through the 7th position. To get the 2nd through the 6th position would be a length of 5 not 6.&lt;/P&gt;</description>
      <pubDate>Sat, 30 Jan 2021 01:00:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-function/m-p/715497#M221005</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-30T01:00:41Z</dc:date>
    </item>
    <item>
      <title>Re: substr function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-function/m-p/715504#M221006</link>
      <description>&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="4"&gt;Hi Cynthia,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="4"&gt;Thank you for taking time in doing the testing and a great example! &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="4"&gt;Like this example:&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size="4"&gt;SUBSTR(VAR,LENGTH(VAR)-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="4" color="#008080"&gt;4&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="4"&gt;,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="4" color="#008080"&gt;5&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="4"&gt;).&lt;FONT face="arial,helvetica,sans-serif"&gt;&amp;nbsp;If the length of var is 6, does the&amp;nbsp;length of the 2nd argumentation is 2?&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="4"&gt;Runrunbunny&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Jan 2021 01:30:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-function/m-p/715504#M221006</guid>
      <dc:creator>runrunbunny</dc:creator>
      <dc:date>2021-01-30T01:30:17Z</dc:date>
    </item>
    <item>
      <title>Re: substr function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-function/m-p/715505#M221007</link>
      <description>&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="4"&gt;Hi Tom,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="4"&gt;Thank you for correcting me!&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="4"&gt;Like this example:&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size="4"&gt;SUBSTR(VAR,LENGTH(VAR)-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="4" color="#008080"&gt;4&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="4"&gt;,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="4" color="#008080"&gt;5&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="4"&gt;).&lt;FONT face="arial,helvetica,sans-serif"&gt;&amp;nbsp;If the length of var is 6, does the&amp;nbsp;length of the 2nd argumentation is 2?&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="4"&gt;Runrunbunny&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Jan 2021 01:31:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-function/m-p/715505#M221007</guid>
      <dc:creator>runrunbunny</dc:creator>
      <dc:date>2021-01-30T01:31:15Z</dc:date>
    </item>
    <item>
      <title>Re: substr function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-function/m-p/715506#M221008</link>
      <description>Hi, When I do the math...6-4=2 .. on my calculator.That's why I said that if you wanted to start extracting at a different location, you might need to change the value for the second argument.&lt;BR /&gt;Cynthia</description>
      <pubDate>Sat, 30 Jan 2021 01:56:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-function/m-p/715506#M221008</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2021-01-30T01:56:23Z</dc:date>
    </item>
    <item>
      <title>Re: substr function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-function/m-p/715509#M221010</link>
      <description>&lt;P&gt;If you want the end of the string then do not include the last argument.&lt;/P&gt;
&lt;P&gt;If you wanted the last one character you would use&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;substr(x,length(x))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So if you want the last N characters you would use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;substr(x,length(x)-(N-1))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that if the length of X is less than N then you need to use SUBSTRN() instead of SUBSTR() or you will get an error when giving it a negative starting position.&lt;/P&gt;</description>
      <pubDate>Sat, 30 Jan 2021 17:45:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-function/m-p/715509#M221010</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-30T17:45:35Z</dc:date>
    </item>
    <item>
      <title>Re: substr function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-function/m-p/715512#M221012</link>
      <description>I really appreciate, Tom.</description>
      <pubDate>Sat, 30 Jan 2021 04:30:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-function/m-p/715512#M221012</guid>
      <dc:creator>runrunbunny</dc:creator>
      <dc:date>2021-01-30T04:30:13Z</dc:date>
    </item>
  </channel>
</rss>

