how to screen zipcodes

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 122
Accepted Solution

how to screen zipcodes

Hi there, I have a dataset with a variable zipcode. zipcode variable includes some Canadian zipcodes which have letters as well as numbers. I want to screen out these Canadian zipcodes. But I am not sure how to do it.

What I want to do is to use where clause in data step that if there are letters in the zipcode, exclude them.

Also, there are many zipcodes that have nine digits instead of five. I want to exclude the last four digits too. Some examples are as below. Please help. Thanks.

zip

L4B 1J8

60532-3622

10022-4614


Accepted Solutions
Solution
‎08-07-2014 10:09 PM
Respected Advisor
Posts: 3,156

Re: how to screen zipcodes

Try this:

data have;

input zip &$ 20.;

length new_zip $ 5;

new_zip=ifc(anyalpha(zip)>0,'.',compress(scan(zip,1,'-')));

cards;

zip

35232

L4B 1J8

60532-3622

10022-4614

943041112

60 532-3622

;


Haikuo

View solution in original post


All Replies
Respected Advisor
Posts: 3,156

Re: how to screen zipcodes

please exhaust all of your scenario, otherwise:

data have;

input zip &$ 20.;

new_zip=ifc(anyalpha(zip)>0,'',scan(zip,1,'-'));

if not missing(new_zip);

cards;

zip

35232

L4B 1J8

60532-3622

10022-4614

;

Good luck,

Haikuo

Frequent Contributor
Posts: 122

Re: how to screen zipcodes

Thank you for your help. I just closely checked my zip codes data. There are some other cases that need attention. There are few that are nine digits without a '-'. Also, some zipcodes were mistyped with a space in between. Some examples are as below

943041112

60 532-3622

Also, if I don't want to remove those zips with letter charaters in them, but just want to use "." to replace it. How?

Thanks again.

Solution
‎08-07-2014 10:09 PM
Respected Advisor
Posts: 3,156

Re: how to screen zipcodes

Try this:

data have;

input zip &$ 20.;

length new_zip $ 5;

new_zip=ifc(anyalpha(zip)>0,'.',compress(scan(zip,1,'-')));

cards;

zip

35232

L4B 1J8

60532-3622

10022-4614

943041112

60 532-3622

;


Haikuo

Trusted Advisor
Posts: 1,228

Re: how to screen zipcodes

data have;

input zip &$ 10.;

new_zip=compress(scan(zip,1,'-'),,'kd');

cards;

L4B 1J8

60532-3622

10022-4614

;

Frequent Contributor
Posts: 122

Re: how to screen zipcodes

I want to mark the first one to missing instead of 418.

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 261 views
  • 7 likes
  • 3 in conversation