<?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: How to get a substring? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-substring/m-p/61655#M13399</link>
    <description>Even this will work..&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
	&lt;BR /&gt;
select reverse(substr(reverse(trim(teststring)),10))&lt;BR /&gt;
from have;&lt;BR /&gt;
&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
Reverse scan mentioned by you is perfect.&lt;BR /&gt;
&lt;BR /&gt;
Jignesh</description>
    <pubDate>Fri, 05 Mar 2010 19:11:33 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2010-03-05T19:11:33Z</dc:date>
    <item>
      <title>How to get a substring?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-substring/m-p/61648#M13392</link>
      <description>Hi all&lt;BR /&gt;
&lt;BR /&gt;
I have a set of strings which end with "_&lt;YYYYMMDD&gt;".&lt;BR /&gt;
&lt;BR /&gt;
I want to extract the substrings without the "_&lt;YYYYMMDD&gt;".&lt;BR /&gt;
&lt;BR /&gt;
I don't know how the rest of the strings will look like.&lt;BR /&gt;
&lt;BR /&gt;
I.e.&lt;BR /&gt;
have: TESTDATAV1_20100110&lt;BR /&gt;
want: TESTDATAV1&lt;BR /&gt;
&lt;BR /&gt;
I would prefer to do it with SQL but I just can't figure out an elegant solution.&lt;BR /&gt;
&lt;BR /&gt;
Any suggestions very welcome.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data have;&lt;BR /&gt;
  input TestString $20.;&lt;BR /&gt;
  datalines;&lt;BR /&gt;
TESTDATAV1_20100110&lt;BR /&gt;
TESTDATAV1_20100111&lt;BR /&gt;
TESTDATAV2_20100110&lt;BR /&gt;
TESTDATAV2_20100111&lt;BR /&gt;
TEST_DATAV1_20100110&lt;BR /&gt;
TEST_DATAV1_20100111&lt;BR /&gt;
TEST_DATAV2_20100110&lt;BR /&gt;
TEST_DATAV2_20100111&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  select ???(TestString) as TestStringRoot&lt;BR /&gt;
    from have&lt;BR /&gt;
  ;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Thanks&lt;BR /&gt;
Patrick&lt;BR /&gt;
&lt;BR /&gt;
P.S:&lt;BR /&gt;
The real world problem I have to solve is creating views with data sets following the pattern as given in the example.&lt;BR /&gt;
I.e: TESTDATAV1_20100110 and TESTDATAV1_20100111 should be selected to build the view TESTDATAV1.&lt;BR /&gt;
&lt;BR /&gt;
The only thing I know for sure is that the library will only contain tables following the naming pattern (=end with "_&lt;YYYYMMDD&gt;").&lt;BR /&gt;
&lt;BR /&gt;
I think I can solve this problem - but suggestions/experience from the field/a code example are very welcome.&lt;/YYYYMMDD&gt;&lt;/YYYYMMDD&gt;&lt;/YYYYMMDD&gt;</description>
      <pubDate>Wed, 24 Feb 2010 10:11:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-substring/m-p/61648#M13392</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-02-24T10:11:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to get a substring?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-substring/m-p/61649#M13393</link>
      <description>You can easily do this with the proper combination of text functions.&lt;BR /&gt;
&lt;BR /&gt;
One possibility would be:&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
select reverse(scan(reverse(TESTSTRING),2,'_')) as TESTSTRING from HAVE;&lt;BR /&gt;
quit;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
See the following info about reverse and scan functions:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000245941.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000245941.htm&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000214639.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000214639.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Wed, 24 Feb 2010 10:31:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-substring/m-p/61649#M13393</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2010-02-24T10:31:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to get a substring?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-substring/m-p/61650#M13394</link>
      <description>Hi Daniel&lt;BR /&gt;
&lt;BR /&gt;
The only thing I know for sure is that the string ends with "_&lt;YYYYMMDD&gt;".&lt;BR /&gt;
&lt;BR /&gt;
There can be 1 to n underscores in the string.&lt;BR /&gt;
&lt;BR /&gt;
The solution you proposed results for "TEST_DATAV1_20100110" in "DATAV1". &lt;BR /&gt;
&lt;BR /&gt;
The result I need is "TEST_DATAV1".&lt;BR /&gt;
&lt;BR /&gt;
It's about substracting a text pattern from a string - but only if the pattern is found at the end of the string. &lt;BR /&gt;
PRXCHANGE() comes to my mind for something like this. I'm just asking myself if there isn't a simple solution which can be used within PROC SQL and I just don't get it.&lt;BR /&gt;
&lt;BR /&gt;
Cheers&lt;BR /&gt;
Patrick

