<?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: pull the middle value between first and last delimiter in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/pull-the-middle-value-between-first-and-last-delimiter/m-p/781734#M249148</link>
    <description>&lt;P&gt;untested, using findc&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set test1;
   s = findc(x, '-');
   e = findc(x, '-', 'b');
   str = substr(x, s+1, e-s-1);
   
   drop s e;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 22 Nov 2021 15:08:00 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2021-11-22T15:08:00Z</dc:date>
    <item>
      <title>pull the middle value between first and last delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pull-the-middle-value-between-first-and-last-delimiter/m-p/781722#M249141</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to find out is there any way to achieve final output in a better way wherein I need to pull the middle value between first and last delimiter. For example:&amp;nbsp;final&amp;nbsp;=&amp;nbsp;ABC -DEF -IJKL&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
x='014-ABC -DEF -IJKL -20 NOV 2019';
run;

data test2;
set test1;
y = substr(x, 1, index(x, "-"));
yl = length(y)+1;
y6 = substr(x, yl);
y2 = scan(y6, -1, "-");
y2l = length(y2)+1;
y3l = length(y6)-y2l;
final = substr(x, yl, y3l);
run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Nov 2021 14:19:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pull-the-middle-value-between-first-and-last-delimiter/m-p/781722#M249141</guid>
      <dc:creator>1239</dc:creator>
      <dc:date>2021-11-22T14:19:56Z</dc:date>
    </item>
    <item>
      <title>Re: pull the middle value between first and last delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pull-the-middle-value-between-first-and-last-delimiter/m-p/781733#M249147</link>
      <description>&lt;P&gt;Here are two ways.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One is to parse the string using - as delimiter and re-build the middle part using CATX().&amp;nbsp; Your messy string with inconsistent use of spaces in addition to hyphens between the words means the result is not exactly what was in the middle of the string.&lt;/P&gt;
&lt;P&gt;Second is to use CALL SCAN() to find out where the second word starts and where the last word starts and then take the characters in between.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
  x='014-ABC -DEF -IJKL -20 NOV 2019';
  length middle1 middle2 $40 ;
  do index=2 to countw(x,'-')-1;
    middle1=catx('-',middle1,scan(x,index,'-'));
  end;
  call scan(x,2,start,len1,'-');
  call scan(x,-1,stop,len2,'-');
  middle2=substrn(x,len1+1,stop-start-1);
  put (_all_) (=/);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;x=014-ABC -DEF -IJKL -20 NOV 2019
middle1=ABC-DEF-IJKL
middle2=ABC -DEF -IJKL
index=5
start=5
len1=4
stop=21
len2=11
&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Nov 2021 14:57:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pull-the-middle-value-between-first-and-last-delimiter/m-p/781733#M249147</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-11-22T14:57:31Z</dc:date>
    </item>
    <item>
      <title>Re: pull the middle value between first and last delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pull-the-middle-value-between-first-and-last-delimiter/m-p/781734#M249148</link>
      <description>&lt;P&gt;untested, using findc&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set test1;
   s = findc(x, '-');
   e = findc(x, '-', 'b');
   str = substr(x, s+1, e-s-1);
   
   drop s e;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Nov 2021 15:08:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pull-the-middle-value-between-first-and-last-delimiter/m-p/781734#M249148</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-11-22T15:08:00Z</dc:date>
    </item>
    <item>
      <title>Re: pull the middle value between first and last delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pull-the-middle-value-between-first-and-last-delimiter/m-p/781743#M249150</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19799"&gt;@1239&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yet another option (more cryptic and probably slower, though) is the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0r8h2fa8djqf1n1cnenrvm573br.htm" target="_blank" rel="noopener"&gt;PRXCHANGE function&lt;/A&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set test1;
length final $30; /* specify a sufficient length */
final=ifc(countc(x,'-')&amp;gt;1, prxchange('s/^[^-]*-(.*)-[^-]*$/\1/',1,x), ' ');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Explanation:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;If two or more delimiters (here: hyphens) are found in variable X, the substring between the first and last delimiter -- zero or more arbitrary characters (except newline), denoted by &lt;FONT face="courier new,courier"&gt;.*&lt;/FONT&gt; -- is taken as the value of variable FINAL.&lt;/LI&gt;
&lt;LI&gt;The substring between the beginning of the string (&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;^&lt;/FONT&gt;&lt;/STRONG&gt;) and the first hyphen as well as the substring between the last hyphen and the end of the string (&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;$&lt;/FONT&gt;&lt;/STRONG&gt;) -- both are sequences of arbitrary characters except the hyphen, denoted by &lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;[^-]*&lt;/FONT&gt;&lt;/STRONG&gt; -- are not part of FINAL.&lt;/LI&gt;
&lt;LI&gt;The replacement&amp;nbsp;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;\1&lt;/FONT&gt;&lt;/STRONG&gt; stands for the substring matched by the pattern&amp;nbsp;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;.*&lt;/FONT&gt;&lt;/STRONG&gt; in parentheses. (See&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0s9ilagexmjl8n1u7e1t1jfnzlk.htm" target="_blank" rel="noopener"&gt;Tables of Perl Regular Expression (PRX) Metacharacters&lt;/A&gt; for the definitions.)&lt;/LI&gt;
&lt;LI&gt;A single replacement (&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/FONT&gt; in the second argument of PRXCHANGE) achieves the desired result.&lt;/LI&gt;
&lt;LI&gt;If less than two hyphens are found in X, FINAL is set to a missing value (third argument of the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p07za51a2k0sxkn1m0tulx2cy464.htm" target="_blank" rel="noopener"&gt;IFC function&lt;/A&gt;), but feel free to define FINAL differently in this case or to remove IFC and COUNTC entirely if the condition is not needed for your data.&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Mon, 22 Nov 2021 16:12:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pull-the-middle-value-between-first-and-last-delimiter/m-p/781743#M249150</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-11-22T16:12:27Z</dc:date>
    </item>
    <item>
      <title>Re: pull the middle value between first and last delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pull-the-middle-value-between-first-and-last-delimiter/m-p/781858#M249195</link>
      <description>&lt;P&gt;Hi, Thanks for your help. Your code also working but I couldn't explore more on &lt;SPAN&gt;PRXCHANGE function.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Nov 2021 05:27:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pull-the-middle-value-between-first-and-last-delimiter/m-p/781858#M249195</guid>
      <dc:creator>1239</dc:creator>
      <dc:date>2021-11-23T05:27:22Z</dc:date>
    </item>
    <item>
      <title>Re: pull the middle value between first and last delimiter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/pull-the-middle-value-between-first-and-last-delimiter/m-p/781864#M249197</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt;&amp;nbsp;explore more on&amp;nbsp;PRXCHANGE function.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data T;
  A=prxchange('s/[^-]+-(.*)-[^-]+/\1/', 1, 'A-B- C-D-E ');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A = 'B- C-D'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Nov 2021 06:00:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/pull-the-middle-value-between-first-and-last-delimiter/m-p/781864#M249197</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-11-23T06:00:21Z</dc:date>
    </item>
  </channel>
</rss>

