BookmarkSubscribeRSS Feed
jerry898969
Pyrite | Level 9
I have a zip field that is defined as character. I need to make it numeric to append it to another table. My problem is that there is a row with "?????" as the zip code. Can anyone suggest a good way to do this check and check to see if it's blank at the same time?

Thank you so much for any help

Jerry
5 REPLIES 5
Flip
Fluorite | Level 6
You could do something like:
if zip ne put(input(zip, best.), z5.) then *FLAG ERROR*
jerry898969
Pyrite | Level 9
Flip,
Thank you so much that helped.

Jerry
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Unfortunately, ZIP_CODE could include zip+4 (character data, length up to 10), if the field length is greater than 5 (and any input validation allows it). And, so, it would be a gamble if/when you attempt to use PROC IMPORT and the first "GUESSINGROWS" values are all five-digit and then comes a zip+4 which would be invalid and the result would be a SAS MISSING value. As well, consider inclusion of other mail/postal codes which may occur any in an input file -- alpha-numeric strings, imbedded blanks likely.

Interesting challenge to consider when using smart tools/wizards for a manually-defined DATA step substitution.

Scott Barry
SBBWorks, Inc.
DanielSantos
Barite | Level 11
For some validations you may succeed by just using some simple validations functions ANYALPHA ANYDIGIT, ANYPUNCT, ANYSPACE, etc.

In this case, it is definitely a task for regular expression matching:

data OUT;
set IN;
EXPR='/^\d{5}([\-]\d{4})?$/'; * reg. expression match pattern for ZIP code;
REGX=prxparse(strip(EXPR)); * parse the expression;
OK=prxmatch(REGX,ZIP); * try the match for ZIP;
put ZIP= OK=; * print result;
run;

More info here:
http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/a002288677.htm
http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/a002291852.htm
http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/a002296115.htm

Also check the following tutorial:
http://www.regular-expressions.info/tutorial.html

Cheers from Portugal.

Daniel Santos @ www.cgd.pt.
jerry898969
Pyrite | Level 9
Daniel & Scott,
Thank you to both of you for your suggestions.

Scott I know for this zip field it is only 5 characters long or less. I have a separate column for the plus4 part of a zip code.

I used Flip's suggestion and got it to work but will have to revisit this situation more then likely in a day or so and find out why the source even has non numeric characters.

Thanks to everyone for all your help

Jerry

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1354 views
  • 0 likes
  • 4 in conversation