BookmarkSubscribeRSS Feed
jerry898969
Pyrite | Level 9
I have a data set that has a subject variable. This is based on email subjects.
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.

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.


data inq_2 ;
set inq ;

subject_ = compress(subject) ;
inq_pos = find(subject_ , 'Inquiry#', 'i') ;
inq_str = substr(subject_ , inq_pos) ;
inq_dash_pos = find(inq_str, '-') ; /*A dash does not always follow. Needs fix */

if inq_pos = 1 then x = substr(subject_ , inq_dash_pos) ;
else x = substr(subject_,1,inq_pos -1) || ' ' || substr(subject_, (inq_pos + inq_dash_pos) - 1) ;

run ;

Thanks for any help
4 REPLIES 4
art297
Opal | Level 21
Jerry,

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:
[pre]
data inq;
informat subject $60.;
input subject &;
cards;
Fake subject no context
Another fake subject Inquiry#1 but not sure
Yet another fake subject Inquiry#12
Yet another fake subject Inquiry#13 but this is more
This line doesn't have Inquiry# followed by any digit(s)
;
data inq_2 ;
set inq ;
inq_pos = index(subject , 'Inquiry#') ;
if inq_pos gt 0 then do;
str1=substr(subject,1,inq_pos-1);
if anydigit(substr(subject,inq_pos+8,1)) then
str2=substr(subject,inq_pos+9);
else str1="";
if anydigit(substr(subject,inq_pos+9,1)) then
str2=substr(subject,inq_pos+10);
end;
run ;
[/pre]
HTH,
Art
---------
> I have a data set that has a subject variable. This
> is based on email subjects.
> 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.
>
> 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.
>
>
> data inq_2 ;
> set inq ;
>
> subject_ = compress(subject) ;
> inq_pos = find(subject_ , 'Inquiry#', 'i') ;
> inq_str = substr(subject_ , inq_pos) ;
> inq_dash_pos = find(inq_str, '-') ; /*A dash does
> s not always follow. Needs fix */
>
> if inq_pos = 1 then x = substr(subject_ ,
> , inq_dash_pos) ;
> else x = substr(subject_,1,inq_pos -1) || ' ' ||
> | substr(subject_, (inq_pos + inq_dash_pos) - 1) ;
>
> run ;
>
> Thanks for any help
Patrick
Opal | Level 21
Agree with Art: Sounds like RegEx. Here an approach:

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.

data want;
  set have;
  length StringBefore StringAfter $60;

  /* Word before and after Inquiry#< digits > */
  retain RegExId;
  drop RegExId;
  if _n_=1 then RegExId=prxparse('/(\S*)\s*(\bInquiry#\d{1,2}\b)\s*(\S*)/o');
  if prxmatch(RegExId,subject) then
  do;
    StringBefore=prxposn(RegExId,1,subject);
    StringAfter=prxposn(RegExId,3,subject);
/* put _n_= @10 StringBefore= @50 StringAfter=;*/
  end;

  /* String before and after Inquiry#< digits >*/
  retain RegExId2;
  drop RegExId2;
  if _n_=1 then RegExId2=prxparse('/(.*)(Inquiry#\d{1,2})(.*)/o');
  if prxmatch(RegExId2,subject) then
  do;
    StringBefore=prxposn(RegExId2,1,subject);
    StringAfter=prxposn(RegExId2,3,subject);
/* put _n_= @10 StringBefore= @50 StringAfter=;*/
  end;

  /* Just remove Inquiry#< digits > in string */
  subject=prxchange('s/(.*)(\bInquiry#\d{1,2}\b)(.*)/$1$3/o',1,subject);
  put subject=;

run;


HTH
Patrick

Message was edited by: Patrick
jerry898969
Pyrite | Level 9
Art & Patrick,

Thank you both and I do apologize for not giving test data.
I have yet to look at either since I was pulled away onto another project.
I will go with your suggestions and use the regex approach. I think it will be the cleanest way to do it.

Jerry
sas_new
Calcite | Level 5
Hi Jerry,

/* Test data*/
data test;
length a $200.;
a="Inquiry#1"; output;
a="Inquiry#12"; output;
a="123Inquiry#12"; output;
a="111IInquiry#12"; output;
run;

data test1;
set test;
if index(a,'Inquiry#') ne 0 then do;
if index(a,'Inquiry#') gt 1 then bf=substr(a,1,index(a,'Inquiry#')-1); /* Stores before that string(Inquiry#)*/
af=substr(a,index(a,'Inquiry#')+8); /* Stores after that string(Inquiry#)*/
end;
run;


This may be better option for you...

Thanks

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 982 views
  • 0 likes
  • 4 in conversation