Help using Base SAS procedures

Verify Field is numeric

Reply
Super Contributor
Posts: 398

Verify Field is numeric

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
Super Contributor
Posts: 359

Re: Verify Field is numeric

You could do something like:
if zip ne put(input(zip, best.), z5.) then *FLAG ERROR*
Super Contributor
Posts: 398

Re: Verify Field is numeric

Flip,
Thank you so much that helped.

Jerry
Super Contributor
Super Contributor
Posts: 3,174

Re: Verify Field is numeric

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.
Super Contributor
Posts: 474

Re: Verify Field is numeric

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.
Super Contributor
Posts: 398

Re: Verify Field is numeric

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
Ask a Question
Discussion stats
  • 5 replies
  • 134 views
  • 0 likes
  • 4 in conversation