DATA Step, Macro, Functions and more

How to Substring Variable from one word to another in middle of string ?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

How to Substring Variable from one word to another in middle of string ?

Hi,

 

I want to subset string between words 'user/utilisateur' and 'du/in' . Considering English and French both.

 

I have variable 'Description' with following values:


DECSRIPTION:

Attribué à utilisateur Ali Khan du groupe Quebec Property 2

Assigned to user Marie-Helene Adams in group CAS

Assigned to user Jeff Mills in group National Personal Lines Auto 3

Assigned to user Wendy Taylor-Wu in group NRU Auto/Casualty

Attribué à utilisateur Laurence A. Weinstein du groupe Quebec 1

 

 

What i want - variable Name with value:

Ali Khan

Marie-Helene Adams

Jeff Mills

Wendy Taylor-Wu

Laurence A. Weinstein

 

 

Thank you!


Accepted Solutions
Solution
2 weeks ago
PROC Star
Posts: 8,117

Re: How to Substring Variable from one word to another in middle of string ?

Posted in reply to vpgodbole

Here is one way:

data want (drop=start end);
  set have;
   if findw(DECSRIPTION,'user') then start=
    findw(DECSRIPTION,'user') +5;
   else start=findw(DECSRIPTION,'utilisateur') +12;
  end=max(findw(DECSRIPTION,'du'),findw(DECSRIPTION,'in')) -1;
  name=substr(DECSRIPTION,start,end-start);
run;

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
Solution
2 weeks ago
PROC Star
Posts: 8,117

Re: How to Substring Variable from one word to another in middle of string ?

Posted in reply to vpgodbole

Here is one way:

data want (drop=start end);
  set have;
   if findw(DECSRIPTION,'user') then start=
    findw(DECSRIPTION,'user') +5;
   else start=findw(DECSRIPTION,'utilisateur') +12;
  end=max(findw(DECSRIPTION,'du'),findw(DECSRIPTION,'in')) -1;
  name=substr(DECSRIPTION,start,end-start);
run;

Art, CEO, AnalystFinder.com

 

Super User
Posts: 22,875

Re: How to Substring Variable from one word to another in middle of string ?

Posted in reply to vpgodbole

If all your data is this structured, use INDEXW to find the location of the words and that will give you the positions of the string you want to extract. Make sure to either upper case or lower case the comparisons.

 

 

Super User
Posts: 10,626

Re: How to Substring Variable from one word to another in middle of string ?

Posted in reply to vpgodbole
data have;
input x $80.;
s=prxmatch('/utilisateur|user/',x);
e=prxmatch('/du groupe|in group/',x);
temp=substr(x,s,e-s);
want=substr(temp,findc(temp,' '));
drop s e temp;
cards;
Attribué à utilisateur Ali Khan du groupe Quebec Property 2
Assigned to user Marie-Helene Adams in group CAS
Assigned to user Jeff Mills in group National Personal Lines Auto 3
Assigned to user Wendy Taylor-Wu in group NRU Auto/Casualty
Attribué à utilisateur Laurence A. Weinstein du groupe Quebec 1
;
run;

proc print noobs;run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 88 views
  • 0 likes
  • 4 in conversation