BookmarkSubscribeRSS Feed
harlananelson
Fluorite | Level 6

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;

 

 

4 REPLIES 4
error_prone
Barite | Level 11

"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.

harlananelson
Fluorite | Level 6

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).

ballardw
Super User

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

harlananelson
Fluorite | Level 6

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 1676 views
  • 1 like
  • 3 in conversation