Help with PERL string count for address var

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 123
Accepted Solution

Help with PERL string count for address var

hi,

i'm looking to count how many words contain 1 or more numeric characters (for address variable).

 

 

The following code almost does it: prxparse('/\d{1}\b/'), derived from a very useful suggestion from Ksharp in prior post.

 

But it needs another enhancement: in example below, all words are properly counted, Except record (i) [which should be 3], where note the 101A in (i) is reverse of A101 in (ii).

 

data have;
input;
have=_infile_;
pid=prxparse('/\d{1}\b/');
s=1;e=length(_infile_);
n=0;
call prxnext(pid,s,e,_infile_,p,l);
do while(p>0);
 n+1;
 call prxnext(pid,s,e,_infile_,p,l);
end;
keep have n;
cards;
()  189 ELIOT STREET | UNIT 112 | ROCKLAND | ON | K4K0G4 | CAN
(i)  1769 101A AVE | SURREY | BC | V4N5V8 | CAN
(ii) 1769 A101 AVE | SURREY | BC | V4N5V8 | CAN
(iii) 204 ALGONQUIN RD UNIT114 
(iv) BUREAU N2P1G1  106
(v) 29-549 RGE RD 232  STURGEON COUNTY  AB  T8L5E9  CAN
;
run;

 

A word boundary:

-can be preceded, or ended by 0 characters (starting or end characters within variable)

-can be preceded, or ended by 1 or more spaces

-word containing a hyphen to be considered two words 

 

Thank you


Accepted Solutions
Solution
‎04-06-2018 11:13 AM
Trusted Advisor
Posts: 1,318

Re: Help with PERL string count for address var

data want;
infile cards dlm='|' truncover;
input @') ' address : $256.;
num=0;
do _n_=1 to countw(address,' -');
num+anydigit(scan(address,_n_,' -'))>0;
end;
cards;
()  189 ELIOT STREET | UNIT 112 | ROCKLAND | ON | K4K0G4 | CAN
(i)  1769 101A AVE | SURREY | BC | V4N5V8 | CAN
(ii) 1769 A101 AVE | SURREY | BC | V4N5V8 | CAN
(iii) 204 ALGONQUIN RD UNIT114 
(iv) BUREAU N2P1G1  106
(v) 29-549 RGE RD 232  STURGEON COUNTY  AB  T8L5E9  CAN
;
run;

View solution in original post


All Replies
Solution
‎04-06-2018 11:13 AM
Trusted Advisor
Posts: 1,318

Re: Help with PERL string count for address var

data want;
infile cards dlm='|' truncover;
input @') ' address : $256.;
num=0;
do _n_=1 to countw(address,' -');
num+anydigit(scan(address,_n_,' -'))>0;
end;
cards;
()  189 ELIOT STREET | UNIT 112 | ROCKLAND | ON | K4K0G4 | CAN
(i)  1769 101A AVE | SURREY | BC | V4N5V8 | CAN
(ii) 1769 A101 AVE | SURREY | BC | V4N5V8 | CAN
(iii) 204 ALGONQUIN RD UNIT114 
(iv) BUREAU N2P1G1  106
(v) 29-549 RGE RD 232  STURGEON COUNTY  AB  T8L5E9  CAN
;
run;
Super User
Posts: 10,788

Re: Help with PERL string count for address var

"V4N5V8V "  should be count  as 1 or 0 ?

 

Frequent Contributor
Posts: 123

Re: Help with PERL string count for address var

hi Ksharp... it is 1 in my example (although a zero could work, but should never be 2,or 3). I've struggled in perl with making count of example (ii) identical to count of (i) (and that is related to how /b function operates i believe
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 179 views
  • 0 likes
  • 3 in conversation