<?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 extract text to RIGHT of character in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/825988#M326255</link>
    <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Is there actually a delimiter of -&amp;gt; in the data or was that just in your example? The existence of a delimiter will make a difference. It is possible to use the SCAN function to get the "back end" of a string, if there is a delimiter. The key is telling SCAN to start at the end of the string for scanning and extraction. Example 1 below uses -&amp;gt; and space as the the delimiters and Example 2 uses the | as the delimiter. The way that SCAN works is that contiguous delimiters are treated as 1 delimiter for purposes of scanning. the -1 tells SCAN to start at the right side of the string and bring back the first chunk on the end.&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example;
  length longstring $150 lastchunk $50 nextchunkback $50;
  infile datalines dlm=',' dsd;
  input examp_num longstring $;
  if examp_num = 1 then do;
     lastchunk = scan(longstring,-1,'-&amp;gt; ');
	 nextchunkback = scan(longstring,-2,'-&amp;gt; ');
  end;
  if examp_num = 2 then do;
     lastchunk = scan(longstring,-1,'|');
	 nextchunkback = scan(longstring,-2,'|');
  end;
  return;
  datalines;
  1,"aaa -&amp;gt; bbbbbbbbb-&amp;gt; cc -&amp;gt; dddddddddddddd"
  2,"xxxxxxxx|yyy|zzzzzzzzz|1z2z3z4z|5y6y7y8y9y|xx10xx11xx12xx"
  ;
  run;
  
  proc print data=example;
  var examp_num longstring lastchunk nextchunkback;
  run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 28 Jul 2022 16:27:42 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2022-07-28T16:27:42Z</dc:date>
    <item>
      <title>How to extract text to RIGHT of character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/825981#M326252</link>
      <description>&lt;P&gt;I have some data which was collected using some decision tree method.&amp;nbsp; The end result is a long text field which has data like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;xxxxxxxx -&amp;gt; xxxxxxxx -&amp;gt; xxxxxxxxxxxxxx -&amp;gt; xxxxxxxxxx&lt;/P&gt;
&lt;P&gt;xxx -&amp;gt; xxxxxxx -&amp;gt; xxxxxxxx&lt;/P&gt;
&lt;P&gt;xxxxxxx -&amp;gt; xxxxxxxx -&amp;gt; xxxxxxxxxxxx -&amp;gt; xxxxxxxxx -&amp;gt; xxxxxx&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;etc...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to extract the LAST string of text for each record to the RIGHT of the LAST "-&amp;gt;" characters.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;xxxxxxxx -&amp;gt; xxxxxxxx -&amp;gt; xxxxxxxxxxxxxx -&amp;gt;&lt;STRONG&gt; xxxxxxxxxx&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;xxx -&amp;gt; xxxxxxx -&amp;gt; &lt;STRONG&gt;xxxxxxxx&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;xxxxxxx -&amp;gt; xxxxxxxx -&amp;gt; xxxxxxxxxxxx -&amp;gt; xxxxxxxxx -&amp;gt; &lt;STRONG&gt;xxxxxx&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;etc...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Given each string between those characters is of different lengths, and each record has a different number of sets, I am lost on how to approach this.&amp;nbsp; &lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 16:06:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/825981#M326252</guid>
      <dc:creator>RandoDando</dc:creator>
      <dc:date>2022-07-28T16:06:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract text to RIGHT of character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/825988#M326255</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Is there actually a delimiter of -&amp;gt; in the data or was that just in your example? The existence of a delimiter will make a difference. It is possible to use the SCAN function to get the "back end" of a string, if there is a delimiter. The key is telling SCAN to start at the end of the string for scanning and extraction. Example 1 below uses -&amp;gt; and space as the the delimiters and Example 2 uses the | as the delimiter. The way that SCAN works is that contiguous delimiters are treated as 1 delimiter for purposes of scanning. the -1 tells SCAN to start at the right side of the string and bring back the first chunk on the end.&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example;
  length longstring $150 lastchunk $50 nextchunkback $50;
  infile datalines dlm=',' dsd;
  input examp_num longstring $;
  if examp_num = 1 then do;
     lastchunk = scan(longstring,-1,'-&amp;gt; ');
	 nextchunkback = scan(longstring,-2,'-&amp;gt; ');
  end;
  if examp_num = 2 then do;
     lastchunk = scan(longstring,-1,'|');
	 nextchunkback = scan(longstring,-2,'|');
  end;
  return;
  datalines;
  1,"aaa -&amp;gt; bbbbbbbbb-&amp;gt; cc -&amp;gt; dddddddddddddd"
  2,"xxxxxxxx|yyy|zzzzzzzzz|1z2z3z4z|5y6y7y8y9y|xx10xx11xx12xx"
  ;
  run;
  
  proc print data=example;
  var examp_num longstring lastchunk nextchunkback;
  run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 16:27:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/825988#M326255</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2022-07-28T16:27:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract text to RIGHT of character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/825990#M326257</link>
      <description>The text field I am looking at actually does have "-&amp;gt;" as the delimiter between sections of the string.</description>
      <pubDate>Thu, 28 Jul 2022 16:23:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/825990#M326257</guid>
      <dc:creator>RandoDando</dc:creator>
      <dc:date>2022-07-28T16:23:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract text to RIGHT of character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/825991#M326258</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dsd missover;
