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

How do I extract the first 5 words of a string? I have two address variables and I want to say "if first 5 words of address1=address2"

 

Thanks in advandced!

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

One option of how to address this. The code will return match='Y' if the first 5 words are the same. Not case sensitive and less than 5 words in string allowed.

 

data sample;
  length address1 address2 $60;
  retain _prxid;
  if _n_=1 then
    _prxid=prxparse('s/^\s*(\w*)\s+(\w*)\s+(\w*)\s+(\w*)\s+(\w*)\s*.*/\U\1\2\3\4\5/o');
  address1="4478 English Elm River Road unit 2";
  address2="4478 English ELm River Road unit 3";
  if prxchange(_prxid,1,address1)=prxchange(_prxid,1,address2) then
    match='Y';
  else match='N';
run;

View solution in original post

9 REPLIES 9
PGStats
Opal | Level 21

What kind of "words" and "strings" are we talking about?

PG
tropical_surfer
Fluorite | Level 6

An address like address1="4478 English Elm River Road unit 2" and address2="4478 English Elm River Road unit 3".

 

I want to check that the first 5 words (i.e."4478 English Elm River Road") is the same in both address1 and address2.

Reeza
Super User

Will it always be 5 words or is that variable? Will you have addresses with less than 5 words?

 

Have you looked at compged or soundex functions?

PGStats
Opal | Level 21

How about a function that counts the matching length, in words :

 

proc fcmp outlib=sasuser.fcmp.character;
function matchWords(str1 $, str2 $);
j =  min(countw(str1), countw(str2));
do i = j to 1 by -1;
    if upcase(scan(str1, i)) ne upcase(scan(str2, i)) then j = i-1;
    end;
return (j);
endsub;
run;
quit;

options cmplib=sasuser.fcmp;

data have;
address1="4478 English Elm River Road unit 2"; 
address2="4478 English Elm River Road unit 3";
m = matchwords(address1, address2);
put _all_;
run;
PG
Patrick
Opal | Level 21

One option of how to address this. The code will return match='Y' if the first 5 words are the same. Not case sensitive and less than 5 words in string allowed.

 

data sample;
  length address1 address2 $60;
  retain _prxid;
  if _n_=1 then
    _prxid=prxparse('s/^\s*(\w*)\s+(\w*)\s+(\w*)\s+(\w*)\s+(\w*)\s*.*/\U\1\2\3\4\5/o');
  address1="4478 English Elm River Road unit 2";
  address2="4478 English ELm River Road unit 3";
  if prxchange(_prxid,1,address1)=prxchange(_prxid,1,address2) then
    match='Y';
  else match='N';
run;
tropical_surfer
Fluorite | Level 6

Life saver!!!! Thank you so much, works perfect! 

 

I have another similar question you might know the solution to. How can I write code to check that address1 equals (is identical) to (address2 minus the last 2 words).

 

Like 

address1=123 main st

address2= 123 main st bldg A

Reeza
Super User

You may have to register and log in to see this, but someone posted a macro to implement USPS abbreviations if that's helpful at all.

 

https://listserv.uga.edu/cgi-bin/wa?A2=ind1606c&L=SAS-L&X=A8B00C8C9C39792B2F&Y=fkhurshed%40gmail.com...

 

Ksharp
Super User
data _null_;
address1="4478 English Elm River Road unit 2"; 
address2="4478 English Elm River Road unit 3";
call scan(address1,6,p1,l1,' ');
call scan(address2,6,p2,l2,' ');
five1=substr(address1,1,p1-2);
five2=substr(address2,1,p2-2);
if five1=five2 then put 'YES';
 else put 'NO';
run;

Patrick
Opal | Level 21

And what happens if the address string is shorter?

  address1="4478 English Elm Road";
  address2="9999 English Elm Road";

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
  • 9 replies
  • 5669 views
  • 3 likes
  • 5 in conversation