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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 848 views
  • 1 like
  • 3 in conversation