<?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 Issue with replacing string within a string in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Issue-with-replacing-string-within-a-string/m-p/11287#M1296</link>
    <description>I have a data set that has a subject variable.  This is based on email subjects.&lt;BR /&gt;
The issue I'm having is that I have to find a specific string within this variable but the string isn't consistent.  The string 'Inquiry#' is what i'm looking for but it has to have 1 or 2 numbers following it.  For ex Inquiry#1 or Inquiry#12.  &lt;BR /&gt;
&lt;BR /&gt;
I need the string before and after this string.  I have done this so far but I feel I could have done it much cleaner.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data inq_2 ;&lt;BR /&gt;
	set inq ;&lt;BR /&gt;
&lt;BR /&gt;
	subject_ = compress(subject) ;&lt;BR /&gt;
	inq_pos = find(subject_ , 'Inquiry#', 'i') ;&lt;BR /&gt;
	inq_str = substr(subject_ , inq_pos) ;&lt;BR /&gt;
	inq_dash_pos = find(inq_str, '-') ;  /*A dash does not always follow.  Needs fix */&lt;BR /&gt;
&lt;BR /&gt;
	if inq_pos = 1 then x = substr(subject_ , inq_dash_pos) ;	&lt;BR /&gt;
	else x = substr(subject_,1,inq_pos -1) || ' ' || substr(subject_, (inq_pos + inq_dash_pos) - 1) ;&lt;BR /&gt;
	&lt;BR /&gt;
run ;&lt;BR /&gt;
&lt;BR /&gt;
Thanks for any help</description>
    <pubDate>Fri, 10 Jun 2011 18:10:47 GMT</pubDate>
    <dc:creator>jerry898969</dc:creator>
    <dc:date>2011-06-10T18:10:47Z</dc:date>
    <item>
      <title>Issue with replacing string within a string</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Issue-with-replacing-string-within-a-string/m-p/11287#M1296</link>
      <description>I have a data set that has a subject variable.  This is based on email subjects.&lt;BR /&gt;
The issue I'm having is that I have to find a specific string within this variable but the string isn't consistent.  The string 'Inquiry#' is what i'm looking for but it has to have 1 or 2 numbers following it.  For ex Inquiry#1 or Inquiry#12.  &lt;BR /&gt;
&lt;BR /&gt;
I need the string before and after this string.  I have done this so far but I feel I could have done it much cleaner.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data inq_2 ;&lt;BR /&gt;
	set inq ;&lt;BR /&gt;
&lt;BR /&gt;
	subject_ = compress(subject) ;&lt;BR /&gt;
	inq_pos = find(subject_ , 'Inquiry#', 'i') ;&lt;BR /&gt;
	inq_str = substr(subject_ , inq_pos) ;&lt;BR /&gt;
	inq_dash_pos = find(inq_str, '-') ;  /*A dash does not always follow.  Needs fix */&lt;BR /&gt;
&lt;BR /&gt;
	if inq_pos = 1 then x = substr(subject_ , inq_dash_pos) ;	&lt;BR /&gt;
	else x = substr(subject_,1,inq_pos -1) || ' ' || substr(subject_, (inq_pos + inq_dash_pos) - 1) ;&lt;BR /&gt;
	&lt;BR /&gt;
run ;&lt;BR /&gt;
&lt;BR /&gt;
Thanks for any help</description>
      <pubDate>Fri, 10 Jun 2011 18:10:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Issue-with-replacing-string-within-a-string/m-p/11287#M1296</guid>
      <dc:creator>jerry898969</dc:creator>
      <dc:date>2011-06-10T18:10:47Z</dc:date>
    </item>
    <item>
      <title>Re: Issue with replacing string within a string</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Issue-with-replacing-string-within-a-string/m-p/11288#M1297</link>
      <description>Jerry,&lt;BR /&gt;
&lt;BR /&gt;
You didn't provide any sample data, or expected results, thus one can only guess.  I'm sure some regex experts can come up with something better, but the following appears to come close to what you want:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data inq;&lt;BR /&gt;
  informat subject $60.;&lt;BR /&gt;
  input subject &amp;amp;;&lt;BR /&gt;
  cards;&lt;BR /&gt;
