<?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: String manipulation - show certain characters based on condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/String-manipulation-show-certain-characters-based-on-condition/m-p/615036#M179859</link>
    <description>&lt;P&gt;SCAN() can probably do what you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ln_no $ Branch $50. ;
datalines;
33 CHARLOTTE COUNTY - DEEP CREEK
22 CHASTAIN SQUARE PUBLIX (closed)
62 CHARLOTTESVILLE AREA - ALBEMARE
64 MARSHALL SQUARE PUBLIX (closed)
;

data want;
  set have;
  length want1 want2 $50;
  want1 = strip(coalescec(scan(branch,2,'-'),scan(branch,-2,'()')));
  want2 = strip(coalescec(scan(branch,2,'-'),scan(branch,1,'()')));
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    ln_no                Branch                 want1         want2

 1      33      CHARLOTTE COUNTY - DEEP CREEK      DEEP CREEK    DEEP CREEK
 2      22      CHASTAIN SQUARE PUBLIX (closed)    closed        CHASTAIN SQUARE PUBLIX
 3      62      CHARLOTTESVILLE AREA - ALBEMARE    ALBEMARE      ALBEMARE
 4      64      MARSHALL SQUARE PUBLIX (closed)    closed        MARSHALL SQUARE PUBLIX&lt;/PRE&gt;</description>
    <pubDate>Fri, 03 Jan 2020 19:14:23 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-01-03T19:14:23Z</dc:date>
    <item>
      <title>String manipulation - show certain characters based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-manipulation-show-certain-characters-based-on-condition/m-p/615034#M179857</link>
      <description>&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; have;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;infile&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;datalines&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; ln_no $ Branch &lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;$50.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; ;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;return&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;datalines&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;33 CHARLOTTE COUNTY - DEEP CREEK&lt;/P&gt;
&lt;P&gt;22 CHASTAIN SQUARE PUBLIX (closed)&lt;/P&gt;
&lt;P&gt;62 CHARLOTTESVILLE AREA - ALBEMARE&lt;/P&gt;
&lt;P&gt;64&amp;nbsp;MARSHALL SQUARE PUBLIX (closed)&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;run&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/*I want to accomplish the following:&lt;/P&gt;
&lt;P&gt;1. if the Branch is separated by - only show the string after the dash (ie DEEP CREEK, ALBERMARLE)&lt;/P&gt;
&lt;P&gt;2. if the Branch is not separated by - just show the string inside the "( )" only (ie closed)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FOOTNOTE&lt;/P&gt;
&lt;P&gt;for #2 also how would I do this if I just want to show the characters before the&amp;nbsp; ( ) or in this case MARSHALL SQUARE PUBLIX&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2020 19:01:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-manipulation-show-certain-characters-based-on-condition/m-p/615034#M179857</guid>
      <dc:creator>Q1983</dc:creator>
      <dc:date>2020-01-03T19:01:24Z</dc:date>
    </item>
    <item>
      <title>Re: String manipulation - show certain characters based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-manipulation-show-certain-characters-based-on-condition/m-p/615036#M179859</link>
      <description>&lt;P&gt;SCAN() can probably do what you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ln_no $ Branch $50. ;
datalines;
33 CHARLOTTE COUNTY - DEEP CREEK
22 CHASTAIN SQUARE PUBLIX (closed)
62 CHARLOTTESVILLE AREA - ALBEMARE
64 MARSHALL SQUARE PUBLIX (closed)
;

data want;
  set have;
  length want1 want2 $50;
  want1 = strip(coalescec(scan(branch,2,'-'),scan(branch,-2,'()')));
  want2 = strip(coalescec(scan(branch,2,'-'),scan(branch,1,'()')));
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    ln_no                Branch                 want1         want2

 1      33      CHARLOTTE COUNTY - DEEP CREEK      DEEP CREEK    DEEP CREEK
 2      22      CHASTAIN SQUARE PUBLIX (closed)    closed        CHASTAIN SQUARE PUBLIX
 3      62      CHARLOTTESVILLE AREA - ALBEMARE    ALBEMARE      ALBEMARE
 4      64      MARSHALL SQUARE PUBLIX (closed)    closed        MARSHALL SQUARE PUBLIX&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Jan 2020 19:14:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-manipulation-show-certain-characters-based-on-condition/m-p/615036#M179859</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-01-03T19:14:23Z</dc:date>
    </item>
    <item>
      <title>Re: String manipulation - show certain characters based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-manipulation-show-certain-characters-based-on-condition/m-p/615037#M179860</link>
      <description>&lt;P&gt;Below should do the job.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(drop=_:);
  infile datalines;
  input ln_no $ _str $50.;
  length branch  $20 branch2 $40;
  branch=scan(_str,2,'-');
  if missing(branch) then branch=scan(_str,-2,'()');
  branch2=scan(_str,1,'()-');
  datalines;
33 CHARLOTTE COUNTY - DEEP CREEK
22 CHASTAIN SQUARE PUBLIX (closed)
62 CHARLOTTESVILLE AREA - ALBEMARE
64 MARSHALL SQUARE PUBLIX (closed)
;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Jan 2020 19:20:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-manipulation-show-certain-characters-based-on-condition/m-p/615037#M179860</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-01-03T19:20:43Z</dc:date>
    </item>
    <item>
      <title>Re: String manipulation - show certain characters based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-manipulation-show-certain-characters-based-on-condition/m-p/615136#M179907</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/5629"&gt;@Q1983&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also use pearl functions to achieve that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines;
	input ln_no $ Branch $50.;

	if prxmatch('/(.*-)(.*)/', Branch) then Branch=prxchange('s/(.*-)(.*)/$2/', -1, Branch);
	else Branch=prxchange('s/(.*)(\(.*\))/$2/', -1, Branch); /* put $1 instead of $2 if you want to display the characters before the ( ) */&lt;BR /&gt;
	return;
	datalines;
