Note that this page was written using SAS 9.1.3 for Windows.
SAS comes with many helpful datasets. One of these is called sashelp.zipcode, which contains information about city names, FIPS codes , ZIP code centroids (latitude and longitude coordinates) and more. This page will take you through the process of accessing and using this data set and ZIP code related functions.
Finding and updating the SAS ZIP code data file
First let’s figure out which version of the zip code data set is stored in the sashelp library.
proc contents data=sashelp.zipcode; run;The CONTENTS Procedure Data Set Name SASHELP.ZIPCODE Observations 41988 Member Type DATA Variables 16 Engine V9 Indexes 1 Created Wednesday, June 23, 2004 08:14:49 PM Observation Length 192 Last Modified Wednesday, June 23, 2004 08:14:49 PM Deleted Observations 0 Protection Compressed NO Data Set Type Sorted NO Label zipcodedownload.com April2004, UNIQUE-updated May 2004, Release 9.1.3 Data Representation WINDOWS_32 Encoding us-ascii ASCII (ANSI) <some output omitted> Alphabetic List of Variables and Attributes # Variable Type Len Format Informat Label 12 AREACODE Num 8 Area Code for ZIP Code. None for APO/FPO 5 CITY Char 35 Name of city/org 9 COUNTY Num 8 FIPS county code. Blank for APO/FPO 10 COUNTYNM Char 25 Name of county/parish. No county for APO/FPO 15 DST Char 1 ZIP Code obeys Daylight Savings: Y-Yes N-No 14 GMTOFFSE Num 8 Diff (hrs) between GMT and time zone for ZIP Code T 11 MSA Num 8 Metro Service Area code by common pop; no MSA for rural 16 PONAME Char 28 $28. $28. USPS Post Office Name 6 STATE Num 8 Two-digit number (FIPS code) for state/territory 7 STATECOD Char 2 Two-letter abbrev. for state name. E 8 STATENAM Char 25 Full name of state/territory E 13 TIMEZONE Char 9 Time Zone for ZIP Code. None for APO/FPO 3 X Num 8 11.6 Longitude (degrees) of the center (centroid) of ZIP Code. 0.0 for APO/FPO 2 Y Num 8 11.6 Latitude (degrees) of the center (centroid) of ZIP Code. 0.0 for APO/FPO 1 ZIP Num 8 Z5. The 5-digit ZIP Code 4 ZIP_ Char 1 ZIP Code Classification:M= APO/FPO P=PO Box CLASS U=Unique zip used for large orgs/businesses/bldgs Blank=Standard/non-unique
From the output of proc contents, we can tell that this version of zipcode data set was created in June of 2004, and it has 16 variables and 41,988 observations. Since SAS updates the zipcode dataset on a regular basis, it is very likely that our version is very out of date. Updates can be found here.
(At the time this page was created the latest version was created in October 2007.)
Clearly the version on file is out of date and needs to be updated. Follow the link above to the SAS page. You will need a SAS profile in order to download the current version. Once you have downloaded and unzipped the new file you will need to use proc cimport to
import the new data file. The file= statement tells SAS where the file you want to import is stored. The library= statement tells SAS where you want the new file to be read into.
After running the program the log file tells us what has happened running proc cimport. First make sure that there are no errors. Since the file was imported successfully the log file shows that the file zipcode_oct07.cpt actually contains three files. The new file that contains unique ZIP codes, zipcode_0704_unique, now contains 18 variables and 41,759 observations.
proc cimport file = 'C:datazipcode_oct07zipcode_oct07.cpt' library = work ; run;NOTE: Proc CIMPORT begins to create/update data set WORK.ZIPCODE_07Q4_UNIQUE NOTE: The data set index ZIP is defined. NOTE: Data set contains 18 variables and 41759 observations. Logical record length is 512 NOTE: Proc CIMPORT begins to create/update data set WORK.ZIPMIL_07Q4 NOTE: The data set index ZIP is defined. NOTE: Data set contains 18 variables and 539 observations. Logical record length is 512 NOTE: Proc CIMPORT begins to create/update data set WORK.ZIPMISC_07Q4 NOTE: The data set index ZIP is defined. NOTE: Data set contains 17 variables and 19 observations. Logical record length is 216
Now that the new file has been imported correctly let’s save a permanent copy of the old file before it is replaced. First we use the libname statement to define a permanent library where the old data files can be stored. Then we use a data step to place a copy of the old data file into the permanent library. Look at the log file to make sure it was copied successfully.
libname zip 'C:data'; data zip.march2004; set sashelp.zipcode; run;2098 libname zip 'C:data'; NOTE: Libref ZIP was successfully assigned as follows: Engine: V9 Physical Name: C:data 2099 data zip.march2004; 2100 set sashelp.zipcode; 2101 run; NOTE: There were 41988 observations read from the data set SASHELP.ZIPCODE. NOTE: The data set ZIP.MARCH2004 has 41988 observations and 16 variables.
Now that you have a backup copy of the original file saved in a different location you can replace it with the newer version. Make sure to check the log output to ensure that everything was copied successfully. As seen below the file sashelp.zipcode now contains 41,759 observations and 18 variables and has been successfully replaced. Remember if there are any problems replacing the file the original is still saved in a permanent library and you can start over.
data sashelp.zipcode; set work.zipcode_07Q4_unique; run;2285 data sashelp.zipcode; 2286 set work.zipcode_07Q4_unique; 2287 run; NOTE: There were 41759 observations read from the data set WORK.ZIPCODE_07Q4_UNIQUE. NOTE: The data set SASHELP.ZIPCODE has 41759 observations and 18 variables.
Using the SAS ZIP code data file
Let’s take a look at the first ten observations in the data set to get a better understanding on what the data set has.
proc print data = sashelp.zipcode (obs=10); run;ZIP_ Obs ZIP Y X CLASS CITY STATE STATECODE STATENAME COUNTY 1 00501 40.813078 -73.046388 U Holtsville 36 NY New York 103 2 00544 40.813223 -73.049288 U Holtsville 36 NY New York 103 3 00601 18.165950 -66.723627 Adjuntas 72 PR Puerto Rico 1 4 00602 18.383005 -67.186553 Aguada 72 PR Puerto Rico 3 5 00603 18.433236 -67.151954 Aguadilla 72 PR Puerto Rico 5 6 00604 18.505289 -67.135899 P Aguadilla 72 PR Puerto Rico 5 7 00605 18.436149 -67.151346 P Aguadilla 72 PR Puerto Rico 5 8 00606 18.185616 -66.977377 Maricao 72 PR Puerto Rico 93 9 00610 18.285948 -67.144161 Anasco 72 PR Puerto Rico 11 10 00611 18.287716 -66.797578 P Angeles 72 PR Puerto Rico 141 Obs COUNTYNM MSA AREACODE TIMEZONE GMTOFFSET DST PONAME 1 Suffolk 5380 631 Eastern -5 Y Holtsville 2 Suffolk 5380 631 Eastern -5 Y Holtsville 3 Adjuntas 0 787 Atlantic -4 N Adjuntas 4 Aguada 60 787 Atlantic -4 N Aguada 5 Aguadilla 60 787 Atlantic -4 N Aguadilla 6 Aguadilla 60 787 Atlantic -4 N Aguadilla 7 Aguadilla 60 787 Atlantic -4 N Aguadilla 8 Maricao 0 787 Atlantic -4 N Maricao 9 Anasco 4840 787 Atlantic -4 N Anasco 10 Utuado 0 787 Atlantic -4 N Angeles
Besides the data file sashelp.zipcode that SAS offers in its sashelp library, SAS also offers a few ZIP code related functions. The list of functions listed below take a single ZIP code or a variable containing ZIP codes as an argument and return related information. Note that individual ZIP codes can be used with or without quotations for all the functions. However, when using a variable as the argument only the ZIPCITY function will accept a string variable. All of the functions accept numeric variables. Additional information including county, area code, latitude and longitude are contained in the dataset sashelp.zipcode as we have seen from the above proc print or from the output of proc contents.
ZIPCITY(‘90024’) will return "Los Angeles, CA"
ZIPNAME(90024) will return "CALIFORNIA"
ZIPNAMEL(‘90024’) will return "California"
ZIPSTATE(‘90024’) will return "CA"
ZIPFIPS(‘90024’) will return 6
The following is a short example showing how to use the ZIP functions. First create the data set zipcode. Next create a new data set called info that defines new variables with city, state and FIPS information. Finally print the new data set to make sure we have the correct results.
data zipcode; input Zipcode ; cards; 90024 02139 64640 92692 60640 ; run; data info; set zipcode; CityState = zipcity(zipcode); StateName = zipname(zipcode); StateNameL = zipnamel(zipcode); State = zipstate(zipcode); FIPS = zipfips(zipcode); run; proc print data = info; run;Obs Zipcode CityState StateName StateNameL State FIPS 1 90024 Los Angeles, CA CALIFORNIA California CA 6 2 2139 Cambridge, MA MASSACHUSETTS Massachusetts MA 25 3 64640 Gallatin, MO MISSOURI Missouri MO 29 4 92692 Mission Viejo, CA CALIFORNIA California CA 6 5 60640 Chicago, IL ILLINOIS Illinois IL 17
Other resources
Louise Hadden and Mike Zdeb. "ZIP Code 411: A Well-Kept SAS Secret", SUGI 31.
SAS/GIS 9.1 Spatial Data and Procedure Guide