<?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: Extracting substring using scan or any function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/18962#M2887</link>
    <description>Two way of doing it...&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data input;&lt;BR /&gt;
  input str $;&lt;BR /&gt;
  datalines;&lt;BR /&gt;
A&lt;BR /&gt;
A,B&lt;BR /&gt;
A,B,C&lt;BR /&gt;
A,B,C,D&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
 &lt;BR /&gt;
data extract1(drop=_str_length _i _comma_pos);&lt;BR /&gt;
  set input;&lt;BR /&gt;
  _str_length = length(str);&lt;BR /&gt;
  do _i = 1 to _str_length;&lt;BR /&gt;
    if substr(str,_i,1) = ',' then _comma_pos=_i-1;&lt;BR /&gt;
  end;&lt;BR /&gt;
  ext=substr(str,1,max(_comma_pos,1));&lt;BR /&gt;
  output;&lt;BR /&gt;
run; &lt;BR /&gt;
 &lt;BR /&gt;
data extract2(drop=_last_element);&lt;BR /&gt;
  set input;&lt;BR /&gt;
  _last_element=scan(str,-1);&lt;BR /&gt;
  ext=tranwrd(str,cats(',',_last_element),'');&lt;BR /&gt;
run; &lt;BR /&gt;
[/pre]</description>
    <pubDate>Wed, 22 Jun 2011 11:40:56 GMT</pubDate>
    <dc:creator>GertNissen</dc:creator>
    <dc:date>2011-06-22T11:40:56Z</dc:date>
    <item>
      <title>Extracting substring using scan or any function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/18961#M2886</link>
      <description>Hi All..&lt;BR /&gt;
&lt;BR /&gt;
     I want to extract the substring from a string till the last occurrence of my delimiter, for example a dataset has values a below..&lt;BR /&gt;
&lt;BR /&gt;
str&lt;BR /&gt;
A&lt;BR /&gt;
A,B&lt;BR /&gt;
A,B,C&lt;BR /&gt;
A,B,C,D&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
I want to extract the substring till the last occurence of ',' in the above dataset.the resultant dataset should look like ...&lt;BR /&gt;
&lt;BR /&gt;
str&lt;BR /&gt;
A&lt;BR /&gt;
A&lt;BR /&gt;
A,B&lt;BR /&gt;
A,B,C&lt;BR /&gt;
&lt;BR /&gt;
Thanks in advance..</description>
      <pubDate>Wed, 22 Jun 2011 10:34:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/18961#M2886</guid>
      <dc:creator>kishore415</dc:creator>
      <dc:date>2011-06-22T10:34:19Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting substring using scan or any function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/18962#M2887</link>
      <description>Two way of doing it...&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data input;&lt;BR /&gt;
  input str $;&lt;BR /&gt;
  datalines;&lt;BR /&gt;
A&lt;BR /&gt;
A,B&lt;BR /&gt;
A,B,C&lt;BR /&gt;
A,B,C,D&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
 &lt;BR /&gt;
data extract1(drop=_str_length _i _comma_pos);&lt;BR /&gt;
  set input;&lt;BR /&gt;
  _str_length = length(str);&lt;BR /&gt;
  do _i = 1 to _str_length;&lt;BR /&gt;
    if substr(str,_i,1) = ',' then _comma_pos=_i-1;&lt;BR /&gt;
  end;&lt;BR /&gt;
  ext=substr(str,1,max(_comma_pos,1));&lt;BR /&gt;
  output;&lt;BR /&gt;
run; &lt;BR /&gt;
 &lt;BR /&gt;
data extract2(drop=_last_element);&lt;BR /&gt;
  set input;&lt;BR /&gt;
  _last_element=scan(str,-1);&lt;BR /&gt;
  ext=tranwrd(str,cats(',',_last_element),'');&lt;BR /&gt;
run; &lt;BR /&gt;
[/pre]</description>
      <pubDate>Wed, 22 Jun 2011 11:40:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/18962#M2887</guid>
      <dc:creator>GertNissen</dc:creator>
      <dc:date>2011-06-22T11:40:56Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting substring using scan or any function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/18963#M2888</link>
      <description>SCAN may be better for your approach. You can specify a list of delimiters if something other than the defaults and to search backwards, the -1 says start at the end and find the first "word" counting backwards.&lt;BR /&gt;
&lt;BR /&gt;
result= scan(str,-1);</description>
      <pubDate>Wed, 22 Jun 2011 18:12:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/18963#M2888</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2011-06-22T18:12:59Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting substring using scan or any function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/18964#M2889</link>
      <description>&lt;P&gt;Just another variation:&lt;BR /&gt; &lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data input; 
input str $; 
datalines;
A
A,B
A,B,C
A,B,C,D
;
run;

data want;
 set input;
 SubStrLength=findc(str, ',',-length(str)) ;
 if SubStrLength=0 then WantStr=Str;
  else WantStr=substr(str,1,SubStrLength-1);
 drop SubStrLength;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And this from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/36"&gt;@cidab&lt;/a&gt;:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data  input;
 input str $;
 want = substr(str,1,length(str)-indexc(reverse(trim(str)),','));    
