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
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;
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;
"V4N5V8V
" should be count as 1 or 0 ?
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.