<?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: using SUBSTR function extracts last 3 values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/using-SUBSTR-function-extracts-last-3-values/m-p/329419#M73696</link>
    <description>&lt;P&gt;Well one method is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;SPAN&gt;data want;&lt;BR /&gt;  trt="treat123";&lt;BR /&gt;  want=substr(trt,lengthn(trt)-2);&lt;BR /&gt;run;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 02 Feb 2017 14:02:49 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-02-02T14:02:49Z</dc:date>
    <item>
      <title>using SUBSTR function extracts last 3 values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-SUBSTR-function-extracts-last-3-values/m-p/329409#M73689</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The below program using substr extracts last 3 values from obs, can anyone please help me to undersatnad the logic &lt;STRONG&gt;using substr function&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;input trt $;&lt;BR /&gt;cards;&lt;BR /&gt;treat123&lt;BR /&gt;treat21&lt;BR /&gt;treat1&lt;BR /&gt;treat1&lt;BR /&gt;trea2&lt;BR /&gt;lbdate&lt;BR /&gt;;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data abc;&lt;BR /&gt;set temp;&lt;BR /&gt;a=reverse(substr(left(reverse(trt)),1,3));&lt;BR /&gt;&lt;STRONG&gt;b=substr(trt,length(trt)-2);&lt;/STRONG&gt;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 13:16:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-SUBSTR-function-extracts-last-3-values/m-p/329409#M73689</guid>
      <dc:creator>tinayong</dc:creator>
      <dc:date>2017-02-02T13:16:18Z</dc:date>
    </item>
    <item>
      <title>Re: using SUBSTR function extracts last 3 values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-SUBSTR-function-extracts-last-3-values/m-p/329416#M73693</link>
      <description>&lt;P&gt;Which of these do you already understand?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;What does the SUBSTR function do?&lt;/LI&gt;
&lt;LI&gt;What are the three parameters for SUBSTR?&lt;/LI&gt;
&lt;LI&gt;What does the LENGTH function do?&lt;/LI&gt;
&lt;LI&gt;What does the REVERSE function do?&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Thu, 02 Feb 2017 13:58:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-SUBSTR-function-extracts-last-3-values/m-p/329416#M73693</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-02-02T13:58:05Z</dc:date>
    </item>
    <item>
      <title>Re: using SUBSTR function extracts last 3 values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-SUBSTR-function-extracts-last-3-values/m-p/329419#M73696</link>
      <description>&lt;P&gt;Well one method is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;SPAN&gt;data want;&lt;BR /&gt;  trt="treat123";&lt;BR /&gt;  want=substr(trt,lengthn(trt)-2);&lt;BR /&gt;run;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 14:02:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-SUBSTR-function-extracts-last-3-values/m-p/329419#M73696</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-02-02T14:02:49Z</dc:date>
    </item>
    <item>
      <title>Re: using SUBSTR function extracts last 3 values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-SUBSTR-function-extracts-last-3-values/m-p/329454#M73710</link>
      <description>output of the program&lt;BR /&gt;123&lt;BR /&gt;t21&lt;BR /&gt;at1&lt;BR /&gt;at1&lt;BR /&gt;ea2&lt;BR /&gt;ate&lt;BR /&gt;&lt;BR /&gt;b=substr(trt,length(trt)-2);&lt;BR /&gt;length of the variabke is 8 and 8-2 =6,&lt;BR /&gt;so for the first observation from the sixth position we get the values using substr function&lt;BR /&gt;but from the second observation onwards the obs values decreasing but still substr function extracting last 3 values&lt;BR /&gt;&lt;BR /&gt;So am missing some logic here, may be am not thinking correct way&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 02 Feb 2017 15:06:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-SUBSTR-function-extracts-last-3-values/m-p/329454#M73710</guid>
      <dc:creator>tinayong</dc:creator>
      <dc:date>2017-02-02T15:06:32Z</dc:date>
    </item>
    <item>
      <title>Re: using SUBSTR function extracts last 3 values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-SUBSTR-function-extracts-last-3-values/m-p/329461#M73713</link>
      <description>&lt;P&gt;For that piece of the puzzle ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second parameter within SUBSTR is a number that&amp;nbsp;indicates which character to begin with.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When the third parameter is omitted (as is the case here), SUBSTR takes all the remaining characters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So this code says measure the length of the incoming string.&amp;nbsp; Substract 2 from that.&amp;nbsp; That becomes the starting position.&amp;nbsp; This means that there will be 3 characters available (as long as the length of the original string is at least 3 characters).&amp;nbsp; Better code would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if length(trt) &amp;gt;= 3 then b = substr(trt, length(trt)-2);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That protects against calculating a negative starting position, something that SUBSTR would complain about.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 15:20:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-SUBSTR-function-extracts-last-3-values/m-p/329461#M73713</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-02-02T15:20:22Z</dc:date>
    </item>
    <item>
      <title>Re: using SUBSTR function extracts last 3 values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-SUBSTR-function-extracts-last-3-values/m-p/329473#M73719</link>
      <description>&lt;P&gt;Good time to read the manual methinks, substr is clearly described there.&lt;/P&gt;
&lt;P&gt;If you just want the numbers at the end (and they don't appear anywhere else in the string) compress is simpler:&lt;/P&gt;
&lt;PRE&gt;data want;
  text="abcd123";
  num=compress(text," ","kd");
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Feb 2017 15:44:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-SUBSTR-function-extracts-last-3-values/m-p/329473#M73719</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-02-02T15:44:27Z</dc:date>
    </item>
    <item>
      <title>Re: using SUBSTR function extracts last 3 values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-SUBSTR-function-extracts-last-3-values/m-p/329505#M73726</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/126419"&gt;@tinayong&lt;/a&gt; wrote:&lt;BR /&gt;output of the program&lt;BR /&gt;123&lt;BR /&gt;t21&lt;BR /&gt;at1&lt;BR /&gt;at1&lt;BR /&gt;ea2&lt;BR /&gt;ate&lt;BR /&gt;&lt;BR /&gt;b=substr(trt,length(trt)-2);&lt;BR /&gt;length of the variabke is 8 and 8-2 =6,&lt;BR /&gt;so for the first observation from the sixth position we get the values using substr function&lt;BR /&gt;but from the second observation onwards the obs values decreasing but still substr function extracting last 3 values&lt;BR /&gt;&lt;BR /&gt;So am missing some logic here, may be am not thinking correct way&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Perhaps you phrased your question incorrectly. If you are wanting the numbers at the end and not the "last 3 values"&amp;nbsp;then &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&amp;nbsp;has your solution with compress function&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 16:50:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-SUBSTR-function-extracts-last-3-values/m-p/329505#M73726</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-02-02T16:50:47Z</dc:date>
    </item>
  </channel>
</rss>

