## last word from right

Solved
Super Contributor
Posts: 409

# last word from right

Hi,  I'm trying to find if there is a space after the last 3 digits then it's a canadian address otherwise it's US.

field

23 Yonge st.  M4H 6F7

23 Yonge st. 902910

827 Bla Bla Rd. T7G 7K2

Sammy Rd. 38278

where there is a postal code I'd say Canadian and 2 and 4 are US.. So I figured if there is a space after the last 3 digits from the right then it's  Can, otherwise US.

Thnks

Accepted Solutions
Solution
‎06-15-2017 09:46 PM
Posts: 1,147

## Re: last word from right

``````data want;
set have;
if prxmatch('m/\s\d\w\d\$/',strip(field)) then flag='Can';
else flag='US';
run;
``````
Thanks,
Jag

All Replies
Solution
‎06-15-2017 09:46 PM
Posts: 1,147

## Re: last word from right

``````data want;
set have;
if prxmatch('m/\s\d\w\d\$/',strip(field)) then flag='Can';
else flag='US';
run;
``````
Thanks,
Jag
PROC Star
Posts: 2,365

## Re: last word from right

[ Edited ]

You could use variations of:

Super User
Posts: 23,752

## Re: last word from right

A different option

1. Use COUNTW to determine the number of words

2. Find last word

3. If Length = 5 then it's US, otherwise, it's CAN.

The Perl is more efficient though

``````length country \$8.;

n_words = countw(text);
last_word = scan(text, n_words);

if length(last_word)=3 then country = 'CAN';
else if length(last_word)=5 then country='US';
else country='CHECKME';``````
PROC Star
Posts: 511

## Re: last word from right

something like this

data want;
set have;
else cntry='US';
run;

☑ This topic is solved.

Discussion stats
• 4 replies
• 364 views
• 3 likes
• 5 in conversation