input var :$100.;
datalines;
xxxxx -&amp;gt; xxxx -&amp;gt; x
xx -&amp;gt; xxxx -&amp;gt; xxxxxxxx
xxxx -&amp;gt; xxx -&amp;gt; xxxxxxxxxxxxxxxxx
;
run;

data want;
	set have;
	want = strip(scan(var, -1, '-&amp;gt;'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="maguiremq_0-1659025489213.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/73847iAA93EB9C107DDB88/image-size/medium?v=v2&amp;amp;px=400" role="button" title="maguiremq_0-1659025489213.png" alt="maguiremq_0-1659025489213.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;You may not need `STRIP`. -1 gets the last in the string before a given delimiter. I specified the -&amp;gt; as the delimiter. There are a whole host of options in the documentation - very useful function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/p0jshdjy2z9zdzn1h7k90u99lyq6.htm" target="_blank" rel="noopener"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/p0jshdjy2z9zdzn1h7k90u99lyq6.htm&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 16:24:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/825991#M326258</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2022-07-28T16:24:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract text to RIGHT of character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/825994#M326260</link>
      <description>&lt;P&gt;That got it. Thanks. I knew it would be that function, I am just more accustomed to starting at the beginning.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 16:30:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/825994#M326260</guid>
      <dc:creator>RandoDando</dc:creator>
      <dc:date>2022-07-28T16:30:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract text to RIGHT of character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/825996#M326261</link>
      <description>&lt;P&gt;The SCAN function can extract "words" from a multiword expression.&amp;nbsp; You have such an expression, with word dividers of '-' and or '&amp;gt;'.&amp;nbsp; Use a -1 to tell scan to find the rightmost word&amp;nbsp; (+1 would be the leftmost word).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  longtext='aaaaaaa-&amp;gt;bbbb-&amp;gt;-&amp;gt;ccccc-&amp;gt;dddddddd-&amp;gt;eeee-&amp;gt;fffff-&amp;gt;gggggg-&amp;gt;hhhhhhh';
  lastword=scan(longtext,-1,'-&amp;gt;');
  put (_all_) (= /);
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 Jul 2022 16:31:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/825996#M326261</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-07-28T16:31:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract text to RIGHT of character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/825998#M326262</link>
      <description>&lt;P&gt;If you had a one byte delimiter, say just &amp;gt;, then you could use scan.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;last_word = scan(string,-1,'&amp;gt;');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that if the string has no &amp;gt; then the result is the whole string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need to use that three byte sequence, '-&amp;gt; ', then you will have to work harder.&amp;nbsp; You will need find the location of the last '-&amp;gt; ' and then use&amp;nbsp;SUBSTRN() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  input string $80.;
  loc = find(string,'-&amp;gt; ',-vlength(string));
  if loc then want = substrn(string,loc+3);
cards;
xxxxxxxx -&amp;gt; xxxxxxxx -&amp;gt; xxxxxxxxxxxxxx -&amp;gt; xxxxxxxxxx
xxx -&amp;gt; xxxxxxx -&amp;gt; xxxxxxxx
xxxxxxx -&amp;gt; xxxxxxxx -&amp;gt; xxxxxxxxxxxx -&amp;gt; xxxxxxxxx -&amp;gt; xxxxxx
none

;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs    string                                                        loc    want

 1     xxxxxxxx -&amp;gt; xxxxxxxx -&amp;gt; xxxxxxxxxxxxxx -&amp;gt; xxxxxxxxxx           40    xxxxxxxxxx
 2     xxx -&amp;gt; xxxxxxx -&amp;gt; xxxxxxxx                                     16    xxxxxxxx
 3     xxxxxxx -&amp;gt; xxxxxxxx -&amp;gt; xxxxxxxxxxxx -&amp;gt; xxxxxxxxx -&amp;gt; xxxxxx     50    xxxxxx
 4     none                                                            0
 5                                                                     0
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 16:39:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/825998#M326262</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-07-28T16:39:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract text to RIGHT of character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/825999#M326263</link>
      <description>&lt;P&gt;That is treating both - and &amp;gt; as delimited, independently.&lt;/P&gt;
&lt;P&gt;So strings like&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;xxx -&amp;gt;  aaa-bbb
xxx -&amp;gt;  aaa&amp;gt;bbb&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Will mistakenly return "bbb".&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 16:41:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/825999#M326263</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-07-28T16:41:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract text to RIGHT of character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/826002#M326266</link>
      <description>Good catch! Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;. Slipped by me. @willisva please look at some other solutions and mark them as the correct answer.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 28 Jul 2022 16:43:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/826002#M326266</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2022-07-28T16:43:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract text to RIGHT of character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/826006#M326268</link>
      <description>I just tried this and so far don't see a big difference in the output.  However, given what you have said I may just go with this.</description>
      <pubDate>Thu, 28 Jul 2022 16:58:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/826006#M326268</guid>
      <dc:creator>RandoDando</dc:creator>
      <dc:date>2022-07-28T16:58:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract text to RIGHT of character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/826016#M326270</link>
      <description>&lt;P&gt;As long as none of the characters after the last -&amp;gt; sequence contain &amp;gt; then just using scan() with &amp;gt; as the delimiter will work fine.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 17:31:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-text-to-RIGHT-of-character/m-p/826016#M326270</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-07-28T17:31:10Z</dc:date>
    </item>
  </channel>
</rss>

