DATA Step, Macro, Functions and more

Using PCRXFIND with DS2

Reply
Occasional Contributor
Posts: 11

Using PCRXFIND with DS2

[ Edited ]

I want to replicate this using PCRXFIND instead of prxmatch for speed and because prxmatch is not working correctly.

 

I expect the following to match and I want the code to use PCRXFIND not prxmatch.

 

John Sales
Mary Acctng                 Act
Joe Findme                  Findme
Sue Hereiam                 Hereiam

 

I am trying to find text in a data file.  Here is a reproducible example.  There is one file with the data to search and another with the search terms.  I want to use regular expressions.  Normally this is done with prxmatch.  I want to do it with PCRXFIND because I am working inside DS2.  I use a hash iterator to iterate through the search terms or each observation in the data set.

 

The search result do not follow any pattern I can recognize.

 

* ds2 PCRXFIND example;

data person;
	input name $ dept $;
	datalines;
John Sales
Mary Acctng
Joe Findme
Sue Hereiam
;
run;


data searchterms;
	infile datalines missover;
	input s_index $ term $;
datalines;
1	Hereiam
2	Findme
3   Acc
;
run;

proc contents data=searchterms; run;
proc print data=searchterms; run;

proc ds2;
	data search_results (overwrite=yes);
	
	dcl double rc c ;
	declare char(8) s_index;
	declare char(8) term;
	declare char(11) name dept;
	declare char(1) c_options;
	declare char(20) search_term search_text;
	dcl package hash h(1, '{select s_index, term from searchterms}');
	dcl package hiter hi('h');
		method init();
			c_options = 'i';
			rc = h.defineKey('s_index');
			rc = h.defineData('term');
			rc = h.defineDone();
		end;
		method run();
			dcl double rc;
			set {select  name, dept from person};
			rc = hi.first();
			do while(rc=0);
				c = prxmatch('/'||compress(term)||'/i',name||'   '||dept);
				search_term = '/'||compress(term)||'/i';
				search_text = name||'   '||dept;
				rc = hi.next();
				output;

			end;
		end;
	enddata;
run;
quit;

 

 

Regular Contributor
Posts: 213

Re: Using PCRXFIND with DS2

Posted in reply to harlananelson

"and because prxmatch is not working correctly." - can you explain this? Never had any problems with prxmatch as long as I used trim on the second argument.

Occasional Contributor
Posts: 11

Re: Using PCRXFIND with DS2

Posted in reply to error_prone

If you run the code I posted, the matched lines don't correspond to the search terms.  I save the search terms in the data set and it is clear I don't need the trim the second term. But I never trim the string to be searched (second term).  Sometimes I trim the search term (first term).

Super User
Posts: 13,556

Re: Using PCRXFIND with DS2

Posted in reply to harlananelson

Perhaps it would help to show what you expect or desire to see given that example data.

Occasional Contributor
Posts: 11

Re: Using PCRXFIND with DS2

Ok, I made that more explicit.  I want the solution to use PCRXFIND.

Ask a Question
Discussion stats
  • 4 replies
  • 117 views
  • 1 like
  • 3 in conversation