33 CHARLOTTE COUNTY - DEEP CREEK
22 CHASTAIN SQUARE PUBLIX (closed)
62 CHARLOTTESVILLE AREA - ALBEMARE
64 MARSHALL SQUARE PUBLIX (closed)
;
run;

proc print;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Explanation :&lt;/P&gt;
&lt;P&gt;- prxmatch('/(&lt;FONT color="#FF6600"&gt;.&lt;/FONT&gt;&lt;FONT color="#0000FF"&gt;*&lt;/FONT&gt;&lt;FONT color="#99CC00"&gt;-&lt;/FONT&gt;)(&lt;FONT color="#FF6600"&gt;.&lt;/FONT&gt;&lt;FONT color="#0000FF"&gt;*&lt;/FONT&gt;)/',branch) looks for the pattern&amp;nbsp;(.&lt;FONT color="#0000FF"&gt;*&lt;/FONT&gt;-)(.&lt;FONT color="#0000FF"&gt;*&lt;/FONT&gt;), meaning any character (&lt;FONT color="#FF6600"&gt;.&lt;/FONT&gt;) zero, one or more times (&lt;FONT color="#0000FF"&gt;*&lt;/FONT&gt;), an hyphen (&lt;FONT color="#99CC00"&gt;-&lt;/FONT&gt;), followed by&amp;nbsp;any character (&lt;FONT color="#FF6600"&gt;.&lt;/FONT&gt;) zero, one or more times (&lt;FONT color="#0000FF"&gt;*&lt;/FONT&gt;). If the condition is true, then the prxchange function retrieves the second argument in parenthesis (&lt;FONT color="#FF00FF"&gt;$2&lt;/FONT&gt;), so the string after the hyphen (prxchange('s/(.*-)(.*)/&lt;FONT color="#FF00FF"&gt;$2&lt;/FONT&gt;/', -1, Branch).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- It is quite the same logic for the second use case: the prxchange function looks for the pattern&amp;nbsp;(&lt;FONT color="#FF6600"&gt;.&lt;/FONT&gt;&lt;FONT color="#0000FF"&gt;*&lt;/FONT&gt;)(&lt;FONT color="#999999"&gt;\(&lt;/FONT&gt;&lt;FONT color="#FF6600"&gt;.&lt;/FONT&gt;&lt;FONT color="#0000FF"&gt;*&lt;/FONT&gt;&lt;FONT color="#999999"&gt;\)&lt;/FONT&gt;),&amp;nbsp;meaning any character (&lt;FONT color="#FF6600"&gt;.&lt;/FONT&gt;) zero, one or more times (&lt;FONT color="#0000FF"&gt;*&lt;/FONT&gt;), followed by&amp;nbsp;any character (&lt;FONT color="#FF6600"&gt;.&lt;/FONT&gt;) zero, one or more times (&lt;FONT color="#0000FF"&gt;*&lt;/FONT&gt;) in parenthesis &lt;FONT color="#999999"&gt;\(&lt;/FONT&gt; and&amp;nbsp;&lt;FONT color="#999999"&gt;\)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;If you want to display the characters before the parenthesis, you can use $1, meaning the first group in parenthesis.&lt;/P&gt;</description>
      <pubDate>Sat, 04 Jan 2020 13:56:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-manipulation-show-certain-characters-based-on-condition/m-p/615136#M179907</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-01-04T13:56:16Z</dc:date>
    </item>
    <item>
      <title>Re: String manipulation - show certain characters based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-manipulation-show-certain-characters-based-on-condition/m-p/615142#M179913</link>
      <description>&lt;P&gt;Appreciate the explanation&lt;/P&gt;</description>
      <pubDate>Sat, 04 Jan 2020 16:31:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-manipulation-show-certain-characters-based-on-condition/m-p/615142#M179913</guid>
      <dc:creator>Q1983</dc:creator>
      <dc:date>2020-01-04T16:31:13Z</dc:date>
    </item>
  </channel>
</rss>

