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

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

1 ACCEPTED SOLUTION

Accepted Solutions
FriedEgg
SAS Employee
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

3 REPLIES 3
FriedEgg
SAS Employee
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;
Ksharp
Super User

"V4N5V8V "  should be count  as 1 or 0 ?

 

brulard
Pyrite | Level 9
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

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
  • 3 replies
  • 489 views
  • 0 likes
  • 3 in conversation