BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cmoore
Obsidian | Level 7

Hi,

 

I am wanting to seperate out client names from a lengthy bit of text based on keywords that they come after. The code that I have been using in dev is below:

 

data have;
infile cards truncover;
input have_str $500.;
cards;
Firm A advises on the Volkswagen settlement with US authorities regarding emissions from diesel engines Firm A advises The Carlyle Group on its $3.2bn acquisition of Atotech advising Coca Cola
;
run;

data want;
set have;
retain _prxid;

/*advised*/
if _n_=1 then
_prxid=prxparse('/(\badvised)[^A-Z]{0,10}(([A-Z]\w+\s*\b)+)/');
if prxmatch(_prxid, have_str) then do;
client1=prxposn(_prxid, 2, have_str);
end;;

/*advises*/
if _n_=1 then
_prxid=prxparse('/(\badvises)[^A-Z]{0,10}(([A-Z]\w+\s*\b)+)/');
if prxmatch(_prxid, have_str) then do;
client2=prxposn(_prxid, 2, have_str);
end;;

/*advising*/
if _n_=1 then
_prxid=prxparse('/(\badvising)[^A-Z]{0,10}(([A-Z]\w+\s*\b)+)/');
if prxmatch(_prxid, have_str) then do;
client3=prxposn(_prxid, 2, have_str);
end;;
run;

 

There are multiple keywords such as "advising" or "advised" that I'm interested in. When the same keyword "advises" appears more than once in the same sentence then the data isn't populated. Any ideas please?

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
infile cards truncover ;
input have_str $500.;
cards;
Firm A advises on the Volkswagen settlement with US authorities regarding emissions from diesel engines Firm A advises The Carlyle Group on its $3.2bn acquisition of Atotech advising Coca Cola
;
run;

data want;
 set have;
 temp=have_str;
 p=prxmatch('/\b(advises|advised|advising)\b/i',temp);

 do while(p);
  temp=substr(temp,p);
  temp=substr(temp,findc(temp,' ')+1);
  name=catx(' ',scan(temp,1,' '),scan(temp,2,' '),scan(temp,3,' '));
  output;
  p=prxmatch('/\b(advises|advised|advising)\b/i',temp);
 end;

 drop temp p;
 run;

View solution in original post

6 REPLIES 6
cmoore
Obsidian | Level 7

The 3 outputs I want are; Client1=Volkswaggen, Client2=The Caryle Group and Client3=Coca Cola

Ksharp
Super User
data have;
infile cards truncover ;
input have_str $500.;
cards;
Firm A advises on the Volkswagen settlement with US authorities regarding emissions from diesel engines Firm A advises The Carlyle Group on its $3.2bn acquisition of Atotech advising Coca Cola
;
run;

data want;
 set have;
 temp=have_str;
 p=prxmatch('/\b(advises|advised|advising)\b/i',temp);

 do while(p);
  temp=substr(temp,p);
  temp=substr(temp,findc(temp,' ')+1);
  name=catx(' ',scan(temp,1,' '),scan(temp,2,' '),scan(temp,3,' '));
  output;
  p=prxmatch('/\b(advises|advised|advising)\b/i',temp);
 end;

 drop temp p;
 run;
FriedEgg
SAS Employee
filename ft15f001 temp;
data have;
infile ft15f001;
input text $194.;
parmcards;
firm a advises on the volkswagen settlement with us authorities regarding  emissions from diesel engines firm a advises the carlyle group on its  $3.2bn acquisition of atotech advising coca cola
;
run;

data want;
	prxid=prxparse('/\badvis(?:es|ed|ing)\b\s+((\b\w+\b\s*){1,3})/i');
	do until (eof);
		set have end=eof;
		start=1;
		stop=length(text);
		call prxnext(prxid, start, stop, text, position, length);
		do while (position > 0);
			call prxposn(prxid, 1, start, length);
			match=substr(text, start, length);
			output;
			call prxnext(prxid, start, stop, text, position, length);
		end;
	end;
run;
cmoore
Obsidian | Level 7

Hi,

 

Thanks for the solutions @Ksharp you have sent me they are very useful. Just a final question in this area. In some data cells I have the following raw data:

 

"Deutsche Bank, Bank of America, Merrill Lynch and Morgan Stanley on National Grid's £3.3 billion equity offering"

 

They have no keywords associated with them such as 'advised' like the example. I am wanting to pull out of the data 'Deutsche Bank', 'Bank of America', 'Merrill Lynch' and 'Morgan Stanley' in exactly the same way as your solution. I'm not interested in obtaining 'National Grid'. Basically a firm has worked with the 4 firms I have highlighted but not National Grid. Are there any rules I can apply for any such scenarios that may come up in my data?

 

Thanks for all your help.


Chris

cmoore
Obsidian | Level 7

Or where the keywords such as 'Advised' etc don't exist take start of sentence to the words "on" to isolate the clients I'm interested in. That's what I'm trying to embed into the current syntax you sent me

Ksharp
Super User

OK. I don't know if the following could work.

 



data have;
infile cards truncover ;
input have_str $500.;
cards;
Firm A advises on the Volkswagen settlement with US authorities regarding emissions from diesel engines Firm A advises The Carlyle Group on its $3.2bn acquisition of Atotech advising Coca Cola
Deutsche Bank, Bank of America, Merrill Lynch and Morgan Stanley on National Grid's £3.3 billion equity offering
;
run;

data want;
 set have;
 temp=have_str;
 p=prxmatch('/\b(advises|advised|advising)\b/i',temp);
if p=0 then do;
   name=substr(temp,1,prxmatch('/\bon\b/i',temp)-1);
   output;
end;
else do;
 do while(p);
  temp=substr(temp,p);
  temp=substr(temp,findc(temp,' ')+1);
  name=catx(' ',scan(temp,1,' '),scan(temp,2,' '),scan(temp,3,' '));
  output;
  p=prxmatch('/\b(advises|advised|advising)\b/i',temp);
 end;
end;
 drop temp p;
 run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1177 views
  • 2 likes
  • 3 in conversation