Fake subject no context&lt;BR /&gt;
Another fake subject Inquiry#1 but not sure&lt;BR /&gt;
Yet another fake subject Inquiry#12&lt;BR /&gt;
Yet another fake subject Inquiry#13 but this is more&lt;BR /&gt;
This line doesn't have Inquiry# followed by any digit(s)&lt;BR /&gt;
;&lt;BR /&gt;
data inq_2 ;&lt;BR /&gt;
  set inq ;&lt;BR /&gt;
  inq_pos = index(subject , 'Inquiry#') ;&lt;BR /&gt;
  if inq_pos gt 0 then do;&lt;BR /&gt;
    str1=substr(subject,1,inq_pos-1);&lt;BR /&gt;
	if anydigit(substr(subject,inq_pos+8,1)) then&lt;BR /&gt;
	 str2=substr(subject,inq_pos+9);&lt;BR /&gt;
    else str1="";&lt;BR /&gt;
	if anydigit(substr(subject,inq_pos+9,1)) then&lt;BR /&gt;
	 str2=substr(subject,inq_pos+10);&lt;BR /&gt;
  end;&lt;BR /&gt;
run ;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
HTH,&lt;BR /&gt;
Art&lt;BR /&gt;
---------&lt;BR /&gt;
&amp;gt; I have a data set that has a subject variable.  This&lt;BR /&gt;
&amp;gt; is based on email subjects.&lt;BR /&gt;
&amp;gt; The issue I'm having is that I have to find a&lt;BR /&gt;
&amp;gt; specific string within this variable but the string&lt;BR /&gt;
&amp;gt; isn't consistent.  The string 'Inquiry#' is what i'm&lt;BR /&gt;
&amp;gt; looking for but it has to have 1 or 2 numbers&lt;BR /&gt;
&amp;gt; following it.  For ex Inquiry#1 or Inquiry#12.  &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; I need the string before and after this string.  I&lt;BR /&gt;
&amp;gt; have done this so far but I feel I could have done it&lt;BR /&gt;
&amp;gt; much cleaner.&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; data inq_2 ;&lt;BR /&gt;
&amp;gt; 	set inq ;&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; 	subject_ = compress(subject) ;&lt;BR /&gt;
&amp;gt; 	inq_pos = find(subject_ , 'Inquiry#', 'i') ;&lt;BR /&gt;
&amp;gt; 	inq_str = substr(subject_ , inq_pos) ;&lt;BR /&gt;
&amp;gt; inq_dash_pos = find(inq_str, '-') ;  /*A dash does&lt;BR /&gt;
&amp;gt; s not always follow.  Needs fix */&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; if inq_pos = 1 then x = substr(subject_ ,&lt;BR /&gt;
&amp;gt; , inq_dash_pos) ;	&lt;BR /&gt;
&amp;gt; else x = substr(subject_,1,inq_pos -1) || ' ' ||&lt;BR /&gt;
&amp;gt; | substr(subject_, (inq_pos + inq_dash_pos) - 1) ;&lt;BR /&gt;
&amp;gt; 	&lt;BR /&gt;
&amp;gt; run ;&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; Thanks for any help</description>
      <pubDate>Fri, 10 Jun 2011 23:40:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Issue-with-replacing-string-within-a-string/m-p/11288#M1297</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2011-06-10T23:40:11Z</dc:date>
    </item>
    <item>
      <title>Re: Issue with replacing string within a string</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Issue-with-replacing-string-within-a-string/m-p/11289#M1298</link>
      <description>Agree with Art: Sounds like RegEx. Here an approach:&lt;BR /&gt;