...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; **trim the input string, reverse it and find the position of last (now first) comma, then substring input string from start to string length minus the number of characters to drop;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Mon, 07 Aug 2017 15:30:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/18964#M2889</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-08-07T15:30:12Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting substring using scan or any function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/18965#M2890</link>
      <description>Dear ballardw &lt;BR /&gt;
&lt;BR /&gt;
scan with -1 will give last word but I want remaining words excepting last word....

Message was edited by: kishore415</description>
      <pubDate>Thu, 23 Jun 2011 03:51:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/18965#M2890</guid>
      <dc:creator>kishore415</dc:creator>
      <dc:date>2011-06-23T03:51:14Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting substring using scan or any function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/18966#M2891</link>
      <description>Dear Patrick...&lt;BR /&gt;
&lt;BR /&gt;
  Your code looks simple and works..thanks a lot...</description>
      <pubDate>Thu, 23 Jun 2011 03:55:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/18966#M2891</guid>
      <dc:creator>kishore415</dc:creator>
      <dc:date>2011-06-23T03:55:01Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting substring using scan or any function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/18967#M2892</link>
      <description>[pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data input;&lt;BR /&gt;
input str $20.;&lt;BR /&gt;
position=findc(str,',','b');&lt;BR /&gt;
if position=0 then want=str;&lt;BR /&gt;
 else want=substr(str,1,position-1);&lt;BR /&gt;
datalines;&lt;BR /&gt;
A&lt;BR /&gt;
A,Brertyr&lt;BR /&gt;
A,B,Ce&lt;BR /&gt;
A,B,C,Ddsd&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Thu, 23 Jun 2011 09:58:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/18967#M2892</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-23T09:58:35Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting substring using scan or any function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/18968#M2893</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #008080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG&gt;&lt;SPAN style="font-size: 10pt; color: #000080; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;P&gt;data&amp;nbsp; &lt;SPAN style="font-size: 10pt; font-family: Courier New;"&gt; input;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: Courier New;"&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 10pt; font-family: Courier New;"&gt;input&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; font-family: Courier New;"&gt; str $;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: Courier New;"&gt;want = substr(str,&lt;/SPAN&gt;&lt;STRONG style="color: #008080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #008080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #008080; font-size: 10pt; font-family: Courier New;"&gt;1&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;SPAN style="font-size: 10pt; font-family: Courier New;"&gt;,length(str)-indexc(reverse(trim(str)),','&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; font-family: Courier New;"&gt;));&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: Courier New;"&gt;&amp;nbsp; **trim the input string, reverse it and find the position of last (now first) comma, then substring input string from start to string length minus the number of characters to drop;&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV&gt;&lt;SPAN style="font-size: 10pt; font-family: Courier New;"&gt; &lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN style="font-size: 10pt; font-family: Courier New;"&gt; &lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN style="font-size: 10pt; font-family: Courier New;"&gt; &lt;/SPAN&gt;&lt;/DIV&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; font-family: Courier New;"&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;/P&gt;&lt;SPAN style="font-size: 10pt; color: #000080; font-family: Courier New;"&gt;&lt;SPAN style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;P&gt;&lt;/P&gt;&lt;/SPAN&gt;&lt;P&gt;&lt;/P&gt;&lt;/SPAN&gt;&lt;P&gt;&lt;SPAN style="color: #008080; font-size: 10pt; font-family: Courier New;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;/STRONG&gt; &lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;P&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #008080; font-size: 10pt; font-family: Courier New;"&gt; &lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #008080; font-size: 10pt; font-family: Courier New;"&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;DIV&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt; &lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/DIV&gt;&lt;P&gt;&lt;/P&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG&gt; &lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;SPAN style="font-size: 10pt; color: #000080; font-family: Courier New;"&gt;&lt;STRONG&gt;&lt;SPAN style="font-size: 10pt; color: #000080; font-family: Courier New;"&gt;&lt;STRONG style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;STRONG&gt; &lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;DIV&gt; &lt;/DIV&gt; &lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;SPAN style="color: #000080; font-size: 10pt; font-family: Courier New;"&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/SPAN&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #008080; font-size: 10pt; font-family: Courier New;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Jun 2011 17:24:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/18968#M2893</guid>
      <dc:creator>cidab</dc:creator>
      <dc:date>2011-06-24T17:24:45Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting substring using scan or any function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/298433#M62762</link>
      <description>&lt;P&gt;cidab has&amp;nbsp;a great solution. I've used this a couple times now to only extract what I need from a string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nathan Och&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2016 20:10:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/298433#M62762</guid>
      <dc:creator>NathanOch</dc:creator>
      <dc:date>2016-09-14T20:10:01Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting substring using scan or any function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/419846#M103266</link>
      <description>&lt;P&gt;data input_extract;&lt;BR /&gt;set input;&lt;BR /&gt;&lt;BR /&gt;len = length(str);&lt;BR /&gt;&lt;BR /&gt;do i =1 to len;&lt;BR /&gt;len1 = len -2;&lt;BR /&gt;sub_str = substr(str,1,len1);&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Dec 2017 20:20:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-substring-using-scan-or-any-function/m-p/419846#M103266</guid>
      <dc:creator>arpi</dc:creator>
      <dc:date>2017-12-09T20:20:55Z</dc:date>
    </item>
  </channel>
</rss>

