EZ Study

Actuarial Biology Chemistry Economics Calculators Confucius Engineer

How to get beautiful geographical picture in SAS?
Zipcode-related Function in SAS & Applications in Proc Gmap

Question 1: You may have some geographacial data and want to present it a easily understandable way, how can we do that in SAS? Here are some example output we can make in SAS:
    Click to see larger picture
The coding part is relative easy, copy the following code and run in SAS, you will get some similar output:
        goptions gunit=pct cback=white htitle=4 htext=3
        colors=(PAGY LIY STY DEGY dark_yellow very_dark_yellow ) ;

        title1 "US Population in 1990";

        proc gmap map=maps.us data=maps.uscity all;
        id state;
        block pop/levels=6;
        choro pop/levels=6;
        run; quit;

Question: One of the most important data in the zipcode data. How do we get the most updated zipcode? The one in "SASHelp" library might not be updated for a while. Following those steps:

1) Go to the following page from SAS to download the most updated zipcode Then, click the "Download" link to redirect to: All Archived zipcode datasets, download the one you need.

2) You may need to log in to download the file. After you download the file, unzip it and saved it, then using the CIMPORT procedure to import the file into a SAS data library. For example:

                proc cimport file='zipcode.cpt' lib=sashelp; run;

3) From the SASHELP library and rename the new ZIPCODE data set and index from the unique name representing the version of the ZIP code file to be ZIPCODE. For example:

        proc datasets lib=sashelp; delete zipcode;
        change zipcode_unique=zipcode; run; quit;
Note the you must have write access to the SASHELP library in order to place the data set in the library.

Question 2: How to use zipcode related function? eg. give any two zipcode, can we calculate the distance between them? or given any zipcode, find the corresponding city, county, state?

1) Use ZIPCITYDISTANCE function to calculate the distance between two ZIP codes. The syntax is: distance=zipcitydistance(zip-code-1, zip-code-2); only available after SAS 9.1.

2) Use geodist function to calculate the distance:
   data _null_;
  distance1=geodist(30.68, -88.25, 35.43, -82.55);
   put 'Distance= ' distance1 'kilometers'; 
   distance2=geodist(30.68, -88.25, 35.43, -82.55, 'M'); 
   put 'Distance = ' distance2 'miles';
   /* Calculate the Geodetic Distance with Input Measured in Degrees */
   data _null_; 
   input lat1 long1 lat2 long2; 
    Distance = geodist(lat1,long1,lat2,long2,'DM');   
   put 'Distance = ' Distance 'miles';
   35.2 -78.1 37.6 -79.8    
   ; run; 

   /* Calculate the Geodetic Distance with Input Measured in Radians */
   data _null_; 
   input lat1 long1 lat2 long2; 
   pi = constant('pi'); 
   lat1 = (pi*lat1)/180;     long1 = (pi*long1)/180;
   lat2 = (pi*lat2)/180;     long2 = (pi*long2)/180;
    Distance = geodist(lat1,long1,lat2,long2,'RM');   
   put 'Distance= ' Distance 'miles';
   35.2 -78.1 37.6 -79.8    
The actual example in the documentation has two ZIP code values enclosed in single quotes, but function will work with or without quotes around the ZIP codes. If you have an invalid ZIP code (or outdated) in your data, you will receive some error: NOTE: Invalid argument to function ZIPCITYDISTANCE at ...

One convenient code to convert to z5. character zipcode format: zip_char=put(input(STORE_ZIP,8.),z5.); so if zip_num=31 which means zip_char="00031".

Check out the next tutorial to learn more about the applcations of: zipcity(zip),zipstate(zip),zipname(zip),ZIPFIPS(zip). Source: www.sas.com

Related links:

Continue to: How to label/annotate each state in SAS   SAS tutorial home
Back to: How to generate state/county geographical output?   Statistics tutorial home