&lt;BR /&gt;
Having read your post once more I was no more sure how exactly the result should look in the end. So below a collection of possibilities. Hope that one will be suitable for your case.&lt;BR /&gt;
&lt;BR /&gt;
data want;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;set have;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;length StringBefore StringAfter $60;&lt;BR /&gt;
&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;/* Word before and after Inquiry#&amp;lt; digits &amp;gt; */&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;retain RegExId;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;drop RegExId;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;if _n_=1 then RegExId=prxparse('/(\S*)\s*(\bInquiry#\d{1,2}\b)\s*(\S*)/o');&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;if prxmatch(RegExId,subject) then&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;do;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;StringBefore=prxposn(RegExId,1,subject);&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;StringAfter=prxposn(RegExId,3,subject);&lt;BR /&gt;
/*    put _n_= @10 StringBefore= @50 StringAfter=;*/&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;end;&lt;BR /&gt;
&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;/* String before and after Inquiry#&amp;lt; digits &amp;gt;*/&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;retain RegExId2;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;drop RegExId2;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;if _n_=1 then RegExId2=prxparse('/(.*)(Inquiry#\d{1,2})(.*)/o');&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;if prxmatch(RegExId2,subject) then&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;do;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;StringBefore=prxposn(RegExId2,1,subject);&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;StringAfter=prxposn(RegExId2,3,subject);&lt;BR /&gt;
/*    put _n_= @10 StringBefore= @50 StringAfter=;*/&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;end;&lt;BR /&gt;
&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;/* Just remove Inquiry#&amp;lt; digits &amp;gt; in string */&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;subject=prxchange('s/(.*)(\bInquiry#\d{1,2}\b)(.*)/$1$3/o',1,subject);&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;put subject=;&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: Patrick</description>
      <pubDate>Sat, 11 Jun 2011 07:08:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Issue-with-replacing-string-within-a-string/m-p/11289#M1298</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2011-06-11T07:08:45Z</dc:date>
    </item>
    <item>
      <title>Re: Issue with replacing string within a string</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Issue-with-replacing-string-within-a-string/m-p/11290#M1299</link>
      <description>Art &amp;amp; Patrick,&lt;BR /&gt;
&lt;BR /&gt;
Thank you both and I do apologize for not giving test data.  &lt;BR /&gt;
I have yet to look at either since I was pulled away onto another project.  &lt;BR /&gt;
I will go with your suggestions and use the regex approach.  I think it will be the cleanest way to do it.&lt;BR /&gt;
&lt;BR /&gt;
Jerry</description>
      <pubDate>Mon, 13 Jun 2011 15:07:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Issue-with-replacing-string-within-a-string/m-p/11290#M1299</guid>
      <dc:creator>jerry898969</dc:creator>
      <dc:date>2011-06-13T15:07:58Z</dc:date>
    </item>
    <item>
      <title>Re: Issue with replacing string within a string</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Issue-with-replacing-string-within-a-string/m-p/11291#M1300</link>
      <description>Hi Jerry,&lt;BR /&gt;
&lt;BR /&gt;
/* Test data*/&lt;BR /&gt;
data test;	&lt;BR /&gt;
	length a $200.;&lt;BR /&gt;
	a="Inquiry#1"; output;&lt;BR /&gt;
	a="Inquiry#12"; output;&lt;BR /&gt;
	a="123Inquiry#12"; output;&lt;BR /&gt;
	a="111IInquiry#12"; output;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data test1;&lt;BR /&gt;
	set test;&lt;BR /&gt;
	if index(a,'Inquiry#') ne 0 then do;&lt;BR /&gt;
		if index(a,'Inquiry#') gt 1 then bf=substr(a,1,index(a,'Inquiry#')-1); /* Stores before that string(Inquiry#)*/&lt;BR /&gt;
		af=substr(a,index(a,'Inquiry#')+8); /* Stores after that string(Inquiry#)*/&lt;BR /&gt;
	end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
This may be better option for you...&lt;BR /&gt;
&lt;BR /&gt;
Thanks</description>
      <pubDate>Mon, 13 Jun 2011 16:20:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Issue-with-replacing-string-within-a-string/m-p/11291#M1300</guid>
      <dc:creator>sas_new</dc:creator>
      <dc:date>2011-06-13T16:20:42Z</dc:date>
    </item>
  </channel>
</rss>