Message was edited by: Patrick&lt;/YYYYMMDD&gt;</description>
      <pubDate>Wed, 24 Feb 2010 11:31:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-substring/m-p/61650#M13394</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-02-24T11:31:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to get a substring?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-substring/m-p/61651#M13395</link>
      <description>Consider using the FIND function with a negative third argument of some max value like -99 -- this resulting value (minus 1) will resolve your third argument to SUBSTR.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Wed, 24 Feb 2010 11:51:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-substring/m-p/61651#M13395</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-02-24T11:51:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to get a substring?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-substring/m-p/61652#M13396</link>
      <description>Scott&lt;BR /&gt;
That works.&lt;BR /&gt;
Thanks&lt;BR /&gt;
Patrick&lt;BR /&gt;
&lt;BR /&gt;
data have;&lt;BR /&gt;
input TestString $20.;&lt;BR /&gt;
datalines;&lt;BR /&gt;
TESTDATAV1_20100110&lt;BR /&gt;
TESTDATAV1_20100111&lt;BR /&gt;
TESTDATAV2_20100110&lt;BR /&gt;
TESTDATAV2_20100111&lt;BR /&gt;
TEST_DATAV1_20100110&lt;BR /&gt;
TEST_DATAV1_20100111&lt;BR /&gt;
TEST_DATAV2_20100110&lt;BR /&gt;
TEST_DATAV2_20100111&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
select substr(TestString,1,find(TestString,'_',-length(TestString))-1) as TestStringRoot&lt;BR /&gt;
from have&lt;BR /&gt;
;&lt;BR /&gt;
quit;</description>
      <pubDate>Wed, 24 Feb 2010 12:14:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-substring/m-p/61652#M13396</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-02-24T12:14:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to get a substring?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-substring/m-p/61653#M13397</link>
      <description>Hi Patrick.&lt;BR /&gt;
&lt;BR /&gt;
OK, then, if it is possible to have more than a underscore, then switch the scan function to a combination of the substr/indexc functions, like this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
reverse(substr(reverse(TESTSTRING),indexc(reverse(TESTSTRING),'_')+1))&lt;BR /&gt;
[/pre]&lt;BR /&gt;
Same logic, which is reverse the string to find the first underscore, trim, then reverse the result again.&lt;BR /&gt;
&lt;BR /&gt;
Both FIND, INDEXC or even INDEX will work.&lt;BR /&gt;
FIND/INDEX searchs a substring in a string, while INDEXC searchs a character (or list of) in a string.&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal.&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Wed, 24 Feb 2010 14:49:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-substring/m-p/61653#M13397</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2010-02-24T14:49:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to get a substring?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-substring/m-p/61654#M13398</link>
      <description>Hi Daniel&lt;BR /&gt;
&lt;BR /&gt;
Thanks. Scott and you are now in sync &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;BR /&gt;
&lt;BR /&gt;
By the way: Since SAS 9 (I think) strings can also be processed backwards by many functions. &lt;BR /&gt;
I.e:&lt;BR /&gt;
reverse(scan(reverse(TESTSTRING),2,'_')) &lt;BR /&gt;
Could also be written as:&lt;BR /&gt;
scan(TESTSTRIN),-2,'_')&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Downunder</description>
      <pubDate>Thu, 25 Feb 2010 08:47:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-substring/m-p/61654#M13398</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-02-25T08:47:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to get a substring?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-substring/m-p/61655#M13399</link>
      <description>Even this will work..&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
	&lt;BR /&gt;
select reverse(substr(reverse(trim(teststring)),10))&lt;BR /&gt;
from have;&lt;BR /&gt;
&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
Reverse scan mentioned by you is perfect.&lt;BR /&gt;
&lt;BR /&gt;
Jignesh</description>
      <pubDate>Fri, 05 Mar 2010 19:11:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-substring/m-p/61655#M13399</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-03-05T19:11:33Z</dc:date>
    </item>
  </channel>
</rss>

